test/tools/javac/T6956462/T6956462.java

Thu, 04 Nov 2010 15:54:46 -0700

author
cl
date
Thu, 04 Nov 2010 15:54:46 -0700
changeset 720
5bb96781fb58
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk7-b117 for changeset 2129a046f117

     1 /*
     2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 6956462
    27  * @summary AssertionError exception throws in the Compiler Tree API in JDK 7.
    28  *
    29  * @build TestClass T6956462
    30  * @run main T6956462
    31  */
    33 import java.io.*;
    34 import java.net.URISyntaxException;
    35 import java.util.*;
    36 import javax.tools.*;
    37 import javax.tools.JavaCompiler.CompilationTask;
    38 import com.sun.source.tree.*;
    39 import com.sun.source.util.*;
    41 public class T6956462 {
    42     public static void main(String[] args) throws Exception {
    43         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    44         if (compiler == null) {
    45             throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    46         }
    47         StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    48         List<File> files = new ArrayList<File>();
    49         files.add(new File(T6956462.class.getResource("TestClass.java").toURI()));
    50         final CompilationTask task = compiler.getTask(null, fm, null,
    51             null, null, fm.getJavaFileObjectsFromFiles(files));
    52         JavacTask javacTask = (JavacTask) task;
    53         for (CompilationUnitTree cu : javacTask.parse()) {
    54             cu.accept(new MyVisitor(javacTask), null);
    55         }
    56     }
    58     private static class MyVisitor extends SimpleTreeVisitor<Tree, Void> {
    59         private final Trees trees;
    60         private CompilationUnitTree file;
    62         private MyVisitor(JavacTask javac) {
    63             this.trees = Trees.instance(javac);
    64         }
    66         @Override
    67         public Tree visitCompilationUnit(CompilationUnitTree file, Void v) {
    68             this.file = file;
    69             for (Tree typeDecl : file.getTypeDecls()) {
    70                 typeDecl.accept(this, v);
    71             }
    72             return null;
    73         }
    75         @Override
    76         public Tree visitImport(ImportTree imp, Void v) {
    77             return null;
    78         }
    80         @Override
    81         public Tree visitMethodInvocation(MethodInvocationTree invoke, Void v) {
    82             invoke.getMethodSelect().accept(this, v);
    83             return null;
    84         }
    86         @Override
    87         public Tree visitBlock(BlockTree block, Void v) {
    88             for (StatementTree stat : block.getStatements()) {
    89                 stat.accept(this, v);
    90             }
    91             return null;
    92         }
    94         @Override
    95         public Tree visitClass(ClassTree clazz, Void v) {
    96             for (Tree member : clazz.getMembers()) {
    97                 member.accept(this, v);
    98             }
    99             return null;
   100         }
   102         @Override
   103         public Tree visitIdentifier(IdentifierTree ident, Void v) {
   104             trees.getScope(trees.getPath(file, ident));
   105             return null;
   106         }
   108         @Override
   109         public Tree visitMethod(MethodTree method, Void v) {
   110             method.getBody().accept(this, v);
   111             return null;
   112         }
   114         @Override
   115         public Tree visitMemberSelect(MemberSelectTree select, Void v) {
   116             select.getExpression().accept(this, v);
   117             return null;
   118         }
   120         @Override
   121         public Tree visitVariable(VariableTree var, Void v) {
   122             var.getInitializer().accept(this, v);
   123             return null;
   124         }
   125     }
   126 }

mercurial