langtools/test/tools/javac/api/TestTrees.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6346249 6392177
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary new Trees API
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.source.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.lang.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.api.JavacTool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
@Anno
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
@SupportedAnnotationTypes("*")
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
public class TestTrees extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    @Anno
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    void annoMethod() { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    @Anno
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    int annoField;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    static final String testSrcDir = System.getProperty("test.src");
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    static final String testClassDir = System.getProperty("test.classes");
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    static final String self = TestTrees.class.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    static PrintWriter out = new PrintWriter(System.err, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        new TestTrees().run();
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    void run() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        JavacTool tool = JavacTool.create();
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                public void report(Diagnostic d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                    error(d.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        Iterable<? extends JavaFileObject> files =
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        Iterable<String> opts = Arrays.asList("-d", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        System.err.println("simple compilation, no processing");
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        JavacTask task = tool.getTask(out, fm, dl, opts, null, files);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        task.setTaskListener(new MyTaskListener(task));
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if (!task.call())
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            throw new AssertionError("compilation failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        opts =  Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        System.err.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        System.err.println("compilation with processing");
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        task = tool.getTask(out, fm, dl,opts, null, files);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        if (!task.call())
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            throw new AssertionError("compilation failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        if (errors > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            throw new AssertionError(errors + " errors occurred");
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    void testElement(Trees trees, Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        trees.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        e.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        System.err.println("testElement: " + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        Tree tree = trees.getTree(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        //System.err.println(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        if (TreeInfo.symbolFor((JCTree)tree) != e)
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            error("bad result from getTree");
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        TreePath path = trees.getPath(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        if (path == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            error("getPath returned null");
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        if (path.getLeaf() != tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            error("bad result from getPath");
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        Element e2 = trees.getElement(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        if (e2 == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            error("getElement returned null");
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        if (e2 != e)
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            error("bad result from getElement");
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        // The TypeMirror is not available yet when annotation processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        // it is set up later during ANALYSE.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        TypeMirror t = trees.getTypeMirror(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        if (t != null && t.getKind() == TypeKind.DECLARED &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                ((DeclaredType)t).asElement() != e2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            error("bad result from getTypeMirror");
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        for (AnnotationMirror m: e.getAnnotationMirrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            testAnnotation(trees, e, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    void testAnnotation(Trees trees, Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        System.err.println("testAnnotation: " + e + " " + a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        Tree tree = trees.getTree(e, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        if (tree.getKind() != Tree.Kind.ANNOTATION)
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            error("bad result from getTree");
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        TreePath path = trees.getPath(e, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        if (path.getLeaf() != tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            error("bad result from getPath");
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    void error(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        if (messager != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            // annotation processing will happen in a separate instance/classloader
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            // so pass the message back to the calling instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            messager.printMessage(Diagnostic.Kind.ERROR, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            System.err.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            errors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    Messager messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    int errors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        Trees trees = Trees.instance(processingEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        messager = processingEnv.getMessager();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        for (Element e: rEnv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            testElement(trees, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        for (TypeElement anno: annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            System.err.println("anno: " + anno);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            System.err.println("elts: " + elts);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            if (elts != null) { // 6397298, should return empty set
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                for (Element e: rEnv.getElementsAnnotatedWith(anno))
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                    testElement(trees, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    public SourceVersion getSupportedSourceVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        return SourceVersion.latest();
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    class MyTaskListener implements TaskListener {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        MyTaskListener(JavacTask task) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            this.task = task;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        public void started(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            System.err.println("started " + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        public void finished(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            //System.err.println("finished " + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            switch (e.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            case ANALYZE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                testElement(Trees.instance(task), e.getTypeElement());
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        private final JavacTask task;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
@Retention(RetentionPolicy.SOURCE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
@interface Anno {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
}