Wed, 26 Mar 2014 12:18:11 +0100
8038080: annotation processors don't visit declaration parameter annotations
Reviewed-by: darcy
Contributed-by: cushon@google.com, joel.franck@oracle.com
duke@1 | 1 | /* |
jlahoda@2254 | 2 | * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /* |
duke@1 | 25 | * @test |
jfranck@2306 | 26 | * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8038080 |
duke@1 | 27 | * @summary Tests that getElementsAnnotatedWith works properly. |
duke@1 | 28 | * @author Joseph D. Darcy |
darcy@1466 | 29 | * @library /tools/javac/lib |
darcy@699 | 30 | * @build JavacTestingAbstractProcessor |
duke@1 | 31 | * @compile TestElementsAnnotatedWith.java |
duke@1 | 32 | * @compile InheritedAnnotation.java |
darcy@1876 | 33 | * @compile TpAnno.java |
jfranck@2306 | 34 | * @compile Anno.java |
duke@1 | 35 | * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java |
duke@1 | 36 | * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java |
duke@1 | 37 | * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java |
darcy@232 | 38 | * @compile -processor TestElementsAnnotatedWith -proc:only C2.java |
darcy@232 | 39 | * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java |
darcy@1876 | 40 | * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java |
jfranck@2306 | 41 | * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java |
jlahoda@2254 | 42 | * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java |
darcy@453 | 43 | * @compile Foo.java |
darcy@453 | 44 | * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo |
duke@1 | 45 | */ |
duke@1 | 46 | |
duke@1 | 47 | import java.lang.annotation.Annotation; |
duke@1 | 48 | import java.util.Collections; |
duke@1 | 49 | import java.util.Set; |
duke@1 | 50 | import java.util.HashSet; |
duke@1 | 51 | import java.util.Arrays; |
duke@1 | 52 | import javax.annotation.processing.*; |
duke@1 | 53 | import javax.lang.model.element.*; |
duke@1 | 54 | import static javax.lang.model.util.ElementFilter.*; |
duke@1 | 55 | |
duke@1 | 56 | /** |
duke@1 | 57 | * This processor verifies that the information returned by |
duke@1 | 58 | * getElementsAnnotatedWith is consistent with the expected results |
duke@1 | 59 | * stored in an AnnotatedElementInfo annotation. |
duke@1 | 60 | */ |
duke@1 | 61 | @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) |
darcy@699 | 62 | public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { |
duke@1 | 63 | |
duke@1 | 64 | public boolean process(Set<? extends TypeElement> annotations, |
duke@1 | 65 | RoundEnvironment roundEnvironment) { |
duke@1 | 66 | TypeElement annotatedElementInfoElement = |
darcy@699 | 67 | elements.getTypeElement("AnnotatedElementInfo"); |
duke@1 | 68 | Set<? extends Element> resultsMeta = Collections.emptySet(); |
duke@1 | 69 | Set<? extends Element> resultsBase = Collections.emptySet(); |
duke@1 | 70 | |
duke@1 | 71 | if (!roundEnvironment.processingOver()) { |
duke@1 | 72 | testNonAnnotations(roundEnvironment); |
duke@1 | 73 | |
duke@1 | 74 | // Verify AnnotatedElementInfo is present on the first |
duke@1 | 75 | // specified type. |
duke@1 | 76 | |
duke@1 | 77 | TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); |
duke@1 | 78 | |
duke@1 | 79 | AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); |
duke@1 | 80 | |
duke@1 | 81 | boolean failed = false; |
duke@1 | 82 | |
duke@1 | 83 | if (annotatedElementInfo == null) |
duke@1 | 84 | throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + |
duke@1 | 85 | firstType); |
duke@1 | 86 | else { |
duke@1 | 87 | // Verify that the annotation information is as |
duke@1 | 88 | // expected. |
duke@1 | 89 | |
mcimadamore@536 | 90 | Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names())); |
duke@1 | 91 | |
duke@1 | 92 | resultsMeta = |
duke@1 | 93 | roundEnvironment. |
darcy@699 | 94 | getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName())); |
duke@1 | 95 | |
duke@1 | 96 | System.err.println("Results: " + resultsMeta); |
duke@1 | 97 | |
duke@1 | 98 | if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { |
duke@1 | 99 | failed = true; |
duke@1 | 100 | System.err.printf("Bad number of elements; expected %d, got %d%n", |
duke@1 | 101 | annotatedElementInfo.expectedSize(), resultsMeta.size()); |
duke@1 | 102 | } else { |
duke@1 | 103 | for(Element element : resultsMeta) { |
duke@1 | 104 | String simpleName = element.getSimpleName().toString(); |
duke@1 | 105 | if (!expectedNames.contains(simpleName) ) { |
duke@1 | 106 | failed = true; |
duke@1 | 107 | System.err.println("Name ``" + simpleName + "'' not expected."); |
duke@1 | 108 | } |
duke@1 | 109 | } |
duke@1 | 110 | } |
duke@1 | 111 | } |
duke@1 | 112 | |
duke@1 | 113 | resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); |
duke@1 | 114 | |
duke@1 | 115 | if (!resultsMeta.equals(resultsBase)) { |
duke@1 | 116 | failed = true; |
duke@1 | 117 | System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + |
duke@1 | 118 | "\nbase: " + resultsBase); |
duke@1 | 119 | } |
duke@1 | 120 | |
duke@1 | 121 | if (failed) { |
duke@1 | 122 | System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); |
duke@1 | 123 | throw new RuntimeException(); |
duke@1 | 124 | } |
duke@1 | 125 | } else { |
duke@1 | 126 | // If processing is over without an error, the specified |
duke@1 | 127 | // elements should be empty so an empty set should be returned. |
duke@1 | 128 | resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); |
duke@1 | 129 | resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); |
duke@1 | 130 | if (!resultsMeta.isEmpty()) |
duke@1 | 131 | throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); |
duke@1 | 132 | if (!resultsBase.isEmpty()) |
duke@1 | 133 | throw new RuntimeException("Nonempty resultsBase: " + resultsBase); |
duke@1 | 134 | |
duke@1 | 135 | } |
duke@1 | 136 | return true; |
duke@1 | 137 | } |
duke@1 | 138 | |
duke@1 | 139 | private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) { |
duke@1 | 140 | try { |
duke@1 | 141 | return roundEnvironment. |
duke@1 | 142 | getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); |
duke@1 | 143 | } catch(ClassNotFoundException cnfe) { |
duke@1 | 144 | throw new RuntimeException(cnfe); |
duke@1 | 145 | } |
duke@1 | 146 | } |
duke@1 | 147 | |
duke@1 | 148 | /** |
duke@1 | 149 | * Verify non-annotation types result in |
duke@1 | 150 | * IllegalArgumentExceptions. |
duke@1 | 151 | */ |
duke@1 | 152 | private void testNonAnnotations(RoundEnvironment roundEnvironment) { |
duke@1 | 153 | try { |
duke@1 | 154 | Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); |
duke@1 | 155 | throw new RuntimeException("Illegal argument exception not thrown"); |
duke@1 | 156 | } catch(IllegalArgumentException iae) {} |
duke@1 | 157 | |
duke@1 | 158 | try { |
darcy@453 | 159 | Set<? extends Element> elements = |
darcy@453 | 160 | roundEnvironment.getElementsAnnotatedWith(processingEnv. |
darcy@453 | 161 | getElementUtils(). |
darcy@453 | 162 | getTypeElement("java.lang.Object") ); |
duke@1 | 163 | throw new RuntimeException("Illegal argument exception not thrown"); |
duke@1 | 164 | } catch(IllegalArgumentException iae) {} |
duke@1 | 165 | } |
duke@1 | 166 | } |