Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/CompletionError.java
32285 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 813530726* @summary Check that CompletionFailures for missing classes are not incorrectly passed to27* the javadoc API clients.28* @modules jdk.javadoc29* @run main CompletionError30*/3132import java.io.File;33import java.net.URI;34import java.nio.file.Files;35import java.nio.file.Paths;36import java.util.Arrays;37import java.util.List;3839import javax.tools.JavaCompiler;40import javax.tools.JavaFileObject;41import javax.tools.SimpleJavaFileObject;42import javax.tools.ToolProvider;4344import com.sun.javadoc.*;45import com.sun.tools.javadoc.Main;4647public class CompletionError extends Doclet48{49private static final String template =50"public class CompletionErrorAuxiliary #extends CompletionErrorMissing# #implements CompletionErrorIntfMissing# {" +51" #public CompletionErrorMissing tf;#" +52" #public CompletionErrorMissing tm() { return null; }#" +53" #public void tm(CompletionErrorMissing m) {}#" +54" #public void tm() throws CompletionErrorExcMissing {}#" +55" #public <T extends CompletionErrorMissing> void tm() {}#" +56" public String toString() { return null; }" +57"}";5859public static void main(String[] args) throws Exception {60JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();61String[] templateParts = template.split("#");62int sources = templateParts.length / 2;63for (int source = 0; source < sources; source++) {64StringBuilder testSource = new StringBuilder();65for (int i = 0; i < templateParts.length; i += 2) {66testSource.append(templateParts[i]);67if (i == 2 * source) {68testSource.append(templateParts[i + 1]);69}70}71test = 0;72testsDone = false;73while (!testsDone) {74List<JavaSource> fileObjects =75Arrays.asList(new JavaSource("CompletionErrorAuxiliary", testSource.toString()),76new JavaSource("CompletionErrorMissing", "public class CompletionErrorMissing {}"),77new JavaSource("CompletionErrorIntfMissing", "public interface CompletionErrorIntfMissing {}"),78new JavaSource("CompletionErrorExcMissing", "public class CompletionErrorExcMissing extends Exception {}"));79Boolean result = compiler.getTask(null, null, null, Arrays.asList("-d", "."), null, fileObjects).call();80if (!result)81throw new Error();82for (String delete : new String[] {"CompletionErrorMissing.class", "CompletionErrorIntfMissing.class", "CompletionErrorExcMissing.class"}) {83Files.delete(Paths.get(delete));84}85// run javadoc:86if (Main.execute("javadoc", "CompletionError", CompletionError.class.getClassLoader(),87"-classpath", ".",88System.getProperty("test.src", ".") + File.separatorChar + "CompletionError.java") != 0)89throw new Error();90}91}92}9394private static int test;95private static boolean testsDone;9697public static boolean start(com.sun.javadoc.RootDoc root) {98ClassDoc aux = root.classNamed("CompletionErrorAuxiliary");99if (aux == null)100throw new AssertionError("Cannot find CompletionErrorAuxiliary");101102FieldDoc tf = findField(aux, "tf");103MethodDoc tm = findMethod(aux, "tm");104MethodDoc cm = findMethod(aux, "toString");105switch (test) {106case 0: aux.superclass(); break;107case 1: aux.superclassType(); break;108case 2: aux.interfaces(); break;109case 3: aux.interfaceTypes(); break;110case 4: if (tf != null) tf.type(); break;111case 5: if (tm != null) tm.overriddenClass(); break;112case 6: if (tm != null) tm.overriddenMethod(); break;113case 7: if (tm != null) tm.overriddenType(); break;114case 8:115if (tm != null) {116for (Parameter p : tm.parameters()) {117p.type();118}119}120break;121case 9: if (tm != null) tm.receiverType(); break;122case 10: if (tm != null) tm.returnType(); break;123case 11: if (tm != null) tm.thrownExceptionTypes(); break;124case 12: if (tm != null) tm.thrownExceptions(); break;125case 13:126if (tm != null) {127for (TypeVariable tv : tm.typeParameters()) {128tv.bounds();129}130}131break;132case 14: if (cm != null) cm.overriddenClass(); break;133case 15: if (cm != null) cm.overriddenMethod(); break;134case 16: if (cm != null) cm.overriddenType(); testsDone = true; break;135default:136throw new IllegalStateException("Unrecognized test!");137}138test++;139return true;140}141142private static MethodDoc findMethod(ClassDoc cd, String name) {143for (MethodDoc m : cd.methods()) {144if (name.equals(m.name()))145return m;146}147148return null;149}150151private static FieldDoc findField(ClassDoc cd, String name) {152for (FieldDoc m : cd.fields()) {153if (name.equals(m.name()))154return m;155}156157return null;158}159160static class JavaSource extends SimpleJavaFileObject {161final String source;162163public JavaSource(String name, String source) {164super(URI.create("myfo:/" + name + ".java"), JavaFileObject.Kind.SOURCE);165this.source = source;166}167168@Override169public CharSequence getCharContent(boolean ignoreEncodingErrors) {170return source;171}172}173}174175176