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