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