test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8011027 8046916
aoqi@0 27 * @library /tools/javac/lib
aoqi@0 28 * @build JavacTestingAbstractProcessor TestTypeParameterAnnotations
aoqi@0 29 * @compile -processor TestTypeParameterAnnotations -proc:only TestTypeParameterAnnotations.java
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.util.*;
aoqi@0 33 import java.lang.annotation.*;
aoqi@0 34 import javax.annotation.processing.*;
aoqi@0 35 import javax.lang.model.element.*;
aoqi@0 36 import javax.tools.*;
aoqi@0 37
aoqi@0 38 @ExpectedTypeParameterAnnotations(typeParameterName="T1",
aoqi@0 39 annotations={"Foo1", "Bar1", "Baz1"})
aoqi@0 40 @ExpectedTypeParameterAnnotations(typeParameterName="T2", annotations={})
aoqi@0 41 @ExpectedTypeParameterAnnotations(typeParameterName="T3",
aoqi@0 42 annotations={"Foo2", "Bar2", "Baz2"})
aoqi@0 43 @ExpectedTypeParameterAnnotations(typeParameterName="T4", annotations={})
aoqi@0 44 public class TestTypeParameterAnnotations<@Foo1 @Bar1 @Baz1 T1, T2, @Foo2 @Bar2 @Baz2 T3, T4> extends
aoqi@0 45 JavacTestingAbstractProcessor {
aoqi@0 46 int round = 0;
aoqi@0 47
aoqi@0 48 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
aoqi@0 49 if (++round == 1) {
aoqi@0 50 int found = (new Scanner()).scan(roundEnv.getRootElements(), null);
aoqi@0 51 if (found == expect) {
aoqi@0 52 ; //nop
aoqi@0 53 } else {
aoqi@0 54 error("unexpected number of results: expected " + expect
aoqi@0 55 + ", found " + found);
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 }
aoqi@0 59 return true;
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 class Scanner extends JavacTestingAbstractProcessor.ElementScanner<Integer,Void> {
aoqi@0 63 @Override
aoqi@0 64 public Integer visitExecutable(ExecutableElement e, Void p) {
aoqi@0 65 super.visitExecutable(e, p);
aoqi@0 66 found += check(e, e.getTypeParameters());
aoqi@0 67 return found;
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 @Override
aoqi@0 71 public Integer visitType(TypeElement e, Void p) {
aoqi@0 72 super.visitType(e, p);
aoqi@0 73 found += check(e, e.getTypeParameters());
aoqi@0 74 return found;
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 int found;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 int check(Element e, List<? extends TypeParameterElement> typarams) {
aoqi@0 81 if (typarams.isEmpty())
aoqi@0 82 return 0;
aoqi@0 83
aoqi@0 84 for (TypeParameterElement tpe : typarams) {
aoqi@0 85 ExpectedTypeParameterAnnotations expected = null;
aoqi@0 86 for (ExpectedTypeParameterAnnotations a : e.getAnnotationsByType(ExpectedTypeParameterAnnotations.class)) {
aoqi@0 87 if (tpe.getSimpleName().contentEquals(a.typeParameterName())) {
aoqi@0 88 expected = a;
aoqi@0 89 break;
aoqi@0 90 }
aoqi@0 91 }
aoqi@0 92 if (expected == null) {
aoqi@0 93 throw new IllegalStateException("Does not have expected values annotation.");
aoqi@0 94 }
aoqi@0 95 checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors(), expected);
aoqi@0 96 checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe), expected);
aoqi@0 97 checkGetAnnotation(tpe, expected);
aoqi@0 98 checkGetAnnotations(tpe, expected);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 return typarams.size();
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 void checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l, ExpectedTypeParameterAnnotations expected) {
aoqi@0 105 String[] expectedAnnotations = expected.annotations();
aoqi@0 106
aoqi@0 107 if (l.size() != expectedAnnotations.length) {
aoqi@0 108 error("Incorrect number of annotations, got " + l.size() +
aoqi@0 109 ", should be " + expectedAnnotations.length, tpe);
aoqi@0 110 return ;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 for (int i = 0; i < expectedAnnotations.length; i++) {
aoqi@0 114 AnnotationMirror m = l.get(i);
aoqi@0 115 if (!m.getAnnotationType().asElement().equals(elements.getTypeElement(expectedAnnotations[i]))) {
aoqi@0 116 error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement());
aoqi@0 117 return ;
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 void checkGetAnnotation(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) {
aoqi@0 123 List<String> expectedAnnotations = Arrays.asList(expected.annotations());
aoqi@0 124
aoqi@0 125 for (Class<? extends Annotation> c : ALL_ANNOTATIONS) {
aoqi@0 126 Object a = tpe.getAnnotation(c);
aoqi@0 127
aoqi@0 128 if (a != null ^ expectedAnnotations.indexOf(c.getName()) != (-1)) {
aoqi@0 129 error("Unexpected behavior for " + c.getName(), tpe);
aoqi@0 130 return ;
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 void checkGetAnnotations(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) {
aoqi@0 136 List<String> expectedAnnotations = Arrays.asList(expected.annotations());
aoqi@0 137
aoqi@0 138 for (Class<? extends Annotation> c : ALL_ANNOTATIONS) {
aoqi@0 139 Object[] a = tpe.getAnnotationsByType(c);
aoqi@0 140
aoqi@0 141 if (a.length > 0 ^ expectedAnnotations.indexOf(c.getName()) != (-1)) {
aoqi@0 142 error("Unexpected behavior for " + c.getName(), tpe);
aoqi@0 143 return ;
aoqi@0 144 }
aoqi@0 145 }
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 void note(String msg) {
aoqi@0 149 messager.printMessage(Diagnostic.Kind.NOTE, msg);
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 void note(String msg, Element e) {
aoqi@0 153 messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 void error(String msg, Element e) {
aoqi@0 157 messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 void error(String msg) {
aoqi@0 161 messager.printMessage(Diagnostic.Kind.ERROR, msg);
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 Class<? extends Annotation>[] ALL_ANNOTATIONS = new Class[] {
aoqi@0 165 Foo1.class, Bar1.class, Baz1.class,
aoqi@0 166 Foo2.class, Bar2.class, Baz2.class,
aoqi@0 167 };
aoqi@0 168
aoqi@0 169 // additional generic elements to test
aoqi@0 170 @ExpectedTypeParameterAnnotations(typeParameterName="W",
aoqi@0 171 annotations={"Foo1", "Bar1", "Baz1"})
aoqi@0 172 @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
aoqi@0 173 @ExpectedTypeParameterAnnotations(typeParameterName="Y",
aoqi@0 174 annotations={"Foo2", "Bar2", "Baz2"})
aoqi@0 175 @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
aoqi@0 176 <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> X m(X x) { return x; }
aoqi@0 177
aoqi@0 178 @ExpectedTypeParameterAnnotations(typeParameterName="W",
aoqi@0 179 annotations={"Foo1", "Bar1", "Baz1"})
aoqi@0 180 @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
aoqi@0 181 @ExpectedTypeParameterAnnotations(typeParameterName="Y",
aoqi@0 182 annotations={"Foo2", "Bar2", "Baz2"})
aoqi@0 183 @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
aoqi@0 184 interface Intf<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> { X m() ; }
aoqi@0 185
aoqi@0 186 @ExpectedTypeParameterAnnotations(typeParameterName="W",
aoqi@0 187 annotations={"Foo1", "Bar1", "Baz1"})
aoqi@0 188 @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
aoqi@0 189 @ExpectedTypeParameterAnnotations(typeParameterName="Y",
aoqi@0 190 annotations={"Foo2", "Bar2", "Baz2"})
aoqi@0 191 @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
aoqi@0 192 class Clazz<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> {
aoqi@0 193 @ExpectedTypeParameterAnnotations(typeParameterName="W",
aoqi@0 194 annotations={"Foo1", "Bar1", "Baz1"})
aoqi@0 195 @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
aoqi@0 196 @ExpectedTypeParameterAnnotations(typeParameterName="Y",
aoqi@0 197 annotations={"Foo2", "Bar2", "Baz2"})
aoqi@0 198 @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
aoqi@0 199 <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> Clazz() { }
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 final int expect = 5 * 4; // top level class, plus preceding examples, 4 type variables each
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 @Target(ElementType.TYPE_PARAMETER)
aoqi@0 206 @interface Foo1 {}
aoqi@0 207
aoqi@0 208 @Target(ElementType.TYPE_PARAMETER)
aoqi@0 209 @interface Bar1 {}
aoqi@0 210
aoqi@0 211 @Target(ElementType.TYPE_PARAMETER)
aoqi@0 212 @interface Baz1 {}
aoqi@0 213
aoqi@0 214 @Target(ElementType.TYPE_PARAMETER)
aoqi@0 215 @interface Foo2 {}
aoqi@0 216
aoqi@0 217 @Target(ElementType.TYPE_PARAMETER)
aoqi@0 218 @interface Bar2 {}
aoqi@0 219
aoqi@0 220 @Target(ElementType.TYPE_PARAMETER)
aoqi@0 221 @interface Baz2 {}
aoqi@0 222
aoqi@0 223 @Repeatable(ExpectedTypeParameterAnnotationsCollection.class)
aoqi@0 224 @interface ExpectedTypeParameterAnnotations {
aoqi@0 225 public String typeParameterName();
aoqi@0 226 public String[] annotations();
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 @interface ExpectedTypeParameterAnnotationsCollection {
aoqi@0 230 public ExpectedTypeParameterAnnotations[] value();
aoqi@0 231 }

mercurial