1/*
2 * Copyright (c) 2015, 2017, 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
26 * @bug 8135307
27 * @summary Check that CompletionFailures for missing classes are not incorrectly passed to
28 *          the javadoc API clients.
29 * @library /tools/lib
30 * @modules jdk.compiler/com.sun.tools.javac.api
31 *          jdk.compiler/com.sun.tools.javac.main
32 *          jdk.jdeps/com.sun.tools.javap
33 * @build toolbox.JavacTask toolbox.ToolBox
34 * @run main CompletionError
35 */
36
37import java.io.File;
38
39import com.sun.javadoc.*;
40import com.sun.tools.javadoc.Main;
41
42import toolbox.JavacTask;
43import toolbox.ToolBox;
44
45public class CompletionError extends Doclet
46{
47    private static final String template =
48            "public class CompletionErrorAuxiliary #extends CompletionErrorMissing# #implements CompletionErrorIntfMissing# {" +
49            "   #public CompletionErrorMissing tf;#" +
50            "   #public CompletionErrorMissing tm() { return null; }#" +
51            "   #public void tm(CompletionErrorMissing m) {}#" +
52            "   #public void tm() throws CompletionErrorExcMissing {}#" +
53            "   #public <T extends CompletionErrorMissing> void tm() {}#" +
54            "   public String toString() { return null; }" +
55            "}";
56
57    private static final String testSrc = System.getProperty("test.src");
58    private static final String testClassPath = System.getProperty("test.class.path");
59
60    public static void main(String[] args) throws Exception {
61        String[] templateParts = template.split("#");
62        int sources = templateParts.length / 2;
63        for (int source = 0; source < sources; source++) {
64            StringBuilder testSource = new StringBuilder();
65            for (int i = 0; i < templateParts.length; i += 2) {
66                testSource.append(templateParts[i]);
67                if (i == 2 * source) {
68                    testSource.append(templateParts[i + 1]);
69                }
70            }
71            test = 0;
72            testsDone = false;
73            while (!testsDone) {
74                ToolBox tb = new ToolBox();
75                new JavacTask(tb)
76                  .sources(testSource.toString(),
77                           "public class CompletionErrorMissing {}",
78                           "public interface CompletionErrorIntfMissing {}",
79                           "public class CompletionErrorExcMissing extends Exception {}")
80                  .outdir(".")
81                  .run()
82                  .writeAll();
83                tb.deleteFiles("CompletionErrorMissing.class", "CompletionErrorIntfMissing.class", "CompletionErrorExcMissing.class");
84                // run javadoc:
85                if (Main.execute("javadoc", "CompletionError", CompletionError.class.getClassLoader(),
86                                 "-classpath", "." + File.pathSeparator + testClassPath,
87                                 new File(testSrc, "CompletionError.java").getPath()) != 0)
88                    throw new Error();
89            }
90        }
91    }
92
93    private static int test;
94    private static boolean testsDone;
95
96    public static boolean start(com.sun.javadoc.RootDoc root) {
97        ClassDoc aux = root.classNamed("CompletionErrorAuxiliary");
98        if (aux == null)
99            throw new AssertionError("Cannot find CompletionErrorAuxiliary");
100
101        FieldDoc tf = findField(aux, "tf");
102        MethodDoc tm = findMethod(aux, "tm");
103        MethodDoc cm = findMethod(aux, "toString");
104        switch (test) {
105            case 0: aux.superclass(); break;
106            case 1: aux.superclassType(); break;
107            case 2: aux.interfaces(); break;
108            case 3: aux.interfaceTypes(); break;
109            case 4: if (tf != null) tf.type(); break;
110            case 5: if (tm != null) tm.overriddenClass(); break;
111            case 6: if (tm != null) tm.overriddenMethod(); break;
112            case 7: if (tm != null) tm.overriddenType(); break;
113            case 8:
114                if (tm != null) {
115                    for (Parameter p : tm.parameters()) {
116                        p.type();
117                    }
118                }
119                break;
120            case 9: if (tm != null) tm.receiverType(); break;
121            case 10: if (tm != null) tm.returnType(); break;
122            case 11: if (tm != null) tm.thrownExceptionTypes(); break;
123            case 12: if (tm != null) tm.thrownExceptions(); break;
124            case 13:
125                if (tm != null) {
126                    for (TypeVariable tv : tm.typeParameters()) {
127                        tv.bounds();
128                    }
129                }
130                break;
131            case 14: if (cm != null) cm.overriddenClass(); break;
132            case 15: if (cm != null) cm.overriddenMethod(); break;
133            case 16: if (cm != null) cm.overriddenType(); testsDone = true; break;
134            default:
135                throw new IllegalStateException("Unrecognized test!");
136        }
137        test++;
138        return true;
139    }
140
141    private static MethodDoc findMethod(ClassDoc cd, String name) {
142        for (MethodDoc m : cd.methods()) {
143            if (name.equals(m.name()))
144                return m;
145        }
146
147        return null;
148    }
149
150    private static FieldDoc findField(ClassDoc cd, String name) {
151        for (FieldDoc m : cd.fields()) {
152            if (name.equals(m.name()))
153                return m;
154        }
155
156        return null;
157    }
158}
159