test/tools/javac/T6956462/T6956462.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T6956462/T6956462.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,126 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6956462
    1.30 + * @summary AssertionError exception throws in the Compiler Tree API in JDK 7.
    1.31 + *
    1.32 + * @build TestClass T6956462
    1.33 + * @run main T6956462
    1.34 + */
    1.35 +
    1.36 +import java.io.*;
    1.37 +import java.net.URISyntaxException;
    1.38 +import java.util.*;
    1.39 +import javax.tools.*;
    1.40 +import javax.tools.JavaCompiler.CompilationTask;
    1.41 +import com.sun.source.tree.*;
    1.42 +import com.sun.source.util.*;
    1.43 +
    1.44 +public class T6956462 {
    1.45 +    public static void main(String[] args) throws Exception {
    1.46 +        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    1.47 +        if (compiler == null) {
    1.48 +            throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    1.49 +        }
    1.50 +        StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    1.51 +        List<File> files = new ArrayList<File>();
    1.52 +        files.add(new File(T6956462.class.getResource("TestClass.java").toURI()));
    1.53 +        final CompilationTask task = compiler.getTask(null, fm, null,
    1.54 +            null, null, fm.getJavaFileObjectsFromFiles(files));
    1.55 +        JavacTask javacTask = (JavacTask) task;
    1.56 +        for (CompilationUnitTree cu : javacTask.parse()) {
    1.57 +            cu.accept(new MyVisitor(javacTask), null);
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    private static class MyVisitor extends SimpleTreeVisitor<Tree, Void> {
    1.62 +        private final Trees trees;
    1.63 +        private CompilationUnitTree file;
    1.64 +
    1.65 +        private MyVisitor(JavacTask javac) {
    1.66 +            this.trees = Trees.instance(javac);
    1.67 +        }
    1.68 +
    1.69 +        @Override
    1.70 +        public Tree visitCompilationUnit(CompilationUnitTree file, Void v) {
    1.71 +            this.file = file;
    1.72 +            for (Tree typeDecl : file.getTypeDecls()) {
    1.73 +                typeDecl.accept(this, v);
    1.74 +            }
    1.75 +            return null;
    1.76 +        }
    1.77 +
    1.78 +        @Override
    1.79 +        public Tree visitImport(ImportTree imp, Void v) {
    1.80 +            return null;
    1.81 +        }
    1.82 +
    1.83 +        @Override
    1.84 +        public Tree visitMethodInvocation(MethodInvocationTree invoke, Void v) {
    1.85 +            invoke.getMethodSelect().accept(this, v);
    1.86 +            return null;
    1.87 +        }
    1.88 +
    1.89 +        @Override
    1.90 +        public Tree visitBlock(BlockTree block, Void v) {
    1.91 +            for (StatementTree stat : block.getStatements()) {
    1.92 +                stat.accept(this, v);
    1.93 +            }
    1.94 +            return null;
    1.95 +        }
    1.96 +
    1.97 +        @Override
    1.98 +        public Tree visitClass(ClassTree clazz, Void v) {
    1.99 +            for (Tree member : clazz.getMembers()) {
   1.100 +                member.accept(this, v);
   1.101 +            }
   1.102 +            return null;
   1.103 +        }
   1.104 +
   1.105 +        @Override
   1.106 +        public Tree visitIdentifier(IdentifierTree ident, Void v) {
   1.107 +            trees.getScope(trees.getPath(file, ident));
   1.108 +            return null;
   1.109 +        }
   1.110 +
   1.111 +        @Override
   1.112 +        public Tree visitMethod(MethodTree method, Void v) {
   1.113 +            method.getBody().accept(this, v);
   1.114 +            return null;
   1.115 +        }
   1.116 +
   1.117 +        @Override
   1.118 +        public Tree visitMemberSelect(MemberSelectTree select, Void v) {
   1.119 +            select.getExpression().accept(this, v);
   1.120 +            return null;
   1.121 +        }
   1.122 +
   1.123 +        @Override
   1.124 +        public Tree visitVariable(VariableTree var, Void v) {
   1.125 +            var.getInitializer().accept(this, v);
   1.126 +            return null;
   1.127 +        }
   1.128 +    }
   1.129 +}

mercurial