langtools/test/tools/javac/modules/ModuleInfoTreeAccess.java
author jjg
Mon, 02 May 2016 16:17:39 -0700
changeset 37758 3ecf9b414e05
parent 36779 9a030c4d2591
child 42407 f3702cff2933
permissions -rw-r--r--
8155774: move code from ModuleTestBase to toolbox Reviewed-by: ksrini, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36779
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     1
/*
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     4
 *
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     8
 *
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    13
 * accompanied this code).
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    14
 *
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    18
 *
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    21
 * questions.
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    22
 */
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    23
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    24
/*
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    25
 * @test 8152771
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    26
 * @summary test tree access of module declarations
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    27
 * @library /tools/lib
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    28
 * @modules
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    29
 *      jdk.compiler/com.sun.tools.javac.api
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    30
 *      jdk.compiler/com.sun.tools.javac.main
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    31
 *      jdk.jdeps/com.sun.tools.javap
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    32
 * @build toolbox.ToolBox ModuleTestBase
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    33
 * @run main ModuleInfoTreeAccess
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    34
 */
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    35
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    36
import java.nio.file.Path;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    37
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    38
import javax.lang.model.element.ModuleElement;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    39
import javax.tools.JavaCompiler;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    41
import javax.tools.StandardJavaFileManager;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    42
import javax.tools.ToolProvider;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    43
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    44
import com.sun.source.doctree.DocCommentTree;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    45
import com.sun.source.util.JavacTask;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    46
import com.sun.source.util.TreePath;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    47
import com.sun.tools.javac.api.JavacTrees;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    48
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    49
import toolbox.ToolBox;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    50
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    51
public class ModuleInfoTreeAccess extends ModuleTestBase {
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    52
    public static void main(String... args) throws Exception {
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    53
        ModuleInfoTreeAccess t = new ModuleInfoTreeAccess();
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    54
        t.runTests();
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    55
    }
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    56
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    57
    private void assertNotNull(String prefix, Object actual) {
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    58
        if (actual == null) {
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    59
            throw new AssertionError(prefix + ": unexpected null! ");
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    60
        }
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    61
    }
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    62
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    63
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36779
diff changeset
    64
    public void testTreePathForModuleDecl(Path base) throws Exception {
36779
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    65
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    66
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    67
        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    68
            Path src = base.resolve("src");
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    69
            tb.writeJavaFiles(src, "/** Test module */ module m1 {}");
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    70
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    71
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src));
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    72
            JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    73
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    74
            task.analyze();
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    75
            JavacTrees trees = JavacTrees.instance(task);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    76
            ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1");
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    77
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    78
            TreePath path = trees.getPath(mdle);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    79
            assertNotNull("path", path);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    80
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    81
            ModuleElement mdle1 = (ModuleElement) trees.getElement(path);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    82
            assertNotNull("mdle1", mdle1);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    83
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    84
            DocCommentTree docCommentTree = trees.getDocCommentTree(mdle);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    85
            assertNotNull("docCommentTree", docCommentTree);
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    86
        }
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    87
    }
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents:
diff changeset
    88
}