langtools/test/tools/javac/treeannotests/TestProcessor.java
author mcimadamore
Wed, 26 Oct 2016 15:41:25 +0100
changeset 41856 13a056e8f16e
parent 36526 3b41f1c69604
child 44573 245bb4e6f983
permissions -rw-r--r--
8168774: Polymorhic signature method check crashes javac Summary: Check for polysig method assumes arity is greater than zero Reviewed-by: vromero
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
/*
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 15385
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
4876
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4876
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4876
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4876
diff changeset
    21
 * questions.
4876
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
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 15385
diff changeset
    24
import java.lang.reflect.Layer;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 15385
diff changeset
    25
import java.lang.reflect.Module;
4876
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    26
import java.io.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    27
import java.util.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    28
import javax.annotation.processing.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    29
import javax.lang.model.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    30
import javax.lang.model.element.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    31
import javax.lang.model.util.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    32
import javax.tools.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    33
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    34
import com.sun.source.util.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    35
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
    36
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
    37
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
    38
import com.sun.tools.javac.tree.*;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    39
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
    40
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    41
/**
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    42
 * 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
    43
 * annotations.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    44
 *
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    45
 * 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
    46
 * 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
    47
 * 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
    48
 * 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
    49
 * 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
    50
 * 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
    51
 * @Test annotation itself.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    52
 */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    53
@SupportedAnnotationTypes({"Test"})
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    54
public class TestProcessor extends AbstractProcessor {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    55
    public SourceVersion getSupportedSourceVersion() {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    56
        return SourceVersion.latest();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    57
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    58
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    59
    /** 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
    60
    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
    61
        if (renv.processingOver())
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    62
            return true;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    63
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    64
        Elements elements = processingEnv.getElementUtils();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    65
        Trees trees = Trees.instance(processingEnv);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    66
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    67
        TypeElement testAnno = elements.getTypeElement("Test");
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    68
        for (Element elem: renv.getElementsAnnotatedWith(testAnno)) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    69
            System.err.println("ELEM: " + elem);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    70
            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
    71
            System.err.println("count: " + count);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    72
            TreePath p = trees.getPath(elem);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    73
            JavaFileObject file = p.getCompilationUnit().getSourceFile();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    74
            JCTree tree = (JCTree) p.getLeaf();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    75
            System.err.println("tree: " + tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    76
            new TestScanner(file).check(tree, count);
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
        return true;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    79
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    80
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    81
    /** 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
    82
    AnnotationMirror getAnnoMirror(Element e, TypeElement anno) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    83
        Types types = processingEnv.getTypeUtils();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    84
        for (AnnotationMirror m: e.getAnnotationMirrors()) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    85
            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
    86
                return m;
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
        return null;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    89
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    90
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    91
    /** 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
    92
    <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
    93
        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
    94
            ExecutableElement ee = e.getKey();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    95
            if (ee.getSimpleName().contentEquals("value")) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    96
                AnnotationValue av = e.getValue();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    97
                return type.cast(av.getValue());
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
    98
            }
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
        return null;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   101
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   102
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   103
    /** 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
   104
    void error(String msg) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   105
        Messager messager = processingEnv.getMessager();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   106
        messager.printMessage(Diagnostic.Kind.ERROR, msg);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   107
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   108
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   109
    /** 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
   110
    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
   111
        // 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
   112
        Messager messager = processingEnv.getMessager();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   113
        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
   114
        messager.printMessage(Diagnostic.Kind.ERROR, text);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   115
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   116
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   117
    /** 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
   118
     * 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
   119
     * 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
   120
     * 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
   121
     * 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
   122
     */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   123
    int getLine(JavaFileObject file, JCTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   124
        try {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   125
            CharSequence cs = file.getCharContent(true);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   126
            int line = 1;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   127
            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
   128
                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
   129
                    line++;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   130
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   131
            return line;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   132
        } catch (IOException e) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   133
            return -1;
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
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   136
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   137
    /** 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
   138
     * 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
   139
     * parameter of the annotation.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   140
     */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   141
    class TestScanner extends TreeScanner {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   142
        /** 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
   143
        TestScanner(JavaFileObject file) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   144
            this.file = file;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   145
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   146
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   147
        /** 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
   148
        void check(JCTree tree, int expectCount) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   149
            foundCount = 0;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   150
            scan(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   151
            if (foundCount != expectCount)
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   152
                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
   153
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   154
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   155
        /** 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
   156
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   157
        public void visitClassDef(JCClassDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   158
            super.visitClassDef(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   159
            check(tree.mods.annotations, "DA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   160
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   161
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   162
        /** 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
   163
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   164
        public void visitMethodDef(JCMethodDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   165
            super.visitMethodDef(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   166
            check(tree.mods.annotations, "DA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   167
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   168
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   169
        /** 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
   170
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   171
        public void visitVarDef(JCVariableDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   172
            super.visitVarDef(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   173
            check(tree.mods.annotations, "DA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   174
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   175
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   176
        /** Check @TA annotations on a type. */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   177
        public void visitAnnotatedType(JCAnnotatedType tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   178
            super.visitAnnotatedType(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   179
            check(tree.annotations, "TA", tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   180
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   181
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   182
        /** 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
   183
         * 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
   184
         * 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
   185
         * node.
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   186
         * @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
   187
         * @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
   188
         * @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
   189
         */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   190
        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
   191
            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
   192
                JCAnnotation anno = l.head;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   193
                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
   194
                    String expect = getStringValue(anno.args.head);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   195
                    foundCount++;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   196
                    System.err.println("found: " + name + " " + expect);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   197
                    String found = new TypePrinter().print(tree);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   198
                    if (!found.equals(expect))
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   199
                        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
   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
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   203
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   204
        /** 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
   205
         * 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
   206
         */
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   207
        String getStringValue(JCExpression e) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 5520
diff changeset
   208
            if (e.hasTag(JCTree.Tag.ASSIGN)) {
4876
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   209
                JCAssign a = (JCAssign) e;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   210
                JCExpression rhs = a.rhs;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 5520
diff changeset
   211
                if (rhs.hasTag(JCTree.Tag.LITERAL)) {
4876
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   212
                    JCLiteral l = (JCLiteral) rhs;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   213
                    return (String) l.value;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   214
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 5520
diff changeset
   215
            } else if (e.hasTag(JCTree.Tag.LITERAL)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 5520
diff changeset
   216
                JCLiteral l = (JCLiteral) e;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 5520
diff changeset
   217
                return (String) l.value;
4876
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   218
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   219
            throw new IllegalArgumentException(e.toString());
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   220
        }
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
        /** 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
   223
        JavaFileObject file;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   224
        /** 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
   225
        int foundCount;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   226
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   227
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   228
    /** 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
   229
    class TypePrinter extends Visitor {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   230
        /** 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
   231
        String print(JCTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   232
            if (tree == null)
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   233
                return null;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   234
            tree.accept(this);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   235
            return result;
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
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   238
        String print(List<? extends JCTree> list) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   239
            return print(list, ", ");
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   240
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   241
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   242
        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
   243
            StringBuilder sb = new StringBuilder();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   244
            if (list.nonEmpty()) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   245
                sb.append(print(list.head));
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   246
                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
   247
                    sb.append(sep);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   248
                    sb.append(print(l.head));
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   249
                }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   250
            }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   251
            return sb.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 visitClassDef(JCClassDecl 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 visitMethodDef(JCMethodDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   261
            result = tree.name.toString();
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 visitVarDef(JCVariableDecl tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   266
            tree.vartype.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 visitAnnotatedType(JCAnnotatedType tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   271
            tree.underlyingType.accept(this);
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 visitTypeIdent(JCPrimitiveTypeTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   276
            result = tree.toString();
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 visitTypeArray(JCArrayTypeTree tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   281
            result = print(tree.elemtype) + "[]";
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 visitTypeApply(JCTypeApply tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   286
            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
   287
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   288
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   289
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   290
        public void visitTypeParameter(JCTypeParameter tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   291
            if (tree.bounds.isEmpty())
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   292
                result = tree.name.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   293
            else
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   294
                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
   295
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   296
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   297
        @Override
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   298
        public void visitWildcard(JCWildcard tree) {
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   299
            if (tree.kind.kind == BoundKind.UNBOUND)
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   300
                result = tree.kind.toString();
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   301
            else
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   302
                result = tree.kind + " " + print(tree.inner);
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   303
        }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   304
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   305
        private String result;
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   306
    }
321ed2a5afcf 6921979: add test program to verify annotations are attached to nodes as expected
jjg
parents:
diff changeset
   307
}