test/tools/javac/api/TestGetElement.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestGetElement.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,235 @@
     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 6930507
    1.30 + * @summary Symbols for anonymous and local classes made too late for use by java tree API
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +import javax.annotation.processing.*;
    1.36 +import javax.lang.model.SourceVersion;
    1.37 +import javax.lang.model.element.*;
    1.38 +import javax.tools.Diagnostic;
    1.39 +import static javax.lang.model.util.ElementFilter.*;
    1.40 +
    1.41 +import com.sun.source.tree.*;
    1.42 +import com.sun.source.util.*;
    1.43 +
    1.44 +@SupportedOptions({"test", "last"})
    1.45 +@SupportedAnnotationTypes("*")
    1.46 +public class TestGetElement extends AbstractProcessor {
    1.47 +    public static void main(String... args) throws Exception {
    1.48 +        new TestGetElement().run();
    1.49 +    }
    1.50 +
    1.51 +    public TestGetElement() { }
    1.52 +
    1.53 +    public void run() throws Exception {
    1.54 +        final String testSrc = System.getProperty("test.src");
    1.55 +        final String testClasses = System.getProperty("test.classes");
    1.56 +        final String myClassName = getClass().getName();
    1.57 +        final String mySrc = new File(testSrc, myClassName + ".java").getPath();
    1.58 +
    1.59 +        final int NUM_TESTS = 90; // #decls in this source file
    1.60 +        for (int i = 1; i <= NUM_TESTS; i++) {
    1.61 +            System.err.println("test " + i);
    1.62 +            File testDir = new File("test" + i);
    1.63 +            File classesDir = new File(testDir, "classes");
    1.64 +            classesDir.mkdirs();
    1.65 +            String[] args = {
    1.66 +                "-d", classesDir.getPath(),
    1.67 +                "-processorpath", testClasses,
    1.68 +                "-processor", myClassName,
    1.69 +                "-proc:only",
    1.70 +                "-Atest=" + i,
    1.71 +                "-Alast=" + (i == NUM_TESTS),
    1.72 +                mySrc
    1.73 +            };
    1.74 +
    1.75 +//            System.err.println("compile: " + Arrays.asList(args));
    1.76 +
    1.77 +            StringWriter sw = new StringWriter();
    1.78 +            PrintWriter pw = new PrintWriter(sw);
    1.79 +            int rc = com.sun.tools.javac.Main.compile(args, pw);
    1.80 +            pw.close();
    1.81 +            String out = sw.toString();
    1.82 +            if (out != null)
    1.83 +                System.err.println(out);
    1.84 +            if (rc != 0) {
    1.85 +                System.err.println("compilation failed: rc=" + rc);
    1.86 +                errors++;
    1.87 +            }
    1.88 +        }
    1.89 +
    1.90 +        if (errors > 0)
    1.91 +            throw new Exception(errors + " errors occurred");
    1.92 +    }
    1.93 +
    1.94 +
    1.95 +    int errors;
    1.96 +
    1.97 +    public boolean process(Set<? extends TypeElement> annotations,
    1.98 +                           RoundEnvironment roundEnvironment)
    1.99 +    {
   1.100 +        if (roundEnvironment.processingOver())
   1.101 +            return true;
   1.102 +
   1.103 +        Map<String,String> options = processingEnv.getOptions();
   1.104 +        int test = Integer.parseInt(options.get("test"));
   1.105 +        boolean _last = Boolean.parseBoolean(options.get("last"));
   1.106 +
   1.107 +        Trees trees = Trees.instance(processingEnv);
   1.108 +        Scanner scanner = new Scanner(trees, _last);
   1.109 +        int nelems = 0;
   1.110 +        for (TypeElement e : typesIn(roundEnvironment.getRootElements())) {
   1.111 +            nelems += scanner.scan(trees.getPath(e), test);
   1.112 +        }
   1.113 +
   1.114 +        Messager m = processingEnv.getMessager();
   1.115 +        int EXPECT = 1;
   1.116 +        if (nelems != EXPECT) {
   1.117 +            m.printMessage(Diagnostic.Kind.ERROR,
   1.118 +                    "Unexpected number of elements found: " + nelems + " expected: " + EXPECT);
   1.119 +        }
   1.120 +        return true;
   1.121 +    }
   1.122 +
   1.123 +    @Override
   1.124 +    public SourceVersion getSupportedSourceVersion() {
   1.125 +        return SourceVersion.latest();
   1.126 +    }
   1.127 +
   1.128 +    class Scanner extends TreePathScanner<Integer,Integer> {
   1.129 +        final Trees trees;
   1.130 +        final boolean last;
   1.131 +        int count;
   1.132 +
   1.133 +        Scanner(Trees trees, boolean last) {
   1.134 +            this.trees = trees;
   1.135 +            this.last = last;
   1.136 +        }
   1.137 +
   1.138 +        @Override
   1.139 +        public Integer visitClass(ClassTree tree, Integer test) {
   1.140 +            return reduce(check(test), super.visitClass(tree, test));
   1.141 +        }
   1.142 +
   1.143 +        @Override
   1.144 +        public Integer visitMethod(MethodTree tree, Integer test) {
   1.145 +            return reduce(check(test), super.visitMethod(tree, test));
   1.146 +        }
   1.147 +
   1.148 +        @Override
   1.149 +        public Integer visitVariable(VariableTree tree, Integer test) {
   1.150 +            return reduce(check(test), super.visitVariable(tree, test));
   1.151 +        }
   1.152 +
   1.153 +        @Override
   1.154 +        public Integer reduce(Integer i1, Integer i2) {
   1.155 +            if (i1 == null || i1.intValue() == 0)
   1.156 +                return i2;
   1.157 +            if (i2 == null || i2.intValue() == 0)
   1.158 +                return i1;
   1.159 +            return (i1 + i2);
   1.160 +        }
   1.161 +
   1.162 +        int check(int test) {
   1.163 +            count++;
   1.164 +
   1.165 +            if (count != test)
   1.166 +                return 0;
   1.167 +
   1.168 +            TreePath p = getCurrentPath();
   1.169 +            Element e = trees.getElement(p);
   1.170 +
   1.171 +            String text = p.getLeaf().toString().replaceAll("\\s+", " ").trim();
   1.172 +            int MAXLEN = 40;
   1.173 +            if (text.length() > MAXLEN)
   1.174 +                text = text.substring(0, MAXLEN - 3) + "...";
   1.175 +
   1.176 +            System.err.println(String.format("%3d: %-" + MAXLEN + "s -- %s",
   1.177 +                    count, text,
   1.178 +                    (e == null ? "null" : e.getKind() + " " + e)));
   1.179 +
   1.180 +            Messager m = processingEnv.getMessager();
   1.181 +            if (e == null) {
   1.182 +                m.printMessage(Diagnostic.Kind.ERROR, "Null element found for " + text);
   1.183 +                return 0;
   1.184 +            }
   1.185 +
   1.186 +            if (last && !e.getSimpleName().contentEquals("last")) {
   1.187 +                m.printMessage(Diagnostic.Kind.ERROR, "Unexpected name in last test: "
   1.188 +                        + e.getSimpleName() + ", expected: last");
   1.189 +            }
   1.190 +
   1.191 +            return 1;
   1.192 +        }
   1.193 +    }
   1.194 +
   1.195 +    // following are all fodder for the test
   1.196 +
   1.197 +    class MemberClass {
   1.198 +        class NestedMemberClass { }
   1.199 +    }
   1.200 +
   1.201 +    {
   1.202 +        class InnerClassInInit { }
   1.203 +        Object o = new Object() { };
   1.204 +    }
   1.205 +
   1.206 +    TestGetElement(TestGetElement unused) {
   1.207 +        class InnerClassInConstr { }
   1.208 +        Object o = new Object() { };
   1.209 +    }
   1.210 +
   1.211 +    void m() {
   1.212 +        class InnerClassInMethod { }
   1.213 +        Object o = new Object() { };
   1.214 +
   1.215 +        class C {
   1.216 +            class MemberClass {
   1.217 +                class NestedMemberClass { }
   1.218 +            }
   1.219 +
   1.220 +            {
   1.221 +                class InnerClassInInit { }
   1.222 +                Object o = new Object() { };
   1.223 +            }
   1.224 +
   1.225 +            C(Object unused) {
   1.226 +                class InnerClassInConstr { }
   1.227 +                Object o = new Object() { };
   1.228 +            }
   1.229 +
   1.230 +            void m() {
   1.231 +                class InnerClassInMethod { }
   1.232 +                Object o = new Object() { };
   1.233 +            }
   1.234 +        }
   1.235 +    }
   1.236 +
   1.237 +    int last; // this name is verified by the test to make sure that all decls are checked
   1.238 +}

mercurial