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
36import java.nio.file.Path;
37
38import javax.lang.model.element.ModuleElement;
39import javax.tools.JavaCompiler;
40import javax.tools.JavaFileObject;
41import javax.tools.StandardJavaFileManager;
42import javax.tools.ToolProvider;
43
44import com.sun.source.doctree.DocCommentTree;
45import com.sun.source.util.JavacTask;
46import com.sun.source.util.TreePath;
47import com.sun.tools.javac.api.JavacTrees;
48
49
50public 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
63    public void testTreePathForModuleDecl(Path base) throws Exception {
64
65        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
66        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
67            Path src = base.resolve("src");
68            tb.writeJavaFiles(src, "/** Test module */ module m1x {}");
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);
75            ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1x");
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    }
87
88    @Test
89    public void testTreePathForModuleDeclWithImport(Path base) throws Exception {
90        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
91        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
92            Path src = base.resolve("src");
93            tb.writeJavaFiles(src, "import java.lang.Deprecated; /** Test module */ @Deprecated module m1x {}");
94
95            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src));
96            JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files);
97
98            task.analyze();
99            JavacTrees trees = JavacTrees.instance(task);
100            ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1x");
101
102            TreePath path = trees.getPath(mdle);
103            assertNotNull("path", path);
104
105            ModuleElement mdle1 = (ModuleElement) trees.getElement(path);
106            assertNotNull("mdle1", mdle1);
107
108            DocCommentTree docCommentTree = trees.getDocCommentTree(mdle);
109            assertNotNull("docCommentTree", docCommentTree);
110        }
111    }
112}
113