author | darcy |
Fri, 02 Feb 2018 10:29:25 -0800 | |
changeset 48723 | 6cb86bf0b51e |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
36779 | 1 |
/* |
2 |
* Copyright (c) 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 8152771 |
|
26 |
* @summary test tree access of module declarations |
|
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.ToolBox ModuleTestBase |
|
33 |
* @run main ModuleInfoTreeAccess |
|
34 |
*/ |
|
35 |
||
36 |
import java.nio.file.Path; |
|
37 |
||
38 |
import javax.lang.model.element.ModuleElement; |
|
39 |
import javax.tools.JavaCompiler; |
|
40 |
import javax.tools.JavaFileObject; |
|
41 |
import javax.tools.StandardJavaFileManager; |
|
42 |
import javax.tools.ToolProvider; |
|
43 |
||
44 |
import com.sun.source.doctree.DocCommentTree; |
|
45 |
import com.sun.source.util.JavacTask; |
|
46 |
import com.sun.source.util.TreePath; |
|
47 |
import com.sun.tools.javac.api.JavacTrees; |
|
48 |
||
49 |
||
50 |
public class ModuleInfoTreeAccess extends ModuleTestBase { |
|
51 |
public static void main(String... args) throws Exception { |
|
52 |
ModuleInfoTreeAccess t = new ModuleInfoTreeAccess(); |
|
53 |
t.runTests(); |
|
54 |
} |
|
55 |
||
56 |
private void assertNotNull(String prefix, Object actual) { |
|
57 |
if (actual == null) { |
|
58 |
throw new AssertionError(prefix + ": unexpected null! "); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
@Test |
|
37758 | 63 |
public void testTreePathForModuleDecl(Path base) throws Exception { |
36779 | 64 |
|
65 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
66 |
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { |
|
67 |
Path src = base.resolve("src"); |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
68 |
tb.writeJavaFiles(src, "/** Test module */ module m1x {}"); |
36779 | 69 |
|
70 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src)); |
|
71 |
JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files); |
|
72 |
||
73 |
task.analyze(); |
|
74 |
JavacTrees trees = JavacTrees.instance(task); |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
75 |
ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1x"); |
36779 | 76 |
|
77 |
TreePath path = trees.getPath(mdle); |
|
78 |
assertNotNull("path", path); |
|
79 |
||
80 |
ModuleElement mdle1 = (ModuleElement) trees.getElement(path); |
|
81 |
assertNotNull("mdle1", mdle1); |
|
82 |
||
83 |
DocCommentTree docCommentTree = trees.getDocCommentTree(mdle); |
|
84 |
assertNotNull("docCommentTree", docCommentTree); |
|
85 |
} |
|
86 |
} |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
87 |
|
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
88 |
@Test |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
89 |
public void testTreePathForModuleDeclWithImport(Path base) throws Exception { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
90 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
91 |
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
92 |
Path src = base.resolve("src"); |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
93 |
tb.writeJavaFiles(src, "import java.lang.Deprecated; /** Test module */ @Deprecated module m1x {}"); |
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
94 |
|
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
95 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src)); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
96 |
JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
97 |
|
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
98 |
task.analyze(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
99 |
JavacTrees trees = JavacTrees.instance(task); |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
100 |
ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1x"); |
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
101 |
|
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
102 |
TreePath path = trees.getPath(mdle); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
103 |
assertNotNull("path", path); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
104 |
|
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
105 |
ModuleElement mdle1 = (ModuleElement) trees.getElement(path); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
106 |
assertNotNull("mdle1", mdle1); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
107 |
|
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
108 |
DocCommentTree docCommentTree = trees.getDocCommentTree(mdle); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
109 |
assertNotNull("docCommentTree", docCommentTree); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
110 |
} |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37758
diff
changeset
|
111 |
} |
36779 | 112 |
} |