ModuleInfoTreeAccess.java revision 3792:d516975e8110
1219019Sgabor/* 2219019Sgabor * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3219019Sgabor * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4219019Sgabor * 5219019Sgabor * This code is free software; you can redistribute it and/or modify it 6219019Sgabor * under the terms of the GNU General Public License version 2 only, as 7219019Sgabor * published by the Free Software Foundation. 8219019Sgabor * 9219019Sgabor * This code is distributed in the hope that it will be useful, but WITHOUT 10219019Sgabor * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11219019Sgabor * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12219019Sgabor * version 2 for more details (a copy is included in the LICENSE file that 13219019Sgabor * accompanied this code). 14219019Sgabor * 15219019Sgabor * You should have received a copy of the GNU General Public License version 16219019Sgabor * 2 along with this work; if not, write to the Free Software Foundation, 17219019Sgabor * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18219019Sgabor * 19219019Sgabor * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20219019Sgabor * or visit www.oracle.com if you need additional information or have any 21219019Sgabor * questions. 22219019Sgabor */ 23219019Sgabor 24219019Sgabor/* 25219019Sgabor * @test 8152771 26219019Sgabor * @summary test tree access of module declarations 27219019Sgabor * @library /tools/lib 28219019Sgabor * @modules 29219019Sgabor * jdk.compiler/com.sun.tools.javac.api 30219019Sgabor * jdk.compiler/com.sun.tools.javac.main 31219019Sgabor * jdk.jdeps/com.sun.tools.javap 32219019Sgabor * @build toolbox.ToolBox ModuleTestBase 33219019Sgabor * @run main ModuleInfoTreeAccess 34219019Sgabor */ 35219019Sgabor 36219019Sgaborimport java.nio.file.Path; 37219019Sgabor 38219019Sgaborimport javax.lang.model.element.ModuleElement; 39219019Sgaborimport javax.tools.JavaCompiler; 40219019Sgaborimport javax.tools.JavaFileObject; 41219019Sgaborimport javax.tools.StandardJavaFileManager; 42219019Sgaborimport javax.tools.ToolProvider; 43219019Sgabor 44219019Sgaborimport com.sun.source.doctree.DocCommentTree; 45219019Sgaborimport com.sun.source.util.JavacTask; 46219019Sgaborimport com.sun.source.util.TreePath; 47219019Sgaborimport com.sun.tools.javac.api.JavacTrees; 48219019Sgabor 49219019Sgaborimport toolbox.ToolBox; 50219019Sgabor 51219019Sgaborpublic class ModuleInfoTreeAccess extends ModuleTestBase { 52219019Sgabor public static void main(String... args) throws Exception { 53219019Sgabor ModuleInfoTreeAccess t = new ModuleInfoTreeAccess(); 54219019Sgabor t.runTests(); 55219019Sgabor } 56219019Sgabor 57219019Sgabor private void assertNotNull(String prefix, Object actual) { 58219019Sgabor if (actual == null) { 59219019Sgabor throw new AssertionError(prefix + ": unexpected null! "); 60219019Sgabor } 61219019Sgabor } 62219019Sgabor 63219019Sgabor @Test 64219019Sgabor public void testTreePathForModuleDecl(Path base) throws Exception { 65219019Sgabor 66219019Sgabor JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 67219019Sgabor try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 68219019Sgabor Path src = base.resolve("src"); 69219019Sgabor tb.writeJavaFiles(src, "/** Test module */ module m1 {}"); 70219019Sgabor 71219019Sgabor Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src)); 72219019Sgabor JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files); 73219019Sgabor 74219019Sgabor task.analyze(); 75219019Sgabor JavacTrees trees = JavacTrees.instance(task); 76219019Sgabor ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1"); 77219019Sgabor 78219019Sgabor TreePath path = trees.getPath(mdle); 79219019Sgabor assertNotNull("path", path); 80219019Sgabor 81219019Sgabor ModuleElement mdle1 = (ModuleElement) trees.getElement(path); 82219019Sgabor assertNotNull("mdle1", mdle1); 83219019Sgabor 84219019Sgabor DocCommentTree docCommentTree = trees.getDocCommentTree(mdle); 85219019Sgabor assertNotNull("docCommentTree", docCommentTree); 86219019Sgabor } 87219019Sgabor } 88219019Sgabor 89219019Sgabor @Test 90219019Sgabor public void testTreePathForModuleDeclWithImport(Path base) throws Exception { 91219019Sgabor JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 92219019Sgabor try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 93219019Sgabor Path src = base.resolve("src"); 94219019Sgabor tb.writeJavaFiles(src, "import java.lang.Deprecated; /** Test module */ @Deprecated module m1 {}"); 95219019Sgabor 96219019Sgabor Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src)); 97219019Sgabor JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files); 98219019Sgabor 99219019Sgabor task.analyze(); 100219019Sgabor JavacTrees trees = JavacTrees.instance(task); 101219019Sgabor ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1"); 102219019Sgabor 103219019Sgabor TreePath path = trees.getPath(mdle); 104219019Sgabor assertNotNull("path", path); 105219019Sgabor 106219019Sgabor ModuleElement mdle1 = (ModuleElement) trees.getElement(path); 107219019Sgabor assertNotNull("mdle1", mdle1); 108219019Sgabor 109219019Sgabor DocCommentTree docCommentTree = trees.getDocCommentTree(mdle); 110219019Sgabor assertNotNull("docCommentTree", docCommentTree); 111219019Sgabor } 112219019Sgabor } 113219019Sgabor} 114219019Sgabor