langtools/test/tools/javac/processing/model/type/BasicAnnoTests.java
author jjg
Tue, 15 Oct 2013 15:57:13 -0700
changeset 21041 99f5e5e97425
parent 17578 46ac954e4a84
child 24292 12414a72f8d2
permissions -rw-r--r--
8026564: import changes from type-annotations forest Reviewed-by: jjg Contributed-by: wdietl@gmail.com, steve.sides@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     1
/*
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     4
 *
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     8
 *
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    13
 * accompanied this code).
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    14
 *
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    18
 *
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    21
 * questions.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    22
 */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    23
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    24
/*
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    25
 * @test
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    26
 * @bug     1234567
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    27
 * @summary Annotations on types
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    28
 * @library /tools/javac/lib
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
    29
 * @ignore
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    30
 * @build JavacTestingAbstractProcessor DPrinter BasicAnnoTests
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    31
 * @compile/process -processor BasicAnnoTests -proc:only BasicAnnoTests.java
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    32
 */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    33
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    34
import java.io.PrintWriter;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    35
import java.lang.annotation.Annotation;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    36
import java.lang.annotation.ElementType;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    37
import java.lang.annotation.Target;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    38
import java.util.Map;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    39
import java.util.Set;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    40
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    41
import javax.annotation.processing.ProcessingEnvironment;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    42
import javax.annotation.processing.RoundEnvironment;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    43
import javax.lang.model.AnnotatedConstruct;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    44
import javax.lang.model.element.AnnotationMirror;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    45
import javax.lang.model.element.AnnotationValue;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    46
import javax.lang.model.element.Element;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    47
import javax.lang.model.element.ExecutableElement;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    48
import javax.lang.model.element.TypeElement;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    49
import javax.lang.model.type.ArrayType;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    50
import javax.lang.model.type.ExecutableType;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    51
import javax.lang.model.type.TypeMirror;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    52
import javax.lang.model.type.TypeVariable;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    53
import javax.lang.model.type.WildcardType;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    54
import javax.tools.Diagnostic.Kind;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    55
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    56
import com.sun.tools.javac.code.Symbol;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    57
import com.sun.tools.javac.code.Type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    58
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    59
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    60
/**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    61
 * The test scans this file looking for test cases annotated with @Test.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    62
 */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    63
public class BasicAnnoTests extends JavacTestingAbstractProcessor {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    64
    DPrinter dprinter;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    65
    PrintWriter out;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    66
    boolean verbose = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    67
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    68
    @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    69
    public void init(ProcessingEnvironment pEnv) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    70
        super.init(pEnv);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    71
        dprinter = new DPrinter(((JavacProcessingEnvironment) pEnv).getContext());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    72
        out = dprinter.out;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    73
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    74
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    75
    @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    76
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    77
        TestElementScanner s = new TestElementScanner();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    78
        for (Element e: roundEnv.getRootElements()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    79
            s.scan(e);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    80
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    81
        return true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    82
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    83
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    84
    void error(Element e, String msg) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    85
        messager.printMessage(Kind.ERROR, msg, e);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    86
        errors++;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    87
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    88
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    89
    int errors;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    90
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    91
    /**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    92
     * Scan an element looking for declarations annotated with @Test.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    93
     * Run a TestTypeScanner on the annotations that are found.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    94
     */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    95
    class TestElementScanner extends ElementScanner<Void,Void> {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    96
        public Void scan(Element elem, Void ignore) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    97
            AnnotationMirror test = getAnnotation(elem, Test.class.getName().replace('$', '.'));
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    98
            if (test != null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
    99
                out.println("Test: " + elem + " " + test);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   100
                TestTypeScanner s = new TestTypeScanner(elem, test);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   101
                s.scan(elem.asType(), null);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   102
                if (getPosn(test) >= s.count)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   103
                    error(elem, "position " + getPosn(test) + " not found");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   104
                if (!s.found) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   105
                    dprinter.printSymbol("element", (Symbol) elem);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   106
                    dprinter.printType("type", (Type) elem.asType());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   107
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   108
                out.println();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   109
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   110
            return super.scan(elem, ignore);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   111
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   112
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   113
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   114
    /**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   115
     * Scan the type of an element, looking for an annotation
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   116
     * to match the expected annotation specified in the @Test annotation.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   117
     */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   118
    class TestTypeScanner extends TypeScanner<Void, Void> {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   119
        Element elem;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   120
        AnnotationMirror test;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   121
        int count = 0;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   122
        boolean found = false;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   123
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   124
        TestTypeScanner(Element elem, AnnotationMirror test) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   125
            this.elem = elem;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   126
            this.test = test;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   127
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   128
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   129
        @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   130
        Void scan(TypeMirror t, Void ignore) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   131
            if (t == null)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   132
                return DEFAULT_VALUE;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   133
            if (verbose)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   134
                out.println("scan " + count + ": " + t);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   135
            if (count == getPosn(test)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   136
                String annoType = getAnnoType(test);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   137
                AnnotationMirror anno = getAnnotation(t, annoType);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   138
                if (anno == null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   139
                    error(elem, "annotation not found on " + count + ": " + t);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   140
                } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   141
                    String v = getValue(anno, "value").toString();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   142
                    if (v.equals(getExpect(test))) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   143
                        out.println("found " + anno + " as expected");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   144
                        found = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   145
                    } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   146
                        error(elem, "Unexpected value: " + v + ", expected: " + getExpect(test));
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   147
                    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   148
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   149
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   150
            count++;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   151
            return super.scan(t, ignore);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   152
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   153
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   154
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   155
    /** Get the position value from an @Test annotation mirror. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   156
    static int getPosn(AnnotationMirror test) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   157
        AnnotationValue v = getValue(test, "posn");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   158
        return (Integer) v.getValue();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   159
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   160
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   161
    /** Get the expect value from an @Test annotation mirror. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   162
    static String getExpect(AnnotationMirror test) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   163
        AnnotationValue v = getValue(test, "expect");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   164
        return (String) v.getValue();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   165
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   166
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   167
    /** Get the annoType value from an @Test annotation mirror. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   168
    static String getAnnoType(AnnotationMirror test) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   169
        AnnotationValue v = getValue(test, "annoType");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   170
        TypeMirror m = (TypeMirror) v.getValue();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   171
        return m.toString();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   172
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   173
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   174
    /**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   175
     * Get a specific annotation mirror from an annotated construct.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   176
     */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   177
    static AnnotationMirror getAnnotation(AnnotatedConstruct e, String name) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   178
        for (AnnotationMirror m: e.getAnnotationMirrors()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   179
            TypeElement te = (TypeElement) m.getAnnotationType().asElement();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   180
            if (te.getQualifiedName().contentEquals(name)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   181
                return m;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   182
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   183
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   184
        return null;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   185
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   186
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   187
    /**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   188
     * Get a specific value from an annotation mirror.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   189
     */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   190
    static AnnotationValue getValue(AnnotationMirror anno, String name) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   191
        Map<? extends ExecutableElement, ? extends AnnotationValue> map = anno.getElementValues();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   192
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> e: map.entrySet()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   193
            if (e.getKey().getSimpleName().contentEquals(name)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   194
                return e.getValue();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   195
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   196
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   197
        return null;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   198
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   199
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   200
    /**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   201
     * The Language Model API does not provide a type scanner, so provide
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   202
     * one sufficient for our needs.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   203
     */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   204
    static class TypeScanner<R, P> extends SimpleTypeVisitor<R, P> {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   205
        @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   206
        public R visitArray(ArrayType t, P p) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   207
            scan(t.getComponentType(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   208
            return super.visitArray(t, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   209
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   210
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   211
        @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   212
        public R visitExecutable(ExecutableType t, P p) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   213
            scan(t.getReceiverType());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   214
            //out.println("  params: " + t.getParameterTypes());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   215
            scan(t.getParameterTypes(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   216
            //out.println("  return: " + t.getReturnType());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   217
            scan(t.getReturnType(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   218
            //out.println("  throws: " + t.getThrownTypes());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   219
            scan(t.getThrownTypes(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   220
            return super.visitExecutable(t, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   221
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   222
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   223
        @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   224
        public R visitTypeVariable(TypeVariable t, P p) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   225
            scan(t.getLowerBound(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   226
            scan(t.getUpperBound(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   227
            return super.visitTypeVariable(t, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   228
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   229
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   230
        @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   231
        public R visitWildcard(WildcardType t, P p) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   232
            scan(t.getExtendsBound(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   233
            scan(t.getSuperBound(), p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   234
            return super.visitWildcard(t, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   235
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   236
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   237
        R scan(TypeMirror t) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   238
            return scan(t, null);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   239
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   240
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   241
        R scan(TypeMirror t, P p) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   242
            return (t == null) ? DEFAULT_VALUE : t.accept(this, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   243
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   244
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   245
        R scan(Iterable<? extends TypeMirror> iter, P p) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   246
            if (iter == null)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   247
                return DEFAULT_VALUE;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   248
            R result = DEFAULT_VALUE;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   249
            for (TypeMirror t: iter)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   250
                result = scan(t, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   251
            return result;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   252
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   253
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   254
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   255
    /** Annotation to identify test cases. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   256
    @interface Test {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   257
        /** Where to look for the annotation, expressed as a scan index. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   258
        int posn();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   259
        /** The annotation to look for. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   260
        Class<? extends Annotation> annoType();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   261
        /** The string representation of the annotation's value. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   262
        String expect();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   263
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   264
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   265
    /** Type annotation to use in test cases. */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   266
    @Target(ElementType.TYPE_USE)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   267
    public @interface TA {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   268
        int value();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   269
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   270
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   271
    @Test(posn=0, annoType=TA.class, expect="1")
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   272
    public @TA(1) int f1;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   273
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   274
    @Test(posn=0, annoType=TA.class, expect="2")
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   275
    public int @TA(2) [] f2;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   276
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   277
    @Test(posn=1, annoType=TA.class, expect="3")
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   278
    public @TA(3) int [] f3;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   279
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   280
    @Test(posn=1, annoType=TA.class, expect="4")
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   281
    public int m1(@TA(4) float a) throws Exception { return 0; }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   282
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   283
    @Test(posn=2, annoType=TA.class, expect="5")
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   284
    public @TA(5) int m2(float a) throws Exception { return 0; }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   285
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   286
    @Test(posn=3, annoType=TA.class, expect="6")
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   287
    public int m3(float a) throws @TA(6) Exception { return 0; }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents:
diff changeset
   288
}