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