|
1 /* |
|
2 * Copyright (c) 2019, 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 * @bug 8234025 |
|
27 * @summary Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package |
|
28 * @modules jdk.compiler |
|
29 * @library /tools/lib /tools/javac/lib |
|
30 * @build toolbox.ModuleBuilder toolbox.ToolBox |
|
31 * @run main TestModuleUnnamedPackage |
|
32 */ |
|
33 |
|
34 import java.io.IOException; |
|
35 import java.nio.file.Path; |
|
36 import java.util.List; |
|
37 import java.util.Set; |
|
38 |
|
39 import javax.annotation.processing.RoundEnvironment; |
|
40 import javax.lang.model.element.ModuleElement; |
|
41 import javax.lang.model.element.PackageElement; |
|
42 import javax.lang.model.element.TypeElement; |
|
43 import javax.lang.model.util.Elements; |
|
44 import javax.tools.JavaCompiler; |
|
45 import javax.tools.JavaFileObject; |
|
46 import javax.tools.StandardJavaFileManager; |
|
47 import javax.tools.StandardLocation; |
|
48 import javax.tools.ToolProvider; |
|
49 |
|
50 import com.sun.source.util.JavacTask; |
|
51 import toolbox.Assert; |
|
52 import toolbox.ToolBox; |
|
53 |
|
54 public class TestModuleUnnamedPackage extends JavacTestingAbstractProcessor { |
|
55 public static void main(String... args) throws IOException { |
|
56 TestModuleUnnamedPackage t = new TestModuleUnnamedPackage(); |
|
57 t.run(); |
|
58 } |
|
59 |
|
60 void run() throws IOException { |
|
61 ToolBox tb = new ToolBox(); |
|
62 Path src = Path.of("src"); |
|
63 |
|
64 tb.writeJavaFiles(src, "module m { exports p; }", |
|
65 "package p; public class C { }"); |
|
66 |
|
67 JavaCompiler c = ToolProvider.getSystemJavaCompiler(); |
|
68 StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); |
|
69 fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, List.of(src)); |
|
70 List<String> options = List.of("-proc:only"); |
|
71 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(tb.findJavaFiles(src)); |
|
72 JavacTask t = (JavacTask) c.getTask(null, fm, null, null, null, files); |
|
73 t.setProcessors(List.of(this)); |
|
74 t.call(); |
|
75 } |
|
76 |
|
77 public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) { |
|
78 if (rEnv.processingOver()) { |
|
79 Elements elements = processingEnv.getElementUtils(); |
|
80 ModuleElement m = elements.getModuleElement("m"); |
|
81 PackageElement unnamed = elements.getPackageElement(m, ""); |
|
82 System.out.println("module m: " + m); |
|
83 System.out.println("module m, unnamed package: " + unnamed); |
|
84 Assert.checkNonNull(unnamed, "unnamed package in module m"); |
|
85 } |
|
86 return true; |
|
87 } |
|
88 } |
|
89 |