test/tools/javac/api/TestTrees.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

     1 /*
     2  * Copyright (c) 2006, 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 6346249 6392177
    27  * @summary new Trees API
    28  */
    30 import com.sun.source.tree.*;
    31 import com.sun.source.util.*;
    32 import java.io.*;
    33 import java.lang.annotation.*;
    34 import java.util.*;
    35 import javax.annotation.processing.*;
    36 import javax.lang.model.SourceVersion;
    37 import javax.lang.model.element.*;
    38 import javax.lang.model.type.*;
    39 import javax.tools.*;
    40 import com.sun.tools.javac.api.JavacTool;
    41 import com.sun.tools.javac.tree.JCTree;
    42 import com.sun.tools.javac.tree.TreeInfo;
    44 @Anno
    45 @SupportedAnnotationTypes("*")
    46 public class TestTrees extends AbstractProcessor {
    48     @Anno
    49     void annoMethod() { }
    51     @Anno
    52     int annoField;
    55     static final String testSrcDir = System.getProperty("test.src");
    56     static final String testClassDir = System.getProperty("test.classes");
    57     static final String self = TestTrees.class.getName();
    58     static PrintWriter out = new PrintWriter(System.err, true);
    60     public static void main(String[] args) throws IOException {
    61         new TestTrees().run();
    62     }
    64     void run() throws IOException {
    66         JavacTool tool = JavacTool.create();
    68         DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
    69                 public void report(Diagnostic d) {
    70                     error(d.toString());
    71                 }
    72             };
    74         StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    75         Iterable<? extends JavaFileObject> files =
    76             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
    78         Iterable<String> opts = Arrays.asList("-d", ".");
    80         System.err.println("simple compilation, no processing");
    81         JavacTask task = tool.getTask(out, fm, dl, opts, null, files);
    82         task.setTaskListener(new MyTaskListener(task));
    83         if (!task.call())
    84             throw new AssertionError("compilation failed");
    86         opts =  Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self);
    88         System.err.println();
    89         System.err.println("compilation with processing");
    90         task = tool.getTask(out, fm, dl,opts, null, files);
    91         if (!task.call())
    92             throw new AssertionError("compilation failed");
    94         if (errors > 0)
    95             throw new AssertionError(errors + " errors occurred");
    96     }
    98     void testElement(Trees trees, Element e) {
    99         trees.getClass();
   100         e.getClass();
   102         System.err.println("testElement: " + e);
   103         Tree tree = trees.getTree(e);
   104         //System.err.println(tree);
   106         if (TreeInfo.symbolFor((JCTree)tree) != e)
   107             error("bad result from getTree");
   109         TreePath path = trees.getPath(e);
   110         if (path == null) {
   111             error("getPath returned null");
   112             return;
   113         }
   114         if (path.getLeaf() != tree)
   115             error("bad result from getPath");
   117         Element e2 = trees.getElement(path);
   118         if (e2 == null) {
   119             error("getElement returned null");
   120             return;
   121         }
   122         if (e2 != e)
   123             error("bad result from getElement");
   125         // The TypeMirror is not available yet when annotation processing;
   126         // it is set up later during ANALYSE.
   127         TypeMirror t = trees.getTypeMirror(path);
   128         if (t != null && t.getKind() == TypeKind.DECLARED &&
   129                 ((DeclaredType)t).asElement() != e2)
   130             error("bad result from getTypeMirror");
   132         for (AnnotationMirror m: e.getAnnotationMirrors()) {
   133             testAnnotation(trees, e, m);
   134         }
   135     }
   137     void testAnnotation(Trees trees, Element e, AnnotationMirror a) {
   138         System.err.println("testAnnotation: " + e + " " + a);
   139         Tree tree = trees.getTree(e, a);
   141         if (tree.getKind() != Tree.Kind.ANNOTATION)
   142             error("bad result from getTree");
   144         TreePath path = trees.getPath(e, a);
   145         if (path.getLeaf() != tree)
   146             error("bad result from getPath");
   147     }
   149     void error(String msg) {
   150         if (messager != null)
   151             // annotation processing will happen in a separate instance/classloader
   152             // so pass the message back to the calling instance.
   153             messager.printMessage(Diagnostic.Kind.ERROR, msg);
   154         else {
   155             System.err.println(msg);
   156             errors++;
   157         }
   159     }
   161     Messager messager;
   162     int errors;
   165     public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
   166         Trees trees = Trees.instance(processingEnv);
   167         messager = processingEnv.getMessager();
   169         for (Element e: rEnv.getRootElements()) {
   170             testElement(trees, e);
   171         }
   173         for (TypeElement anno: annos) {
   174             Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno);
   175             System.err.println("anno: " + anno);
   176             System.err.println("elts: " + elts);
   177             if (elts != null) { // 6397298, should return empty set
   178                 for (Element e: rEnv.getElementsAnnotatedWith(anno))
   179                     testElement(trees, e);
   180             }
   181         }
   183         return true;
   184     }
   186     @Override
   187     public SourceVersion getSupportedSourceVersion() {
   188         return SourceVersion.latest();
   189     }
   191     class MyTaskListener implements TaskListener {
   192         MyTaskListener(JavacTask task) {
   193             this.task = task;
   194         }
   196         public void started(TaskEvent e) {
   197             System.err.println("started " + e);
   198         }
   200         public void finished(TaskEvent e) {
   201             //System.err.println("finished " + e);
   202             switch (e.getKind()) {
   203             case ANALYZE:
   204                 testElement(Trees.instance(task), e.getTypeElement());
   205                 break;
   206             }
   207         }
   209         private final JavacTask task;
   210     }
   212 }
   214 @Retention(RetentionPolicy.SOURCE)
   215 @interface Anno {
   216 }

mercurial