jlahoda@2906: /* jlahoda@2906: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. jlahoda@2906: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jlahoda@2906: * jlahoda@2906: * This code is free software; you can redistribute it and/or modify it jlahoda@2906: * under the terms of the GNU General Public License version 2 only, as jlahoda@2906: * published by the Free Software Foundation. jlahoda@2906: * jlahoda@2906: * This code is distributed in the hope that it will be useful, but WITHOUT jlahoda@2906: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jlahoda@2906: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jlahoda@2906: * version 2 for more details (a copy is included in the LICENSE file that jlahoda@2906: * accompanied this code). jlahoda@2906: * jlahoda@2906: * You should have received a copy of the GNU General Public License version jlahoda@2906: * 2 along with this work; if not, write to the Free Software Foundation, jlahoda@2906: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jlahoda@2906: * jlahoda@2906: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jlahoda@2906: * or visit www.oracle.com if you need additional information or have any jlahoda@2906: * questions. jlahoda@2906: */ jlahoda@2906: jlahoda@2906: /* jlahoda@2906: * @test jlahoda@2906: * @bug 8135307 jlahoda@2906: * @summary Check that CompletionFailures for missing classes are not incorrectly passed to jlahoda@2906: * the javadoc API clients. jlahoda@2906: * @modules jdk.javadoc jlahoda@2906: * @run main CompletionError jlahoda@2906: */ jlahoda@2906: jlahoda@2906: import java.io.File; jlahoda@2906: import java.net.URI; jlahoda@2906: import java.nio.file.Files; jlahoda@2906: import java.nio.file.Paths; jlahoda@2906: import java.util.Arrays; jlahoda@2906: import java.util.List; jlahoda@2906: jlahoda@2906: import javax.tools.JavaCompiler; jlahoda@2906: import javax.tools.JavaFileObject; jlahoda@2906: import javax.tools.SimpleJavaFileObject; jlahoda@2906: import javax.tools.ToolProvider; jlahoda@2906: jlahoda@2906: import com.sun.javadoc.*; jlahoda@2906: import com.sun.tools.javadoc.Main; jlahoda@2906: jlahoda@2906: public class CompletionError extends Doclet jlahoda@2906: { jlahoda@2906: private static final String template = jlahoda@2906: "public class CompletionErrorAuxiliary #extends CompletionErrorMissing# #implements CompletionErrorIntfMissing# {" + jlahoda@2906: " #public CompletionErrorMissing tf;#" + jlahoda@2906: " #public CompletionErrorMissing tm() { return null; }#" + jlahoda@2906: " #public void tm(CompletionErrorMissing m) {}#" + jlahoda@2906: " #public void tm() throws CompletionErrorExcMissing {}#" + jlahoda@2906: " #public void tm() {}#" + jlahoda@2906: " public String toString() { return null; }" + jlahoda@2906: "}"; jlahoda@2906: jlahoda@2906: public static void main(String[] args) throws Exception { jlahoda@2906: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); jlahoda@2906: String[] templateParts = template.split("#"); jlahoda@2906: int sources = templateParts.length / 2; jlahoda@2906: for (int source = 0; source < sources; source++) { jlahoda@2906: StringBuilder testSource = new StringBuilder(); jlahoda@2906: for (int i = 0; i < templateParts.length; i += 2) { jlahoda@2906: testSource.append(templateParts[i]); jlahoda@2906: if (i == 2 * source) { jlahoda@2906: testSource.append(templateParts[i + 1]); jlahoda@2906: } jlahoda@2906: } jlahoda@2906: test = 0; jlahoda@2906: testsDone = false; jlahoda@2906: while (!testsDone) { jlahoda@2906: List fileObjects = jlahoda@2906: Arrays.asList(new JavaSource("CompletionErrorAuxiliary", testSource.toString()), jlahoda@2906: new JavaSource("CompletionErrorMissing", "public class CompletionErrorMissing {}"), jlahoda@2906: new JavaSource("CompletionErrorIntfMissing", "public interface CompletionErrorIntfMissing {}"), jlahoda@2906: new JavaSource("CompletionErrorExcMissing", "public class CompletionErrorExcMissing extends Exception {}")); jlahoda@2906: Boolean result = compiler.getTask(null, null, null, Arrays.asList("-d", "."), null, fileObjects).call(); jlahoda@2906: if (!result) jlahoda@2906: throw new Error(); jlahoda@2906: for (String delete : new String[] {"CompletionErrorMissing.class", "CompletionErrorIntfMissing.class", "CompletionErrorExcMissing.class"}) { jlahoda@2906: Files.delete(Paths.get(delete)); jlahoda@2906: } jlahoda@2906: // run javadoc: jlahoda@2906: if (Main.execute("javadoc", "CompletionError", CompletionError.class.getClassLoader(), jlahoda@2906: "-classpath", ".", jlahoda@2906: System.getProperty("test.src", ".") + File.separatorChar + "CompletionError.java") != 0) jlahoda@2906: throw new Error(); jlahoda@2906: } jlahoda@2906: } jlahoda@2906: } jlahoda@2906: jlahoda@2906: private static int test; jlahoda@2906: private static boolean testsDone; jlahoda@2906: jlahoda@2906: public static boolean start(com.sun.javadoc.RootDoc root) { jlahoda@2906: ClassDoc aux = root.classNamed("CompletionErrorAuxiliary"); jlahoda@2906: if (aux == null) jlahoda@2906: throw new AssertionError("Cannot find CompletionErrorAuxiliary"); jlahoda@2906: jlahoda@2906: FieldDoc tf = findField(aux, "tf"); jlahoda@2906: MethodDoc tm = findMethod(aux, "tm"); jlahoda@2906: MethodDoc cm = findMethod(aux, "toString"); jlahoda@2906: switch (test) { jlahoda@2906: case 0: aux.superclass(); break; jlahoda@2906: case 1: aux.superclassType(); break; jlahoda@2906: case 2: aux.interfaces(); break; jlahoda@2906: case 3: aux.interfaceTypes(); break; jlahoda@2906: case 4: if (tf != null) tf.type(); break; jlahoda@2906: case 5: if (tm != null) tm.overriddenClass(); break; jlahoda@2906: case 6: if (tm != null) tm.overriddenMethod(); break; jlahoda@2906: case 7: if (tm != null) tm.overriddenType(); break; jlahoda@2906: case 8: jlahoda@2906: if (tm != null) { jlahoda@2906: for (Parameter p : tm.parameters()) { jlahoda@2906: p.type(); jlahoda@2906: } jlahoda@2906: } jlahoda@2906: break; jlahoda@2906: case 9: if (tm != null) tm.receiverType(); break; jlahoda@2906: case 10: if (tm != null) tm.returnType(); break; jlahoda@2906: case 11: if (tm != null) tm.thrownExceptionTypes(); break; jlahoda@2906: case 12: if (tm != null) tm.thrownExceptions(); break; jlahoda@2906: case 13: jlahoda@2906: if (tm != null) { jlahoda@2906: for (TypeVariable tv : tm.typeParameters()) { jlahoda@2906: tv.bounds(); jlahoda@2906: } jlahoda@2906: } jlahoda@2906: break; jlahoda@2906: case 14: if (cm != null) cm.overriddenClass(); break; jlahoda@2906: case 15: if (cm != null) cm.overriddenMethod(); break; jlahoda@2906: case 16: if (cm != null) cm.overriddenType(); testsDone = true; break; jlahoda@2906: default: jlahoda@2906: throw new IllegalStateException("Unrecognized test!"); jlahoda@2906: } jlahoda@2906: test++; jlahoda@2906: return true; jlahoda@2906: } jlahoda@2906: jlahoda@2906: private static MethodDoc findMethod(ClassDoc cd, String name) { jlahoda@2906: for (MethodDoc m : cd.methods()) { jlahoda@2906: if (name.equals(m.name())) jlahoda@2906: return m; jlahoda@2906: } jlahoda@2906: jlahoda@2906: return null; jlahoda@2906: } jlahoda@2906: jlahoda@2906: private static FieldDoc findField(ClassDoc cd, String name) { jlahoda@2906: for (FieldDoc m : cd.fields()) { jlahoda@2906: if (name.equals(m.name())) jlahoda@2906: return m; jlahoda@2906: } jlahoda@2906: jlahoda@2906: return null; jlahoda@2906: } jlahoda@2906: jlahoda@2906: static class JavaSource extends SimpleJavaFileObject { jlahoda@2906: final String source; jlahoda@2906: jlahoda@2906: public JavaSource(String name, String source) { jlahoda@2906: super(URI.create("myfo:/" + name + ".java"), JavaFileObject.Kind.SOURCE); jlahoda@2906: this.source = source; jlahoda@2906: } jlahoda@2906: jlahoda@2906: @Override jlahoda@2906: public CharSequence getCharContent(boolean ignoreEncodingErrors) { jlahoda@2906: return source; jlahoda@2906: } jlahoda@2906: } jlahoda@2906: }