author | ohair |
Tue, 25 May 2010 15:54:51 -0700 | |
changeset 5520 | 86e4b9a9da40 |
parent 5320 | e2aaa958b02d |
child 6720 | f16f91662ad8 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2006, 2009, 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 |
|
4551
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
26 |
* @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 |
10 | 27 |
* @summary Tests that getElementsAnnotatedWith works properly. |
28 |
* @author Joseph D. Darcy |
|
29 |
* @compile TestElementsAnnotatedWith.java |
|
30 |
* @compile InheritedAnnotation.java |
|
31 |
* @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java |
|
32 |
* @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java |
|
33 |
* @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
|
34 |
* @compile -processor TestElementsAnnotatedWith -proc:only C2.java |
dd8663d971d1
6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
darcy
parents:
10
diff
changeset
|
35 |
* @compile -processor TestElementsAnnotatedWith -proc:only Foo.java |
4551
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
36 |
* @compile Foo.java |
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
37 |
* @compile/process -processor TestElementsAnnotatedWith -proc:only Foo |
10 | 38 |
*/ |
39 |
||
40 |
import java.lang.annotation.Annotation; |
|
2215
dd8663d971d1
6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
darcy
parents:
10
diff
changeset
|
41 |
import java.io.*; |
10 | 42 |
import java.util.Collections; |
43 |
import java.util.Set; |
|
44 |
import java.util.HashSet; |
|
2215
dd8663d971d1
6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
darcy
parents:
10
diff
changeset
|
45 |
import java.util.List; |
dd8663d971d1
6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
darcy
parents:
10
diff
changeset
|
46 |
import java.util.ArrayList; |
10 | 47 |
import java.util.Arrays; |
48 |
import javax.annotation.processing.*; |
|
2215
dd8663d971d1
6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
darcy
parents:
10
diff
changeset
|
49 |
import javax.tools.*; |
10 | 50 |
import javax.lang.model.SourceVersion; |
51 |
import javax.lang.model.element.*; |
|
52 |
import javax.lang.model.util.*; |
|
53 |
import static javax.lang.model.util.ElementFilter.*; |
|
54 |
||
55 |
/** |
|
56 |
* This processor verifies that the information returned by |
|
57 |
* getElementsAnnotatedWith is consistent with the expected results |
|
58 |
* stored in an AnnotatedElementInfo annotation. |
|
59 |
*/ |
|
60 |
@SupportedAnnotationTypes("*") |
|
61 |
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) |
|
62 |
public class TestElementsAnnotatedWith extends AbstractProcessor { |
|
63 |
||
64 |
public boolean process(Set<? extends TypeElement> annotations, |
|
65 |
RoundEnvironment roundEnvironment) { |
|
66 |
Elements elementUtils = processingEnv.getElementUtils(); |
|
67 |
||
68 |
TypeElement annotatedElementInfoElement = |
|
69 |
elementUtils.getTypeElement("AnnotatedElementInfo"); |
|
70 |
Set<? extends Element> resultsMeta = Collections.emptySet(); |
|
71 |
Set<? extends Element> resultsBase = Collections.emptySet(); |
|
72 |
||
73 |
if (!roundEnvironment.processingOver()) { |
|
74 |
testNonAnnotations(roundEnvironment); |
|
75 |
||
76 |
// Verify AnnotatedElementInfo is present on the first |
|
77 |
// specified type. |
|
78 |
||
79 |
TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); |
|
80 |
||
81 |
AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); |
|
82 |
||
83 |
boolean failed = false; |
|
84 |
||
85 |
if (annotatedElementInfo == null) |
|
86 |
throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + |
|
87 |
firstType); |
|
88 |
else { |
|
89 |
// Verify that the annotation information is as |
|
90 |
// expected. |
|
91 |
||
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
4551
diff
changeset
|
92 |
Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names())); |
10 | 93 |
|
94 |
resultsMeta = |
|
95 |
roundEnvironment. |
|
96 |
getElementsAnnotatedWith(elementUtils. |
|
97 |
getTypeElement(annotatedElementInfo. |
|
98 |
annotationName())) ; |
|
99 |
||
100 |
System.err.println("Results: " + resultsMeta); |
|
101 |
||
102 |
if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { |
|
103 |
failed = true; |
|
104 |
System.err.printf("Bad number of elements; expected %d, got %d%n", |
|
105 |
annotatedElementInfo.expectedSize(), resultsMeta.size()); |
|
106 |
} else { |
|
107 |
for(Element element : resultsMeta) { |
|
108 |
String simpleName = element.getSimpleName().toString(); |
|
109 |
if (!expectedNames.contains(simpleName) ) { |
|
110 |
failed = true; |
|
111 |
System.err.println("Name ``" + simpleName + "'' not expected."); |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); |
|
118 |
||
119 |
if (!resultsMeta.equals(resultsBase)) { |
|
120 |
failed = true; |
|
121 |
System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + |
|
122 |
"\nbase: " + resultsBase); |
|
123 |
} |
|
124 |
||
125 |
if (failed) { |
|
126 |
System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); |
|
127 |
throw new RuntimeException(); |
|
128 |
} |
|
129 |
} else { |
|
130 |
// If processing is over without an error, the specified |
|
131 |
// elements should be empty so an empty set should be returned. |
|
132 |
resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); |
|
133 |
resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); |
|
134 |
if (!resultsMeta.isEmpty()) |
|
135 |
throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); |
|
136 |
if (!resultsBase.isEmpty()) |
|
137 |
throw new RuntimeException("Nonempty resultsBase: " + resultsBase); |
|
138 |
||
139 |
} |
|
140 |
return true; |
|
141 |
} |
|
142 |
||
143 |
private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) { |
|
144 |
try { |
|
145 |
return roundEnvironment. |
|
146 |
getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); |
|
147 |
} catch(ClassNotFoundException cnfe) { |
|
148 |
throw new RuntimeException(cnfe); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Verify non-annotation types result in |
|
154 |
* IllegalArgumentExceptions. |
|
155 |
*/ |
|
156 |
private void testNonAnnotations(RoundEnvironment roundEnvironment) { |
|
157 |
try { |
|
158 |
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); |
|
159 |
throw new RuntimeException("Illegal argument exception not thrown"); |
|
160 |
} catch(IllegalArgumentException iae) {} |
|
161 |
||
162 |
try { |
|
4551
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
163 |
Set<? extends Element> elements = |
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
164 |
roundEnvironment.getElementsAnnotatedWith(processingEnv. |
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
165 |
getElementUtils(). |
a986317586f6
6911854: Update TestElementsAnnotatedWith.java to use @compile/proc
darcy
parents:
3777
diff
changeset
|
166 |
getTypeElement("java.lang.Object") ); |
10 | 167 |
throw new RuntimeException("Illegal argument exception not thrown"); |
168 |
} catch(IllegalArgumentException iae) {} |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
public SourceVersion getSupportedSourceVersion() { |
|
173 |
return SourceVersion.latest(); |
|
174 |
} |
|
175 |
} |