jfranck@1709: /* jfranck@1709: * Copyright (c) 2013, 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 jfranck@1709: * @bug 8011027 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.lang.model.util.*; jfranck@1709: import javax.tools.*; jfranck@1709: jfranck@1709: public class TestTypeParameterAnnotations<@Foo @Bar @Baz T> extends 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: if (typarams.size() != 1) jfranck@1709: return 0; jfranck@1709: jfranck@1709: for (TypeParameterElement tpe: typarams) { jfranck@1709: boolean b1 = checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors()); jfranck@1709: boolean b2 = checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe)); jfranck@1709: boolean b3 = checkGetAnnotation(tpe); jfranck@1709: boolean b4 = checkGetAnnotations(tpe); jfranck@1709: return b1 && b2 && b3 && b4 ? 1 : 0; jfranck@1709: } jfranck@1709: return 0; jfranck@1709: } jfranck@1709: jfranck@1709: boolean checkAnnotationMirrors(TypeParameterElement tpe, List l) { jfranck@1709: if (l.size() != 3) { jfranck@1709: error("To few annotations, got " + l.size() + jfranck@1709: ", should be 3", tpe); jfranck@1709: return false; jfranck@1709: } jfranck@1709: jfranck@1709: AnnotationMirror m = l.get(0); jfranck@1709: if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Foo"))) { jfranck@1709: error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement()); jfranck@1709: return false; jfranck@1709: } jfranck@1709: m = l.get(1); jfranck@1709: if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Bar"))) { jfranck@1709: error("Wrong type of annotation, was expecting @Bar", m.getAnnotationType().asElement()); jfranck@1709: return false; jfranck@1709: } jfranck@1709: m = l.get(2); jfranck@1709: if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Baz"))) { jfranck@1709: error("Wrong type of annotation, was expecting @Baz", m.getAnnotationType().asElement()); jfranck@1709: return false; jfranck@1709: } jfranck@1709: return true; jfranck@1709: } jfranck@1709: jfranck@1709: boolean checkGetAnnotation(TypeParameterElement tpe) { jfranck@1709: Foo f = tpe.getAnnotation(Foo.class); jfranck@1709: if (f == null) jfranck@1709: error("Expecting @Foo to be present in getAnnotation()", tpe); jfranck@1709: jfranck@1709: Bar b = tpe.getAnnotation(Bar.class); jfranck@1709: if (b == null) jfranck@1709: error("Expecting @Bar to be present in getAnnotation()", tpe); jfranck@1709: jfranck@1709: Baz z = tpe.getAnnotation(Baz.class); jfranck@1709: if (z == null) jfranck@1709: error("Expecting @Baz to be present in getAnnotation()", tpe); jfranck@1709: jfranck@1709: return f != null && jfranck@1709: b != null && jfranck@1709: z != null; jfranck@1709: } jfranck@1709: jfranck@1709: boolean checkGetAnnotations(TypeParameterElement tpe) { jfranck@1709: Foo[] f = tpe.getAnnotationsByType(Foo.class); jfranck@1709: if (f.length != 1) { jfranck@1709: error("Expecting 1 @Foo to be present in getAnnotationsByType()", tpe); jfranck@1709: return false; jfranck@1709: } jfranck@1709: jfranck@1709: Bar[] b = tpe.getAnnotationsByType(Bar.class); jfranck@1709: if (b.length != 1) { jfranck@1709: error("Expecting 1 @Bar to be present in getAnnotationsByType()", tpe); jfranck@1709: return false; jfranck@1709: } jfranck@1709: jfranck@1709: Baz[] z = tpe.getAnnotationsByType(Baz.class); jfranck@1709: if (z.length != 1) { jfranck@1709: error("Expecting 1 @Baz to be present in getAnnotationsByType()", tpe); jfranck@1709: return false; jfranck@1709: } jfranck@1709: jfranck@1709: return true; 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: jfranck@1709: // additional generic elements to test jfranck@1709: <@Foo @Bar @Baz X> X m(X x) { return x; } jfranck@1709: jfranck@1709: interface Intf<@Foo @Bar @Baz X> { X m() ; } jfranck@1709: jfranck@1709: class Class<@Foo @Bar @Baz X> { jfranck@1709: <@Foo @Bar @Baz Y> Class() { } jfranck@1709: } jfranck@1709: jfranck@1709: final int expect = 5; // top level class, plus preceding examples jfranck@1709: } jfranck@1709: jfranck@1709: @Target(ElementType.TYPE_PARAMETER) jfranck@1709: @interface Foo {} jfranck@1709: jfranck@1709: @Target(ElementType.TYPE_PARAMETER) jfranck@1709: @interface Bar {} jfranck@1709: jfranck@1709: @Target(ElementType.TYPE_PARAMETER) jfranck@1709: @interface Baz {}