jfranck@1709: /* jlahoda@2427: * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. jfranck@1709: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jfranck@1709: * jfranck@1709: * This code is free software; you can redistribute it and/or modify it jfranck@1709: * under the terms of the GNU General Public License version 2 only, as jfranck@1709: * published by the Free Software Foundation. jfranck@1709: * jfranck@1709: * This code is distributed in the hope that it will be useful, but WITHOUT jfranck@1709: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jfranck@1709: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jfranck@1709: * version 2 for more details (a copy is included in the LICENSE file that jfranck@1709: * accompanied this code). jfranck@1709: * jfranck@1709: * You should have received a copy of the GNU General Public License version jfranck@1709: * 2 along with this work; if not, write to the Free Software Foundation, jfranck@1709: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jfranck@1709: * jfranck@1709: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jfranck@1709: * or visit www.oracle.com if you need additional information or have any jfranck@1709: * questions. jfranck@1709: */ jfranck@1709: jfranck@1709: /* jfranck@1709: * @test jlahoda@2427: * @bug 8011027 8046916 jfranck@1709: * @library /tools/javac/lib jfranck@1709: * @build JavacTestingAbstractProcessor TestTypeParameterAnnotations jfranck@1709: * @compile -processor TestTypeParameterAnnotations -proc:only TestTypeParameterAnnotations.java jfranck@1709: */ jfranck@1709: jfranck@1709: import java.util.*; jfranck@1709: import java.lang.annotation.*; jfranck@1709: import javax.annotation.processing.*; jfranck@1709: import javax.lang.model.element.*; jfranck@1709: import javax.tools.*; jfranck@1709: jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="T1", jlahoda@2427: annotations={"Foo1", "Bar1", "Baz1"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="T2", annotations={}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="T3", jlahoda@2427: annotations={"Foo2", "Bar2", "Baz2"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="T4", annotations={}) jlahoda@2427: public class TestTypeParameterAnnotations<@Foo1 @Bar1 @Baz1 T1, T2, @Foo2 @Bar2 @Baz2 T3, T4> extends jlahoda@2427: JavacTestingAbstractProcessor { jfranck@1709: int round = 0; jfranck@1709: jfranck@1709: public boolean process(Set annotations, RoundEnvironment roundEnv) { jfranck@1709: if (++round == 1) { jfranck@1709: int found = (new Scanner()).scan(roundEnv.getRootElements(), null); jfranck@1709: if (found == expect) { jfranck@1709: ; //nop jfranck@1709: } else { jfranck@1709: error("unexpected number of results: expected " + expect jfranck@1709: + ", found " + found); jfranck@1709: } jfranck@1709: jfranck@1709: } jfranck@1709: return true; jfranck@1709: } jfranck@1709: jfranck@1709: class Scanner extends JavacTestingAbstractProcessor.ElementScanner { jfranck@1709: @Override jfranck@1709: public Integer visitExecutable(ExecutableElement e, Void p) { jfranck@1709: super.visitExecutable(e, p); jfranck@1709: found += check(e, e.getTypeParameters()); jfranck@1709: return found; jfranck@1709: } jfranck@1709: jfranck@1709: @Override jfranck@1709: public Integer visitType(TypeElement e, Void p) { jfranck@1709: super.visitType(e, p); jfranck@1709: found += check(e, e.getTypeParameters()); jfranck@1709: return found; jfranck@1709: } jfranck@1709: jfranck@1709: int found; jfranck@1709: } jfranck@1709: jfranck@1709: int check(Element e, List typarams) { jfranck@1709: if (typarams.isEmpty()) jfranck@1709: return 0; jfranck@1709: jlahoda@2427: for (TypeParameterElement tpe : typarams) { jlahoda@2427: ExpectedTypeParameterAnnotations expected = null; jlahoda@2427: for (ExpectedTypeParameterAnnotations a : e.getAnnotationsByType(ExpectedTypeParameterAnnotations.class)) { jlahoda@2427: if (tpe.getSimpleName().contentEquals(a.typeParameterName())) { jlahoda@2427: expected = a; jlahoda@2427: break; jlahoda@2427: } jlahoda@2427: } jlahoda@2427: if (expected == null) { jlahoda@2427: throw new IllegalStateException("Does not have expected values annotation."); jlahoda@2427: } jlahoda@2427: checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors(), expected); jlahoda@2427: checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe), expected); jlahoda@2427: checkGetAnnotation(tpe, expected); jlahoda@2427: checkGetAnnotations(tpe, expected); jfranck@1709: } jlahoda@2427: jlahoda@2427: return typarams.size(); jfranck@1709: } jfranck@1709: jlahoda@2427: void checkAnnotationMirrors(TypeParameterElement tpe, List l, ExpectedTypeParameterAnnotations expected) { jlahoda@2427: String[] expectedAnnotations = expected.annotations(); jlahoda@2427: jlahoda@2427: if (l.size() != expectedAnnotations.length) { jlahoda@2427: error("Incorrect number of annotations, got " + l.size() + jlahoda@2427: ", should be " + expectedAnnotations.length, tpe); jlahoda@2427: return ; jfranck@1709: } jfranck@1709: jlahoda@2427: for (int i = 0; i < expectedAnnotations.length; i++) { jlahoda@2427: AnnotationMirror m = l.get(i); jlahoda@2427: if (!m.getAnnotationType().asElement().equals(elements.getTypeElement(expectedAnnotations[i]))) { jlahoda@2427: error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement()); jlahoda@2427: return ; jlahoda@2427: } jfranck@1709: } jfranck@1709: } jfranck@1709: jlahoda@2427: void checkGetAnnotation(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) { jlahoda@2427: List expectedAnnotations = Arrays.asList(expected.annotations()); jfranck@1709: jlahoda@2427: for (Class c : ALL_ANNOTATIONS) { jlahoda@2427: Object a = tpe.getAnnotation(c); jfranck@1709: jlahoda@2427: if (a != null ^ expectedAnnotations.indexOf(c.getName()) != (-1)) { jlahoda@2427: error("Unexpected behavior for " + c.getName(), tpe); jlahoda@2427: return ; jlahoda@2427: } jlahoda@2427: } jfranck@1709: } jfranck@1709: jlahoda@2427: void checkGetAnnotations(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) { jlahoda@2427: List expectedAnnotations = Arrays.asList(expected.annotations()); jlahoda@2427: jlahoda@2427: for (Class c : ALL_ANNOTATIONS) { jlahoda@2427: Object[] a = tpe.getAnnotationsByType(c); jlahoda@2427: jlahoda@2427: if (a.length > 0 ^ expectedAnnotations.indexOf(c.getName()) != (-1)) { jlahoda@2427: error("Unexpected behavior for " + c.getName(), tpe); jlahoda@2427: return ; jlahoda@2427: } jfranck@1709: } jfranck@1709: } jfranck@1709: jfranck@1709: void note(String msg) { jfranck@1709: messager.printMessage(Diagnostic.Kind.NOTE, msg); jfranck@1709: } jfranck@1709: jfranck@1709: void note(String msg, Element e) { jfranck@1709: messager.printMessage(Diagnostic.Kind.NOTE, msg, e); jfranck@1709: } jfranck@1709: jfranck@1709: void error(String msg, Element e) { jfranck@1709: messager.printMessage(Diagnostic.Kind.ERROR, msg, e); jfranck@1709: } jfranck@1709: jfranck@1709: void error(String msg) { jfranck@1709: messager.printMessage(Diagnostic.Kind.ERROR, msg); jfranck@1709: } jfranck@1709: jlahoda@2427: Class[] ALL_ANNOTATIONS = new Class[] { jlahoda@2427: Foo1.class, Bar1.class, Baz1.class, jlahoda@2427: Foo2.class, Bar2.class, Baz2.class, jlahoda@2427: }; jlahoda@2427: jfranck@1709: // additional generic elements to test jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="W", jlahoda@2427: annotations={"Foo1", "Bar1", "Baz1"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Y", jlahoda@2427: annotations={"Foo2", "Bar2", "Baz2"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) jlahoda@2427: <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> X m(X x) { return x; } jfranck@1709: jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="W", jlahoda@2427: annotations={"Foo1", "Bar1", "Baz1"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Y", jlahoda@2427: annotations={"Foo2", "Bar2", "Baz2"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) jlahoda@2427: interface Intf<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> { X m() ; } jfranck@1709: jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="W", jlahoda@2427: annotations={"Foo1", "Bar1", "Baz1"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Y", jlahoda@2427: annotations={"Foo2", "Bar2", "Baz2"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) jlahoda@2427: class Clazz<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> { jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="W", jlahoda@2427: annotations={"Foo1", "Bar1", "Baz1"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Y", jlahoda@2427: annotations={"Foo2", "Bar2", "Baz2"}) jlahoda@2427: @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={}) jlahoda@2427: <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> Clazz() { } jfranck@1709: } jfranck@1709: jlahoda@2427: final int expect = 5 * 4; // top level class, plus preceding examples, 4 type variables each jfranck@1709: } jfranck@1709: jfranck@1709: @Target(ElementType.TYPE_PARAMETER) jlahoda@2427: @interface Foo1 {} jfranck@1709: jfranck@1709: @Target(ElementType.TYPE_PARAMETER) jlahoda@2427: @interface Bar1 {} jfranck@1709: jfranck@1709: @Target(ElementType.TYPE_PARAMETER) jlahoda@2427: @interface Baz1 {} jlahoda@2427: jlahoda@2427: @Target(ElementType.TYPE_PARAMETER) jlahoda@2427: @interface Foo2 {} jlahoda@2427: jlahoda@2427: @Target(ElementType.TYPE_PARAMETER) jlahoda@2427: @interface Bar2 {} jlahoda@2427: jlahoda@2427: @Target(ElementType.TYPE_PARAMETER) jlahoda@2427: @interface Baz2 {} jlahoda@2427: jlahoda@2427: @Repeatable(ExpectedTypeParameterAnnotationsCollection.class) jlahoda@2427: @interface ExpectedTypeParameterAnnotations { jlahoda@2427: public String typeParameterName(); jlahoda@2427: public String[] annotations(); jlahoda@2427: } jlahoda@2427: jlahoda@2427: @interface ExpectedTypeParameterAnnotationsCollection { jlahoda@2427: public ExpectedTypeParameterAnnotations[] value(); jlahoda@2427: }