1.1 --- a/test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java Thu Jun 19 12:22:39 2014 +0100 1.2 +++ b/test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java Wed Jun 18 10:44:16 2014 +0200 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -23,7 +23,7 @@ 1.11 1.12 /* 1.13 * @test 1.14 - * @bug 8011027 1.15 + * @bug 8011027 8046916 1.16 * @library /tools/javac/lib 1.17 * @build JavacTestingAbstractProcessor TestTypeParameterAnnotations 1.18 * @compile -processor TestTypeParameterAnnotations -proc:only TestTypeParameterAnnotations.java 1.19 @@ -33,10 +33,16 @@ 1.20 import java.lang.annotation.*; 1.21 import javax.annotation.processing.*; 1.22 import javax.lang.model.element.*; 1.23 -import javax.lang.model.util.*; 1.24 import javax.tools.*; 1.25 1.26 -public class TestTypeParameterAnnotations<@Foo @Bar @Baz T> extends JavacTestingAbstractProcessor { 1.27 +@ExpectedTypeParameterAnnotations(typeParameterName="T1", 1.28 + annotations={"Foo1", "Bar1", "Baz1"}) 1.29 +@ExpectedTypeParameterAnnotations(typeParameterName="T2", annotations={}) 1.30 +@ExpectedTypeParameterAnnotations(typeParameterName="T3", 1.31 + annotations={"Foo2", "Bar2", "Baz2"}) 1.32 +@ExpectedTypeParameterAnnotations(typeParameterName="T4", annotations={}) 1.33 +public class TestTypeParameterAnnotations<@Foo1 @Bar1 @Baz1 T1, T2, @Foo2 @Bar2 @Baz2 T3, T4> extends 1.34 + JavacTestingAbstractProcessor { 1.35 int round = 0; 1.36 1.37 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 1.38 @@ -74,82 +80,69 @@ 1.39 int check(Element e, List<? extends TypeParameterElement> typarams) { 1.40 if (typarams.isEmpty()) 1.41 return 0; 1.42 - if (typarams.size() != 1) 1.43 - return 0; 1.44 1.45 - for (TypeParameterElement tpe: typarams) { 1.46 - boolean b1 = checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors()); 1.47 - boolean b2 = checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe)); 1.48 - boolean b3 = checkGetAnnotation(tpe); 1.49 - boolean b4 = checkGetAnnotations(tpe); 1.50 - return b1 && b2 && b3 && b4 ? 1 : 0; 1.51 + for (TypeParameterElement tpe : typarams) { 1.52 + ExpectedTypeParameterAnnotations expected = null; 1.53 + for (ExpectedTypeParameterAnnotations a : e.getAnnotationsByType(ExpectedTypeParameterAnnotations.class)) { 1.54 + if (tpe.getSimpleName().contentEquals(a.typeParameterName())) { 1.55 + expected = a; 1.56 + break; 1.57 + } 1.58 + } 1.59 + if (expected == null) { 1.60 + throw new IllegalStateException("Does not have expected values annotation."); 1.61 + } 1.62 + checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors(), expected); 1.63 + checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe), expected); 1.64 + checkGetAnnotation(tpe, expected); 1.65 + checkGetAnnotations(tpe, expected); 1.66 } 1.67 - return 0; 1.68 + 1.69 + return typarams.size(); 1.70 } 1.71 1.72 - boolean checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l) { 1.73 - if (l.size() != 3) { 1.74 - error("To few annotations, got " + l.size() + 1.75 - ", should be 3", tpe); 1.76 - return false; 1.77 + void checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l, ExpectedTypeParameterAnnotations expected) { 1.78 + String[] expectedAnnotations = expected.annotations(); 1.79 + 1.80 + if (l.size() != expectedAnnotations.length) { 1.81 + error("Incorrect number of annotations, got " + l.size() + 1.82 + ", should be " + expectedAnnotations.length, tpe); 1.83 + return ; 1.84 } 1.85 1.86 - AnnotationMirror m = l.get(0); 1.87 - if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Foo"))) { 1.88 - error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement()); 1.89 - return false; 1.90 + for (int i = 0; i < expectedAnnotations.length; i++) { 1.91 + AnnotationMirror m = l.get(i); 1.92 + if (!m.getAnnotationType().asElement().equals(elements.getTypeElement(expectedAnnotations[i]))) { 1.93 + error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement()); 1.94 + return ; 1.95 + } 1.96 } 1.97 - m = l.get(1); 1.98 - if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Bar"))) { 1.99 - error("Wrong type of annotation, was expecting @Bar", m.getAnnotationType().asElement()); 1.100 - return false; 1.101 - } 1.102 - m = l.get(2); 1.103 - if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Baz"))) { 1.104 - error("Wrong type of annotation, was expecting @Baz", m.getAnnotationType().asElement()); 1.105 - return false; 1.106 - } 1.107 - return true; 1.108 } 1.109 1.110 - boolean checkGetAnnotation(TypeParameterElement tpe) { 1.111 - Foo f = tpe.getAnnotation(Foo.class); 1.112 - if (f == null) 1.113 - error("Expecting @Foo to be present in getAnnotation()", tpe); 1.114 + void checkGetAnnotation(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) { 1.115 + List<String> expectedAnnotations = Arrays.asList(expected.annotations()); 1.116 1.117 - Bar b = tpe.getAnnotation(Bar.class); 1.118 - if (b == null) 1.119 - error("Expecting @Bar to be present in getAnnotation()", tpe); 1.120 + for (Class<? extends Annotation> c : ALL_ANNOTATIONS) { 1.121 + Object a = tpe.getAnnotation(c); 1.122 1.123 - Baz z = tpe.getAnnotation(Baz.class); 1.124 - if (z == null) 1.125 - error("Expecting @Baz to be present in getAnnotation()", tpe); 1.126 - 1.127 - return f != null && 1.128 - b != null && 1.129 - z != null; 1.130 + if (a != null ^ expectedAnnotations.indexOf(c.getName()) != (-1)) { 1.131 + error("Unexpected behavior for " + c.getName(), tpe); 1.132 + return ; 1.133 + } 1.134 + } 1.135 } 1.136 1.137 - boolean checkGetAnnotations(TypeParameterElement tpe) { 1.138 - Foo[] f = tpe.getAnnotationsByType(Foo.class); 1.139 - if (f.length != 1) { 1.140 - error("Expecting 1 @Foo to be present in getAnnotationsByType()", tpe); 1.141 - return false; 1.142 + void checkGetAnnotations(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) { 1.143 + List<String> expectedAnnotations = Arrays.asList(expected.annotations()); 1.144 + 1.145 + for (Class<? extends Annotation> c : ALL_ANNOTATIONS) { 1.146 + Object[] a = tpe.getAnnotationsByType(c); 1.147 + 1.148 + if (a.length > 0 ^ expectedAnnotations.indexOf(c.getName()) != (-1)) { 1.149 + error("Unexpected behavior for " + c.getName(), tpe); 1.150 + return ; 1.151 + } 1.152 } 1.153 - 1.154 - Bar[] b = tpe.getAnnotationsByType(Bar.class); 1.155 - if (b.length != 1) { 1.156 - error("Expecting 1 @Bar to be present in getAnnotationsByType()", tpe); 1.157 - return false; 1.158 - } 1.159 - 1.160 - Baz[] z = tpe.getAnnotationsByType(Baz.class); 1.161 - if (z.length != 1) { 1.162 - error("Expecting 1 @Baz to be present in getAnnotationsByType()", tpe); 1.163 - return false; 1.164 - } 1.165 - 1.166 - return true; 1.167 } 1.168 1.169 void note(String msg) { 1.170 @@ -168,23 +161,71 @@ 1.171 messager.printMessage(Diagnostic.Kind.ERROR, msg); 1.172 } 1.173 1.174 + Class<? extends Annotation>[] ALL_ANNOTATIONS = new Class[] { 1.175 + Foo1.class, Bar1.class, Baz1.class, 1.176 + Foo2.class, Bar2.class, Baz2.class, 1.177 + }; 1.178 + 1.179 // additional generic elements to test 1.180 - <@Foo @Bar @Baz X> X m(X x) { return x; } 1.181 + @ExpectedTypeParameterAnnotations(typeParameterName="W", 1.182 + annotations={"Foo1", "Bar1", "Baz1"}) 1.183 + @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) 1.184 + @ExpectedTypeParameterAnnotations(typeParameterName="Y", 1.185 + annotations={"Foo2", "Bar2", "Baz2"}) 1.186 + @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) 1.187 + <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> X m(X x) { return x; } 1.188 1.189 - interface Intf<@Foo @Bar @Baz X> { X m() ; } 1.190 + @ExpectedTypeParameterAnnotations(typeParameterName="W", 1.191 + annotations={"Foo1", "Bar1", "Baz1"}) 1.192 + @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) 1.193 + @ExpectedTypeParameterAnnotations(typeParameterName="Y", 1.194 + annotations={"Foo2", "Bar2", "Baz2"}) 1.195 + @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) 1.196 + interface Intf<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> { X m() ; } 1.197 1.198 - class Class<@Foo @Bar @Baz X> { 1.199 - <@Foo @Bar @Baz Y> Class() { } 1.200 + @ExpectedTypeParameterAnnotations(typeParameterName="W", 1.201 + annotations={"Foo1", "Bar1", "Baz1"}) 1.202 + @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) 1.203 + @ExpectedTypeParameterAnnotations(typeParameterName="Y", 1.204 + annotations={"Foo2", "Bar2", "Baz2"}) 1.205 + @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) 1.206 + class Clazz<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> { 1.207 + @ExpectedTypeParameterAnnotations(typeParameterName="W", 1.208 + annotations={"Foo1", "Bar1", "Baz1"}) 1.209 + @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) 1.210 + @ExpectedTypeParameterAnnotations(typeParameterName="Y", 1.211 + annotations={"Foo2", "Bar2", "Baz2"}) 1.212 + @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) 1.213 + <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> Clazz() { } 1.214 } 1.215 1.216 - final int expect = 5; // top level class, plus preceding examples 1.217 + final int expect = 5 * 4; // top level class, plus preceding examples, 4 type variables each 1.218 } 1.219 1.220 @Target(ElementType.TYPE_PARAMETER) 1.221 -@interface Foo {} 1.222 +@interface Foo1 {} 1.223 1.224 @Target(ElementType.TYPE_PARAMETER) 1.225 -@interface Bar {} 1.226 +@interface Bar1 {} 1.227 1.228 @Target(ElementType.TYPE_PARAMETER) 1.229 -@interface Baz {} 1.230 +@interface Baz1 {} 1.231 + 1.232 +@Target(ElementType.TYPE_PARAMETER) 1.233 +@interface Foo2 {} 1.234 + 1.235 +@Target(ElementType.TYPE_PARAMETER) 1.236 +@interface Bar2 {} 1.237 + 1.238 +@Target(ElementType.TYPE_PARAMETER) 1.239 +@interface Baz2 {} 1.240 + 1.241 +@Repeatable(ExpectedTypeParameterAnnotationsCollection.class) 1.242 +@interface ExpectedTypeParameterAnnotations { 1.243 + public String typeParameterName(); 1.244 + public String[] annotations(); 1.245 +} 1.246 + 1.247 +@interface ExpectedTypeParameterAnnotationsCollection { 1.248 + public ExpectedTypeParameterAnnotations[] value(); 1.249 +}