langtools/test/tools/javac/treeannotests/TestProcessor.java
author jjg
Wed, 03 Feb 2010 16:58:57 -0800
changeset 4876 321ed2a5afcf
child 5520 86e4b9a9da40
permissions -rw-r--r--
6921979: add test program to verify annotations are attached to nodes as expected Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4876
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     1
/*
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     2
 * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     4
 *
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     8
 *
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    13
 * accompanied this code).
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    14
 *
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    18
 *
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    21
 * have any questions.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    22
 */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    23
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    24
import java.io.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    25
import java.util.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    26
import javax.annotation.processing.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    27
import javax.lang.model.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    28
import javax.lang.model.element.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    29
import javax.lang.model.util.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    30
import javax.tools.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    31
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    32
import com.sun.source.util.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.code.BoundKind;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.tree.JCTree.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.tree.TreeScanner;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    36
import com.sun.tools.javac.tree.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.util.List;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    38
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    39
/**
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    40
 * Test processor used to check test programs using the @Test, @DA, and @TA
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    41
 * annotations.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    42
 *
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    43
 * The processor looks for elements annotated with @Test, and analyzes the
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    44
 * syntax trees for those elements. Within such trees, the processor looks
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    45
 * for the DA annotations on decls and TA annotations on types.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    46
 * The value of these annotations should be a simple string rendition of
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    47
 * the tree node to which it is attached.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    48
 * The expected number of annotations is given by the parameter to the
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    49
 * @Test annotation itself.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    50
 */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    51
@SupportedAnnotationTypes({"Test"})
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    52
public class TestProcessor extends AbstractProcessor {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    53
    public SourceVersion getSupportedSourceVersion() {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    54
        return SourceVersion.latest();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    55
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    56
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    57
    /** Process trees for elements annotated with the @Test(n) annotation. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    58
    public boolean process(Set<? extends TypeElement> annos, RoundEnvironment renv) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    59
        if (renv.processingOver())
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    60
            return true;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    61
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    62
        Elements elements = processingEnv.getElementUtils();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    63
        Trees trees = Trees.instance(processingEnv);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    64
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    65
        TypeElement testAnno = elements.getTypeElement("Test");
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    66
        for (Element elem: renv.getElementsAnnotatedWith(testAnno)) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    67
            System.err.println("ELEM: " + elem);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    68
            int count = getValue(getAnnoMirror(elem, testAnno), Integer.class);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    69
            System.err.println("count: " + count);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    70
            TreePath p = trees.getPath(elem);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    71
            JavaFileObject file = p.getCompilationUnit().getSourceFile();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    72
            JCTree tree = (JCTree) p.getLeaf();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    73
            System.err.println("tree: " + tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    74
            new TestScanner(file).check(tree, count);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    75
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    76
        return true;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    77
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    78
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    79
    /** Get the AnnotationMirror on an element for a given annotation. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    80
    AnnotationMirror getAnnoMirror(Element e, TypeElement anno) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    81
        Types types = processingEnv.getTypeUtils();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    82
        for (AnnotationMirror m: e.getAnnotationMirrors()) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    83
            if (types.isSameType(m.getAnnotationType(), anno.asType()))
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    84
                return m;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    85
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    86
        return null;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    87
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    88
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    89
    /** Get the value of the value element of an annotation mirror. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    90
    <T> T getValue(AnnotationMirror m, Class<T> type) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    91
        for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> e: m.getElementValues().entrySet()) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    92
            ExecutableElement ee = e.getKey();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    93
            if (ee.getSimpleName().contentEquals("value")) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    94
                AnnotationValue av = e.getValue();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    95
                return type.cast(av.getValue());
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    96
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    97
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    98
        return null;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    99
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   100
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   101
    /** Report an error to the annotation processing system. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   102
    void error(String msg) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   103
        Messager messager = processingEnv.getMessager();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   104
        messager.printMessage(Diagnostic.Kind.ERROR, msg);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   105
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   106
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   107
    /** Report an error to the annotation processing system. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   108
    void error(JavaFileObject file, JCTree tree, String msg) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   109
        // need better API for reporting tree position errors to the messager
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   110
        Messager messager = processingEnv.getMessager();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   111
        String text = file.getName() + ":" + getLine(file, tree) + ": " + msg;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   112
        messager.printMessage(Diagnostic.Kind.ERROR, text);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   113
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   114
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   115
    /** Get the line number for the primary position for a tree.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   116
     * The code is intended to be simple, although not necessarily efficient.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   117
     * However, note that a file manager such as JavacFileManager is likely
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   118
     * to cache the results of file.getCharContent, avoiding the need to read
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   119
     * the bits from disk each time this method is called.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   120
     */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   121
    int getLine(JavaFileObject file, JCTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   122
        try {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   123
            CharSequence cs = file.getCharContent(true);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   124
            int line = 1;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   125
            for (int i = 0; i < tree.pos; i++) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   126
                if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   127
                    line++;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   128
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   129
            return line;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   130
        } catch (IOException e) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   131
            return -1;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   132
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   133
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   134
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   135
    /** Scan a tree, looking for @DA and @TA annotations, and verifying that such
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   136
     * annotations are attached to the expected tree node matching the string
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   137
     * parameter of the annotation.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   138
     */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   139
    class TestScanner extends TreeScanner {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   140
        /** Create a scanner for a given file. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   141
        TestScanner(JavaFileObject file) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   142
            this.file = file;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   143
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   144
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   145
        /** Check the annotations in a given tree. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   146
        void check(JCTree tree, int expectCount) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   147
            foundCount = 0;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   148
            scan(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   149
            if (foundCount != expectCount)
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   150
                error(file, tree, "Wrong number of annotations found: " + foundCount + ", expected: " + expectCount);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   151
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   152
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   153
        /** Check @DA annotations on a class declaration. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   154
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   155
        public void visitClassDef(JCClassDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   156
            super.visitClassDef(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   157
            check(tree.mods.annotations, "DA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   158
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   159
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   160
        /** Check @DA annotations on a method declaration. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   161
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   162
        public void visitMethodDef(JCMethodDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   163
            super.visitMethodDef(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   164
            check(tree.mods.annotations, "DA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   165
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   166
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   167
        /** Check @DA annotations on a field, parameter or local variable declaration. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   168
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   169
        public void visitVarDef(JCVariableDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   170
            super.visitVarDef(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   171
            check(tree.mods.annotations, "DA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   172
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   173
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   174
        /** Check @TA annotations on a type. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   175
        public void visitAnnotatedType(JCAnnotatedType tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   176
            super.visitAnnotatedType(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   177
            check(tree.annotations, "TA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   178
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   179
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   180
        /** Check to see if a list of annotations contains a named annotation, and
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   181
         * if so, verify the annotation is expected by comparing the value of the
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   182
         * annotation's argument against the string rendition of the reference tree
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   183
         * node.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   184
         * @param annos the list of annotations to be checked
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   185
         * @param name  the name of the annotation to be checked
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   186
         * @param tree  the tree against which to compare the annotations's argument
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   187
         */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   188
        void check(List<? extends JCAnnotation> annos, String name, JCTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   189
            for (List<? extends JCAnnotation> l = annos; l.nonEmpty(); l = l.tail) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   190
                JCAnnotation anno = l.head;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   191
                if (anno.annotationType.toString().equals(name) && (anno.args.size() == 1)) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   192
                    String expect = getStringValue(anno.args.head);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   193
                    foundCount++;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   194
                    System.err.println("found: " + name + " " + expect);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   195
                    String found = new TypePrinter().print(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   196
                    if (!found.equals(expect))
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   197
                        error(file, anno, "Unexpected result: expected: \"" + expect + "\", found: \"" + found + "\"");
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   198
                }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   199
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   200
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   201
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   202
        /** Get the string value of an annotation argument, which is given by the
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   203
         * expression <i>name</i>=<i>value</i>.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   204
         */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   205
        String getStringValue(JCExpression e) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   206
            if (e.getTag() == JCTree.ASSIGN)  {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   207
                JCAssign a = (JCAssign) e;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   208
                JCExpression rhs = a.rhs;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   209
                if (rhs.getTag() == JCTree.LITERAL) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   210
                    JCLiteral l = (JCLiteral) rhs;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   211
                    return (String) l.value;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   212
                }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   213
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   214
            throw new IllegalArgumentException(e.toString());
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   215
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   216
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   217
        /** The file for the tree. Used to locate errors. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   218
        JavaFileObject file;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   219
        /** The number of annotations that have been found. @see #check */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   220
        int foundCount;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   221
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   222
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   223
    /** Convert a type or decl tree to a reference string used by the @DA and @TA annotations. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   224
    class TypePrinter extends Visitor {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   225
        /** Convert a type or decl tree to a string. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   226
        String print(JCTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   227
            if (tree == null)
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   228
                return null;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   229
            tree.accept(this);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   230
            return result;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   231
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   232
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   233
        String print(List<? extends JCTree> list) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   234
            return print(list, ", ");
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   235
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   236
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   237
        String print(List<? extends JCTree> list, String sep) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   238
            StringBuilder sb = new StringBuilder();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   239
            if (list.nonEmpty()) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   240
                sb.append(print(list.head));
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   241
                for (List<? extends JCTree> l = list.tail; l.nonEmpty(); l = l.tail) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   242
                    sb.append(sep);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   243
                    sb.append(print(l.head));
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   244
                }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   245
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   246
            return sb.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   247
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   248
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   249
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   250
        public void visitClassDef(JCClassDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   251
            result = tree.name.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   252
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   253
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   254
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   255
        public void visitMethodDef(JCMethodDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   256
            result = tree.name.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   257
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   258
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   259
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   260
        public void visitVarDef(JCVariableDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   261
            tree.vartype.accept(this);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   262
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   263
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   264
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   265
        public void visitAnnotatedType(JCAnnotatedType tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   266
            tree.underlyingType.accept(this);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   267
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   268
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   269
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   270
        public void visitTypeIdent(JCPrimitiveTypeTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   271
            result = tree.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   272
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   273
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   274
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   275
        public void visitTypeArray(JCArrayTypeTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   276
            result = print(tree.elemtype) + "[]";
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   277
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   278
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   279
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   280
        public void visitTypeApply(JCTypeApply tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   281
            result = print(tree.clazz) + "<" + print(tree.arguments) + ">";
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   282
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   283
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   284
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   285
        public void visitTypeParameter(JCTypeParameter tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   286
            if (tree.bounds.isEmpty())
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   287
                result = tree.name.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   288
            else
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   289
                result = tree.name + " extends " + print(tree.bounds, "&");
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   290
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   291
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   292
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   293
        public void visitWildcard(JCWildcard tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   294
            if (tree.kind.kind == BoundKind.UNBOUND)
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   295
                result = tree.kind.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   296
            else
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   297
                result = tree.kind + " " + print(tree.inner);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   298
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   299
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   300
        private String result;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   301
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   302
}