test/tools/javadoc/CompletionError.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 2906
d3a51adc115f
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

     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  */
    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  * @modules jdk.javadoc
    30  * @run main CompletionError
    31  */
    33 import java.io.File;
    34 import java.net.URI;
    35 import java.nio.file.Files;
    36 import java.nio.file.Paths;
    37 import java.util.Arrays;
    38 import java.util.List;
    40 import javax.tools.JavaCompiler;
    41 import javax.tools.JavaFileObject;
    42 import javax.tools.SimpleJavaFileObject;
    43 import javax.tools.ToolProvider;
    45 import com.sun.javadoc.*;
    46 import com.sun.tools.javadoc.Main;
    48 public class CompletionError extends Doclet
    49 {
    50     private static final String template =
    51             "public class CompletionErrorAuxiliary #extends CompletionErrorMissing# #implements CompletionErrorIntfMissing# {" +
    52             "   #public CompletionErrorMissing tf;#" +
    53             "   #public CompletionErrorMissing tm() { return null; }#" +
    54             "   #public void tm(CompletionErrorMissing m) {}#" +
    55             "   #public void tm() throws CompletionErrorExcMissing {}#" +
    56             "   #public <T extends CompletionErrorMissing> void tm() {}#" +
    57             "   public String toString() { return null; }" +
    58             "}";
    60     public static void main(String[] args) throws Exception {
    61         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    62         String[] templateParts = template.split("#");
    63         int sources = templateParts.length / 2;
    64         for (int source = 0; source < sources; source++) {
    65             StringBuilder testSource = new StringBuilder();
    66             for (int i = 0; i < templateParts.length; i += 2) {
    67                 testSource.append(templateParts[i]);
    68                 if (i == 2 * source) {
    69                     testSource.append(templateParts[i + 1]);
    70                 }
    71             }
    72             test = 0;
    73             testsDone = false;
    74             while (!testsDone) {
    75                 List<JavaSource> fileObjects =
    76                         Arrays.asList(new JavaSource("CompletionErrorAuxiliary", testSource.toString()),
    77                                       new JavaSource("CompletionErrorMissing", "public class CompletionErrorMissing {}"),
    78                                       new JavaSource("CompletionErrorIntfMissing", "public interface CompletionErrorIntfMissing {}"),
    79                                       new JavaSource("CompletionErrorExcMissing", "public class CompletionErrorExcMissing extends Exception {}"));
    80                 Boolean result = compiler.getTask(null, null, null, Arrays.asList("-d", "."), null, fileObjects).call();
    81                 if (!result)
    82                     throw new Error();
    83                 for (String delete : new String[] {"CompletionErrorMissing.class", "CompletionErrorIntfMissing.class", "CompletionErrorExcMissing.class"}) {
    84                     Files.delete(Paths.get(delete));
    85                 }
    86                 // run javadoc:
    87                 if (Main.execute("javadoc", "CompletionError", CompletionError.class.getClassLoader(),
    88                                  "-classpath", ".",
    89                                  System.getProperty("test.src", ".") + File.separatorChar + "CompletionError.java") != 0)
    90                     throw new Error();
    91             }
    92         }
    93     }
    95     private static int test;
    96     private static boolean testsDone;
    98     public static boolean start(com.sun.javadoc.RootDoc root) {
    99         ClassDoc aux = root.classNamed("CompletionErrorAuxiliary");
   100         if (aux == null)
   101             throw new AssertionError("Cannot find CompletionErrorAuxiliary");
   103         FieldDoc tf = findField(aux, "tf");
   104         MethodDoc tm = findMethod(aux, "tm");
   105         MethodDoc cm = findMethod(aux, "toString");
   106         switch (test) {
   107             case 0: aux.superclass(); break;
   108             case 1: aux.superclassType(); break;
   109             case 2: aux.interfaces(); break;
   110             case 3: aux.interfaceTypes(); break;
   111             case 4: if (tf != null) tf.type(); break;
   112             case 5: if (tm != null) tm.overriddenClass(); break;
   113             case 6: if (tm != null) tm.overriddenMethod(); break;
   114             case 7: if (tm != null) tm.overriddenType(); break;
   115             case 8:
   116                 if (tm != null) {
   117                     for (Parameter p : tm.parameters()) {
   118                         p.type();
   119                     }
   120                 }
   121                 break;
   122             case 9: if (tm != null) tm.receiverType(); break;
   123             case 10: if (tm != null) tm.returnType(); break;
   124             case 11: if (tm != null) tm.thrownExceptionTypes(); break;
   125             case 12: if (tm != null) tm.thrownExceptions(); break;
   126             case 13:
   127                 if (tm != null) {
   128                     for (TypeVariable tv : tm.typeParameters()) {
   129                         tv.bounds();
   130                     }
   131                 }
   132                 break;
   133             case 14: if (cm != null) cm.overriddenClass(); break;
   134             case 15: if (cm != null) cm.overriddenMethod(); break;
   135             case 16: if (cm != null) cm.overriddenType(); testsDone = true; break;
   136             default:
   137                 throw new IllegalStateException("Unrecognized test!");
   138         }
   139         test++;
   140         return true;
   141     }
   143     private static MethodDoc findMethod(ClassDoc cd, String name) {
   144         for (MethodDoc m : cd.methods()) {
   145             if (name.equals(m.name()))
   146                 return m;
   147         }
   149         return null;
   150     }
   152     private static FieldDoc findField(ClassDoc cd, String name) {
   153         for (FieldDoc m : cd.fields()) {
   154             if (name.equals(m.name()))
   155                 return m;
   156         }
   158         return null;
   159     }
   161     static class JavaSource extends SimpleJavaFileObject {
   162         final String source;
   164         public JavaSource(String name, String source) {
   165             super(URI.create("myfo:/" + name + ".java"), JavaFileObject.Kind.SOURCE);
   166             this.source = source;
   167         }
   169         @Override
   170         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   171             return source;
   172         }
   173     }
   174 }

mercurial