langtools/test/jdk/jshell/JavadocTest.java
author jlahoda
Wed, 02 Nov 2016 07:38:37 +0100
changeset 41865 3ef02797070d
child 42407 f3702cff2933
child 42258 a1aafd5ea6ec
permissions -rw-r--r--
8131019: jshell tool: access javadoc from tool Summary: Adding internal support to resolve {@inheritDoc} and format javadoc to plain text for use by jdk.jshell and jdk.scripting.nashorn.shell, enhancing Shift-<tab> documentation in JShell with ability to show javadoc. Reviewed-by: jjg, rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     1
/*
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     4
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     8
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    14
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    18
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    21
 * questions.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    22
 */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    23
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    24
/*
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    25
 * @test
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    26
 * @bug 8131019
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    27
 * @summary Test Javadoc
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    28
 * @library /tools/lib
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.main
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    31
 *          jdk.jshell
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    32
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    33
 * @build KullaTesting TestingInputStream Compiler
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    34
 * @run testng JavadocTest
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    35
 */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    36
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    37
import java.io.IOException;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    38
import java.lang.reflect.Field;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    39
import java.nio.file.Files;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    40
import java.nio.file.Path;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    41
import java.nio.file.Paths;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    42
import java.util.Arrays;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    43
import java.util.jar.JarEntry;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    44
import java.util.jar.JarOutputStream;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    45
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    46
import org.testng.annotations.Test;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    47
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    48
@Test
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    49
public class JavadocTest extends KullaTesting {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    50
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    51
    private final Compiler compiler = new Compiler();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    52
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    53
    public void testJavadoc() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    54
        prepareZip();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    55
        assertJavadoc("test.Clazz|", "test.Clazz\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    56
                                     "Top level. ");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    57
        assertEval("test.Clazz clz = null;");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    58
        assertJavadoc("clz.test(|", "String test.Clazz.test(int p) throws IllegalStateException\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    59
                                    " javadoc1A\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    60
                                    "\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    61
                                    " @param p param\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    62
                                    " @throws IllegalStateException exc\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    63
                                    " @return value\n");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    64
        //undefined handling:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    65
        assertJavadoc("clz.undef|");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    66
    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    67
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    68
    private void prepareZip() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    69
        String clazz =
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    70
                "package test;\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    71
                "/**Top level." +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    72
                " */\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    73
                "public class Clazz {\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    74
                "    /**\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    75
                "     * javadoc1A\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    76
                "     *\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    77
                "     * @param p param\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    78
                "     * @throws IllegalStateException exc\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    79
                "     * @return value\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    80
                "     */\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    81
                "    public String test(int p) throws IllegalStateException { return null;}\n" +
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    82
                "}\n";
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    83
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    84
        Path srcZip = Paths.get("src.zip");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    85
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    86
        try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    87
            out.putNextEntry(new JarEntry("test/Clazz.java"));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    88
            out.write(clazz.getBytes());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    89
        } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    90
            throw new IllegalStateException(ex);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    91
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    92
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    93
        compiler.compile(clazz);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    94
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    95
        try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    96
            Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    97
            availableSources.setAccessible(true);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    98
            availableSources.set(getAnalysis(), Arrays.asList(srcZip));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    99
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   100
            throw new IllegalStateException(ex);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   101
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   102
        addToClasspath(compiler.getClassDir());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   103
    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   104
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   105
}