36526
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
*
|
|
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.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @summary Verify that annotation processing works.
|
|
27 |
* @library /tools/lib
|
|
28 |
* @modules
|
|
29 |
* jdk.compiler/com.sun.tools.javac.api
|
|
30 |
* jdk.compiler/com.sun.tools.javac.main
|
|
31 |
* jdk.jdeps/com.sun.tools.javap
|
|
32 |
* @build ToolBox ModuleTestBase
|
|
33 |
* @run main AnnotationProcessing
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.nio.file.Files;
|
|
37 |
import java.nio.file.Path;
|
|
38 |
import java.util.Arrays;
|
|
39 |
import java.util.HashMap;
|
|
40 |
import java.util.List;
|
|
41 |
import java.util.Map;
|
|
42 |
import java.util.Objects;
|
|
43 |
import java.util.Set;
|
|
44 |
import java.util.stream.Collectors;
|
|
45 |
|
|
46 |
import javax.annotation.processing.AbstractProcessor;
|
|
47 |
import javax.annotation.processing.RoundEnvironment;
|
|
48 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
|
49 |
import javax.annotation.processing.SupportedOptions;
|
|
50 |
import javax.lang.model.SourceVersion;
|
|
51 |
import javax.lang.model.element.Element;
|
|
52 |
import javax.lang.model.element.ModuleElement;
|
|
53 |
import javax.lang.model.element.ModuleElement.ProvidesDirective;
|
|
54 |
import javax.lang.model.element.ModuleElement.UsesDirective;
|
|
55 |
import javax.lang.model.element.PackageElement;
|
|
56 |
import javax.lang.model.element.TypeElement;
|
|
57 |
import javax.lang.model.type.TypeKind;
|
|
58 |
import javax.lang.model.util.ElementFilter;
|
|
59 |
import javax.lang.model.util.ElementScanner9;
|
|
60 |
|
|
61 |
public class AnnotationProcessing extends ModuleTestBase {
|
|
62 |
|
|
63 |
public static void main(String... args) throws Exception {
|
|
64 |
new AnnotationProcessing().runTests();
|
|
65 |
}
|
|
66 |
|
|
67 |
@Test
|
|
68 |
void testAPSingleModule(Path base) throws Exception {
|
|
69 |
Path moduleSrc = base.resolve("module-src");
|
|
70 |
Path m1 = moduleSrc.resolve("m1");
|
|
71 |
|
|
72 |
Path classes = base.resolve("classes");
|
|
73 |
|
|
74 |
Files.createDirectories(classes);
|
|
75 |
|
|
76 |
tb.writeJavaFiles(m1,
|
|
77 |
"module m1 { }",
|
|
78 |
"package impl; public class Impl { }");
|
|
79 |
|
|
80 |
String log = tb.new JavacTask()
|
|
81 |
.options("-modulesourcepath", moduleSrc.toString(),
|
|
82 |
"-processor", AP.class.getName(),
|
|
83 |
"-AexpectedEnclosedElements=m1=>impl")
|
|
84 |
.outdir(classes)
|
|
85 |
.files(findJavaFiles(moduleSrc))
|
|
86 |
.run()
|
|
87 |
.writeAll()
|
|
88 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
89 |
|
|
90 |
if (!log.isEmpty())
|
|
91 |
throw new AssertionError("Unexpected output: " + log);
|
|
92 |
}
|
|
93 |
|
|
94 |
@Test
|
|
95 |
void testAPMultiModule(Path base) throws Exception {
|
|
96 |
Path moduleSrc = base.resolve("module-src");
|
|
97 |
Path m1 = moduleSrc.resolve("m1");
|
|
98 |
Path m2 = moduleSrc.resolve("m2");
|
|
99 |
|
|
100 |
Path classes = base.resolve("classes");
|
|
101 |
|
|
102 |
Files.createDirectories(classes);
|
|
103 |
|
|
104 |
tb.writeJavaFiles(m1,
|
|
105 |
"module m1 { }",
|
|
106 |
"package impl1; public class Impl1 { }");
|
|
107 |
|
|
108 |
tb.writeJavaFiles(m2,
|
|
109 |
"module m2 { }",
|
|
110 |
"package impl2; public class Impl2 { }");
|
|
111 |
|
|
112 |
String log = tb.new JavacTask()
|
|
113 |
.options("-modulesourcepath", moduleSrc.toString(),
|
|
114 |
"-processor", AP.class.getName(),
|
|
115 |
"-AexpectedEnclosedElements=m1=>impl1,m2=>impl2")
|
|
116 |
.outdir(classes)
|
|
117 |
.files(findJavaFiles(moduleSrc))
|
|
118 |
.run()
|
|
119 |
.writeAll()
|
|
120 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
121 |
|
|
122 |
if (!log.isEmpty())
|
|
123 |
throw new AssertionError("Unexpected output: " + log);
|
|
124 |
}
|
|
125 |
|
|
126 |
@SupportedAnnotationTypes("*")
|
|
127 |
@SupportedOptions("expectedEnclosedElements")
|
|
128 |
public static final class AP extends AbstractProcessor {
|
|
129 |
|
|
130 |
private Map<String, List<String>> module2ExpectedEnclosedElements;
|
|
131 |
|
|
132 |
@Override
|
|
133 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
|
134 |
if (module2ExpectedEnclosedElements == null) {
|
|
135 |
module2ExpectedEnclosedElements = new HashMap<>();
|
|
136 |
|
|
137 |
String expectedEnclosedElements =
|
|
138 |
processingEnv.getOptions().get("expectedEnclosedElements");
|
|
139 |
|
|
140 |
for (String moduleDef : expectedEnclosedElements.split(",")) {
|
|
141 |
String[] module2Packages = moduleDef.split("=>");
|
|
142 |
|
|
143 |
module2ExpectedEnclosedElements.put(module2Packages[0],
|
|
144 |
Arrays.asList(module2Packages[1].split(":")));
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
//verify ModuleType and ModuleSymbol behavior:
|
|
149 |
for (Element root : roundEnv.getRootElements()) {
|
|
150 |
ModuleElement module = processingEnv.getElementUtils().getModuleOf(root);
|
|
151 |
|
|
152 |
assertEquals(TypeKind.MODULE, module.asType().getKind());
|
|
153 |
|
|
154 |
boolean[] seenModule = new boolean[1];
|
|
155 |
|
|
156 |
module.accept(new ElementScanner9<Void, Void>() {
|
|
157 |
@Override
|
|
158 |
public Void visitModule(ModuleElement e, Void p) {
|
|
159 |
seenModule[0] = true;
|
|
160 |
return null;
|
|
161 |
}
|
|
162 |
@Override
|
|
163 |
public Void scan(Element e, Void p) {
|
|
164 |
throw new AssertionError("Shouldn't get here.");
|
|
165 |
}
|
|
166 |
}, null);
|
|
167 |
|
|
168 |
assertEquals(true, seenModule[0]);
|
|
169 |
|
|
170 |
List<String> actualElements =
|
|
171 |
module.getEnclosedElements()
|
|
172 |
.stream()
|
|
173 |
.map(s -> (PackageElement) s)
|
|
174 |
.map(p -> p.getQualifiedName().toString())
|
|
175 |
.collect(Collectors.toList());
|
|
176 |
|
|
177 |
assertEquals(module2ExpectedEnclosedElements.remove(module.getQualifiedName().toString()),
|
|
178 |
actualElements);
|
|
179 |
}
|
|
180 |
|
|
181 |
if (roundEnv.processingOver()) {
|
|
182 |
assertEquals(true, module2ExpectedEnclosedElements.isEmpty());
|
|
183 |
}
|
|
184 |
|
|
185 |
return false;
|
|
186 |
}
|
|
187 |
|
|
188 |
@Override
|
|
189 |
public SourceVersion getSupportedSourceVersion() {
|
|
190 |
return SourceVersion.latest();
|
|
191 |
}
|
|
192 |
|
|
193 |
}
|
|
194 |
|
|
195 |
@Test
|
|
196 |
void testVerifyUsesProvides(Path base) throws Exception {
|
|
197 |
Path moduleSrc = base.resolve("module-src");
|
|
198 |
Path m1 = moduleSrc.resolve("m1");
|
|
199 |
|
|
200 |
Path classes = base.resolve("classes");
|
|
201 |
|
|
202 |
Files.createDirectories(classes);
|
|
203 |
|
|
204 |
tb.writeJavaFiles(m1,
|
|
205 |
"module m1 { exports api; uses api.Api; provides api.Api with impl.Impl; }",
|
|
206 |
"package api; public class Api { }",
|
|
207 |
"package impl; public class Impl extends api.Api { }");
|
|
208 |
|
|
209 |
String log = tb.new JavacTask()
|
|
210 |
.options("-doe", "-processor", VerifyUsesProvidesAP.class.getName())
|
|
211 |
.outdir(classes)
|
|
212 |
.files(findJavaFiles(moduleSrc))
|
|
213 |
.run()
|
|
214 |
.writeAll()
|
|
215 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
216 |
|
|
217 |
if (!log.isEmpty())
|
|
218 |
throw new AssertionError("Unexpected output: " + log);
|
|
219 |
}
|
|
220 |
|
|
221 |
@SupportedAnnotationTypes("*")
|
|
222 |
public static final class VerifyUsesProvidesAP extends AbstractProcessor {
|
|
223 |
|
|
224 |
@Override
|
|
225 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
|
226 |
TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
|
|
227 |
|
|
228 |
assertNonNull("Cannot find api.Api", api);
|
|
229 |
|
|
230 |
ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
|
|
231 |
|
|
232 |
assertNonNull("modle is null", modle);
|
|
233 |
|
|
234 |
List<? extends UsesDirective> uses = ElementFilter.usesIn(modle.getDirectives());
|
|
235 |
assertEquals(1, uses.size());
|
|
236 |
assertEquals("api.Api", uses.iterator().next().getService().getQualifiedName().toString());
|
|
237 |
|
|
238 |
List<? extends ProvidesDirective> provides = ElementFilter.providesIn(modle.getDirectives());
|
|
239 |
assertEquals(1, provides.size());
|
|
240 |
assertEquals("api.Api", provides.iterator().next().getService().getQualifiedName().toString());
|
|
241 |
assertEquals("impl.Impl", provides.iterator().next().getImplementation().getQualifiedName().toString());
|
|
242 |
|
|
243 |
return false;
|
|
244 |
}
|
|
245 |
|
|
246 |
@Override
|
|
247 |
public SourceVersion getSupportedSourceVersion() {
|
|
248 |
return SourceVersion.latest();
|
|
249 |
}
|
|
250 |
|
|
251 |
}
|
|
252 |
|
|
253 |
@Test
|
|
254 |
void testPackageNoModule(Path base) throws Exception {
|
|
255 |
Path src = base.resolve("src");
|
|
256 |
Path classes = base.resolve("classes");
|
|
257 |
|
|
258 |
Files.createDirectories(classes);
|
|
259 |
|
|
260 |
tb.writeJavaFiles(src,
|
|
261 |
"package api; public class Api { }");
|
|
262 |
|
|
263 |
String log = tb.new JavacTask()
|
|
264 |
.options("-processor", VerifyPackageNoModule.class.getName(),
|
|
265 |
"-source", "8",
|
|
266 |
"-Xlint:-options")
|
|
267 |
.outdir(classes)
|
|
268 |
.files(findJavaFiles(src))
|
|
269 |
.run()
|
|
270 |
.writeAll()
|
|
271 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
272 |
|
|
273 |
if (!log.isEmpty())
|
|
274 |
throw new AssertionError("Unexpected output: " + log);
|
|
275 |
}
|
|
276 |
|
|
277 |
@SupportedAnnotationTypes("*")
|
|
278 |
public static final class VerifyPackageNoModule extends AbstractProcessor {
|
|
279 |
|
|
280 |
@Override
|
|
281 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
|
282 |
TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
|
|
283 |
|
|
284 |
assertNonNull("Cannot find api.Api", api);
|
|
285 |
|
|
286 |
ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
|
|
287 |
|
|
288 |
assertNull("modle is not null", modle);
|
|
289 |
|
|
290 |
return false;
|
|
291 |
}
|
|
292 |
|
|
293 |
@Override
|
|
294 |
public SourceVersion getSupportedSourceVersion() {
|
|
295 |
return SourceVersion.latest();
|
|
296 |
}
|
|
297 |
|
|
298 |
}
|
|
299 |
|
|
300 |
private static void assertNonNull(String msg, Object val) {
|
|
301 |
if (val == null) {
|
|
302 |
throw new AssertionError(msg);
|
|
303 |
}
|
|
304 |
}
|
|
305 |
|
|
306 |
private static void assertNull(String msg, Object val) {
|
|
307 |
if (val != null) {
|
|
308 |
throw new AssertionError(msg);
|
|
309 |
}
|
|
310 |
}
|
|
311 |
|
|
312 |
private static void assertEquals(Object expected, Object actual) {
|
|
313 |
if (!Objects.equals(expected, actual)) {
|
|
314 |
throw new AssertionError("expected: " + expected + "; actual=" + actual);
|
|
315 |
}
|
|
316 |
}
|
|
317 |
|
|
318 |
}
|