Mon, 22 Apr 2013 10:24:19 +0200
8011027: Type parameter annotations not passed through to javax.lang.model
Reviewed-by: jjg, darcy
jfranck@1709 | 1 | /* |
jfranck@1709 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
jfranck@1709 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jfranck@1709 | 4 | * |
jfranck@1709 | 5 | * This code is free software; you can redistribute it and/or modify it |
jfranck@1709 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jfranck@1709 | 7 | * published by the Free Software Foundation. |
jfranck@1709 | 8 | * |
jfranck@1709 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jfranck@1709 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jfranck@1709 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jfranck@1709 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jfranck@1709 | 13 | * accompanied this code). |
jfranck@1709 | 14 | * |
jfranck@1709 | 15 | * You should have received a copy of the GNU General Public License version |
jfranck@1709 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jfranck@1709 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jfranck@1709 | 18 | * |
jfranck@1709 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jfranck@1709 | 20 | * or visit www.oracle.com if you need additional information or have any |
jfranck@1709 | 21 | * questions. |
jfranck@1709 | 22 | */ |
jfranck@1709 | 23 | |
jfranck@1709 | 24 | /* |
jfranck@1709 | 25 | * @test |
jfranck@1709 | 26 | * @bug 8011027 |
jfranck@1709 | 27 | * @library /tools/javac/lib |
jfranck@1709 | 28 | * @build JavacTestingAbstractProcessor TestTypeParameterAnnotations |
jfranck@1709 | 29 | * @compile -processor TestTypeParameterAnnotations -proc:only TestTypeParameterAnnotations.java |
jfranck@1709 | 30 | */ |
jfranck@1709 | 31 | |
jfranck@1709 | 32 | import java.util.*; |
jfranck@1709 | 33 | import java.lang.annotation.*; |
jfranck@1709 | 34 | import javax.annotation.processing.*; |
jfranck@1709 | 35 | import javax.lang.model.element.*; |
jfranck@1709 | 36 | import javax.lang.model.util.*; |
jfranck@1709 | 37 | import javax.tools.*; |
jfranck@1709 | 38 | |
jfranck@1709 | 39 | public class TestTypeParameterAnnotations<@Foo @Bar @Baz T> extends JavacTestingAbstractProcessor { |
jfranck@1709 | 40 | int round = 0; |
jfranck@1709 | 41 | |
jfranck@1709 | 42 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jfranck@1709 | 43 | if (++round == 1) { |
jfranck@1709 | 44 | int found = (new Scanner()).scan(roundEnv.getRootElements(), null); |
jfranck@1709 | 45 | if (found == expect) { |
jfranck@1709 | 46 | ; //nop |
jfranck@1709 | 47 | } else { |
jfranck@1709 | 48 | error("unexpected number of results: expected " + expect |
jfranck@1709 | 49 | + ", found " + found); |
jfranck@1709 | 50 | } |
jfranck@1709 | 51 | |
jfranck@1709 | 52 | } |
jfranck@1709 | 53 | return true; |
jfranck@1709 | 54 | } |
jfranck@1709 | 55 | |
jfranck@1709 | 56 | class Scanner extends JavacTestingAbstractProcessor.ElementScanner<Integer,Void> { |
jfranck@1709 | 57 | @Override |
jfranck@1709 | 58 | public Integer visitExecutable(ExecutableElement e, Void p) { |
jfranck@1709 | 59 | super.visitExecutable(e, p); |
jfranck@1709 | 60 | found += check(e, e.getTypeParameters()); |
jfranck@1709 | 61 | return found; |
jfranck@1709 | 62 | } |
jfranck@1709 | 63 | |
jfranck@1709 | 64 | @Override |
jfranck@1709 | 65 | public Integer visitType(TypeElement e, Void p) { |
jfranck@1709 | 66 | super.visitType(e, p); |
jfranck@1709 | 67 | found += check(e, e.getTypeParameters()); |
jfranck@1709 | 68 | return found; |
jfranck@1709 | 69 | } |
jfranck@1709 | 70 | |
jfranck@1709 | 71 | int found; |
jfranck@1709 | 72 | } |
jfranck@1709 | 73 | |
jfranck@1709 | 74 | int check(Element e, List<? extends TypeParameterElement> typarams) { |
jfranck@1709 | 75 | if (typarams.isEmpty()) |
jfranck@1709 | 76 | return 0; |
jfranck@1709 | 77 | if (typarams.size() != 1) |
jfranck@1709 | 78 | return 0; |
jfranck@1709 | 79 | |
jfranck@1709 | 80 | for (TypeParameterElement tpe: typarams) { |
jfranck@1709 | 81 | boolean b1 = checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors()); |
jfranck@1709 | 82 | boolean b2 = checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe)); |
jfranck@1709 | 83 | boolean b3 = checkGetAnnotation(tpe); |
jfranck@1709 | 84 | boolean b4 = checkGetAnnotations(tpe); |
jfranck@1709 | 85 | return b1 && b2 && b3 && b4 ? 1 : 0; |
jfranck@1709 | 86 | } |
jfranck@1709 | 87 | return 0; |
jfranck@1709 | 88 | } |
jfranck@1709 | 89 | |
jfranck@1709 | 90 | boolean checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l) { |
jfranck@1709 | 91 | if (l.size() != 3) { |
jfranck@1709 | 92 | error("To few annotations, got " + l.size() + |
jfranck@1709 | 93 | ", should be 3", tpe); |
jfranck@1709 | 94 | return false; |
jfranck@1709 | 95 | } |
jfranck@1709 | 96 | |
jfranck@1709 | 97 | AnnotationMirror m = l.get(0); |
jfranck@1709 | 98 | if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Foo"))) { |
jfranck@1709 | 99 | error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement()); |
jfranck@1709 | 100 | return false; |
jfranck@1709 | 101 | } |
jfranck@1709 | 102 | m = l.get(1); |
jfranck@1709 | 103 | if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Bar"))) { |
jfranck@1709 | 104 | error("Wrong type of annotation, was expecting @Bar", m.getAnnotationType().asElement()); |
jfranck@1709 | 105 | return false; |
jfranck@1709 | 106 | } |
jfranck@1709 | 107 | m = l.get(2); |
jfranck@1709 | 108 | if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Baz"))) { |
jfranck@1709 | 109 | error("Wrong type of annotation, was expecting @Baz", m.getAnnotationType().asElement()); |
jfranck@1709 | 110 | return false; |
jfranck@1709 | 111 | } |
jfranck@1709 | 112 | return true; |
jfranck@1709 | 113 | } |
jfranck@1709 | 114 | |
jfranck@1709 | 115 | boolean checkGetAnnotation(TypeParameterElement tpe) { |
jfranck@1709 | 116 | Foo f = tpe.getAnnotation(Foo.class); |
jfranck@1709 | 117 | if (f == null) |
jfranck@1709 | 118 | error("Expecting @Foo to be present in getAnnotation()", tpe); |
jfranck@1709 | 119 | |
jfranck@1709 | 120 | Bar b = tpe.getAnnotation(Bar.class); |
jfranck@1709 | 121 | if (b == null) |
jfranck@1709 | 122 | error("Expecting @Bar to be present in getAnnotation()", tpe); |
jfranck@1709 | 123 | |
jfranck@1709 | 124 | Baz z = tpe.getAnnotation(Baz.class); |
jfranck@1709 | 125 | if (z == null) |
jfranck@1709 | 126 | error("Expecting @Baz to be present in getAnnotation()", tpe); |
jfranck@1709 | 127 | |
jfranck@1709 | 128 | return f != null && |
jfranck@1709 | 129 | b != null && |
jfranck@1709 | 130 | z != null; |
jfranck@1709 | 131 | } |
jfranck@1709 | 132 | |
jfranck@1709 | 133 | boolean checkGetAnnotations(TypeParameterElement tpe) { |
jfranck@1709 | 134 | Foo[] f = tpe.getAnnotationsByType(Foo.class); |
jfranck@1709 | 135 | if (f.length != 1) { |
jfranck@1709 | 136 | error("Expecting 1 @Foo to be present in getAnnotationsByType()", tpe); |
jfranck@1709 | 137 | return false; |
jfranck@1709 | 138 | } |
jfranck@1709 | 139 | |
jfranck@1709 | 140 | Bar[] b = tpe.getAnnotationsByType(Bar.class); |
jfranck@1709 | 141 | if (b.length != 1) { |
jfranck@1709 | 142 | error("Expecting 1 @Bar to be present in getAnnotationsByType()", tpe); |
jfranck@1709 | 143 | return false; |
jfranck@1709 | 144 | } |
jfranck@1709 | 145 | |
jfranck@1709 | 146 | Baz[] z = tpe.getAnnotationsByType(Baz.class); |
jfranck@1709 | 147 | if (z.length != 1) { |
jfranck@1709 | 148 | error("Expecting 1 @Baz to be present in getAnnotationsByType()", tpe); |
jfranck@1709 | 149 | return false; |
jfranck@1709 | 150 | } |
jfranck@1709 | 151 | |
jfranck@1709 | 152 | return true; |
jfranck@1709 | 153 | } |
jfranck@1709 | 154 | |
jfranck@1709 | 155 | void note(String msg) { |
jfranck@1709 | 156 | messager.printMessage(Diagnostic.Kind.NOTE, msg); |
jfranck@1709 | 157 | } |
jfranck@1709 | 158 | |
jfranck@1709 | 159 | void note(String msg, Element e) { |
jfranck@1709 | 160 | messager.printMessage(Diagnostic.Kind.NOTE, msg, e); |
jfranck@1709 | 161 | } |
jfranck@1709 | 162 | |
jfranck@1709 | 163 | void error(String msg, Element e) { |
jfranck@1709 | 164 | messager.printMessage(Diagnostic.Kind.ERROR, msg, e); |
jfranck@1709 | 165 | } |
jfranck@1709 | 166 | |
jfranck@1709 | 167 | void error(String msg) { |
jfranck@1709 | 168 | messager.printMessage(Diagnostic.Kind.ERROR, msg); |
jfranck@1709 | 169 | } |
jfranck@1709 | 170 | |
jfranck@1709 | 171 | // additional generic elements to test |
jfranck@1709 | 172 | <@Foo @Bar @Baz X> X m(X x) { return x; } |
jfranck@1709 | 173 | |
jfranck@1709 | 174 | interface Intf<@Foo @Bar @Baz X> { X m() ; } |
jfranck@1709 | 175 | |
jfranck@1709 | 176 | class Class<@Foo @Bar @Baz X> { |
jfranck@1709 | 177 | <@Foo @Bar @Baz Y> Class() { } |
jfranck@1709 | 178 | } |
jfranck@1709 | 179 | |
jfranck@1709 | 180 | final int expect = 5; // top level class, plus preceding examples |
jfranck@1709 | 181 | } |
jfranck@1709 | 182 | |
jfranck@1709 | 183 | @Target(ElementType.TYPE_PARAMETER) |
jfranck@1709 | 184 | @interface Foo {} |
jfranck@1709 | 185 | |
jfranck@1709 | 186 | @Target(ElementType.TYPE_PARAMETER) |
jfranck@1709 | 187 | @interface Bar {} |
jfranck@1709 | 188 | |
jfranck@1709 | 189 | @Target(ElementType.TYPE_PARAMETER) |
jfranck@1709 | 190 | @interface Baz {} |