ModuleInfoTreeAccess.java revision 3315:8b64ecd96dbe
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 49import toolbox.ToolBox; 50 51public class ModuleInfoTreeAccess extends ModuleTestBase { 52 public static void main(String... args) throws Exception { 53 ModuleInfoTreeAccess t = new ModuleInfoTreeAccess(); 54 t.runTests(); 55 } 56 57 private void assertNotNull(String prefix, Object actual) { 58 if (actual == null) { 59 throw new AssertionError(prefix + ": unexpected null! "); 60 } 61 } 62 63 @Test 64 void testTreePathForModuleDecl(Path base) throws Exception { 65 66 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 67 try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 68 Path src = base.resolve("src"); 69 tb.writeJavaFiles(src, "/** Test module */ module m1 {}"); 70 71 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src)); 72 JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files); 73 74 task.analyze(); 75 JavacTrees trees = JavacTrees.instance(task); 76 ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1"); 77 78 TreePath path = trees.getPath(mdle); 79 assertNotNull("path", path); 80 81 ModuleElement mdle1 = (ModuleElement) trees.getElement(path); 82 assertNotNull("mdle1", mdle1); 83 84 DocCommentTree docCommentTree = trees.getDocCommentTree(mdle); 85 assertNotNull("docCommentTree", docCommentTree); 86 } 87 } 88} 89