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