Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/lib/Tester.java
38813 views
/*1* Copyright (c) 2003, 2008, 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* A utility used to invoke and test the javadoc tool.25*26* @author Scott Seligman27*/282930import java.io.*;31import java.util.*;32import com.sun.javadoc.*;333435public class Tester {3637protected final String TEST_SRC = System.getProperty("test.src", ".");38protected final String TEST_CLASSES = System.getProperty("test.classes",39".");40private final String DEFAULT_ARGS[] = {41"-sourcepath", TEST_SRC,42};4344private final File outputFile = new File(TEST_CLASSES, "testrun.out");45private final File expectedOutputFile = new File(TEST_SRC, "expected.out");46// private final File bootstrapMarkerFile = new File("bootstrap");4748// True if we should "set expectations" by writing the expected output file49// rather than reading it and comparing.50// private final boolean bootstrap = bootstrapMarkerFile.isFile();5152private String docletName;53private String[] args;54private Writer out = null;555657/*58* Individual tests can extend this to create generics-aware doclets.59*/60public static abstract class Doclet extends com.sun.javadoc.Doclet {61public static LanguageVersion languageVersion() {62return LanguageVersion.JAVA_1_5;63}64}656667public Tester(String docletName) {68this(docletName, new String[0]);69}7071public Tester(String docletName, String... additionalArgs) {72this.docletName = docletName;7374int len = DEFAULT_ARGS.length + additionalArgs.length;75args = new String[len];76System.arraycopy(DEFAULT_ARGS, 0, args, 0, DEFAULT_ARGS.length);77System.arraycopy(additionalArgs, 0, args, DEFAULT_ARGS.length,78additionalArgs.length);7980try {81out = new BufferedWriter(new FileWriter(outputFile));82} catch (IOException e) {83throw new Error("Could not open output file " + outputFile);84}85}8687public void run() throws IOException {88try {89if (com.sun.tools.javadoc.Main.execute("javadoc",90docletName,91getClass().getClassLoader(),92args) != 0) {93throw new Error("Javadoc errors encountered.");94}95System.out.println("--> Output written to " + outputFile);96} finally {97out.close();98}99}100101/*102* Compare output of test run to expected output.103* Throw an Error if they don't match.104*/105public void verify() throws IOException {106BufferedReader thisRun =107new BufferedReader(new FileReader(outputFile));108BufferedReader expected =109new BufferedReader(new FileReader(expectedOutputFile));110111for (int lineNum = 1; true; lineNum++) {112String line1 = thisRun.readLine();113String line2 = expected.readLine();114if (line1 == null && line2 == null) {115return; // EOF with all lines matching116}117if (line1 == null || !line1.equals(line2)) {118throw new Error(outputFile + ":" + lineNum +119": output doesn't match");120}121}122}123124125public void println(Object o) throws IOException {126prln(0, o);127}128129public void println() throws IOException {130prln();131}132133public void printPackage(PackageDoc p) throws IOException {134prPackage(0, p);135}136137public void printClass(ClassDoc cd) throws IOException {138if (cd.isAnnotationType())139printAnnotationType((AnnotationTypeDoc)cd);140else141prClass(0, cd);142}143144public void printAnnotationType(AnnotationTypeDoc at) throws IOException {145prAnnotationType(0, at);146}147148public void printField(FieldDoc f) throws IOException {149prField(0, f);150}151152public void printParameter(Parameter p) throws IOException {153prParameter(0, p);154}155156public void printMethod(MethodDoc m) throws IOException {157prln(0, "method " + m);158prMethod(0, m);159}160161public void printAnnotationTypeElement(AnnotationTypeElementDoc e)162throws IOException {163prln(0, "element " + e);164prMethod(0, e);165}166167public void printConstructor(ConstructorDoc c) throws IOException {168prln(0, "constructor " + c);169prExecutable(0, c);170}171172173private void prPackage(int off, PackageDoc p) throws IOException {174prln(off, "package " + p);175prAnnotations(off + 2, p.annotations());176}177178private void prClass(int off, ClassDoc cd) throws IOException {179prln(off,180(cd.isInterface() ? "interface" : cd.isEnum() ? "enum" : "class")181+ " " + cd);182prln(off + 2, "name: " + cd.simpleTypeName() + " / " +183cd.typeName() + " / " + cd.qualifiedTypeName());184prAnnotations(off + 2, cd.annotations());185prLabel(off + 2, "type parameters");186for (Type t : cd.typeParameters())187prln(off + 4, t);188prParamTags(off + 2, cd.typeParamTags());189prLabel(off + 2, "nested in");190prln(off + 4, cd.containingClass());191prLabel(off + 2, "superclass");192prln(off + 4, cd.superclassType());193prLabel(off + 2, "interfaces");194Type[] ts = cd.interfaceTypes();195Arrays.sort(ts);196for (Type t : ts)197prln(off + 4, t);198prLabel(off + 2, "enum constants");199for (FieldDoc f : cd.enumConstants())200prln(off + 4, f.name());201prLabel(off + 2, "fields");202for (FieldDoc f : cd.fields())203prln(off + 4, f.type() + " " + f.name());204prLabel(off + 2, "constructors");205for (ConstructorDoc c : cd.constructors())206prln(off + 4, c.name() + c.flatSignature());207prLabel(off + 2, "methods");208for (MethodDoc m : cd.methods())209prln(off + 4, typeUseString(m.returnType()) + " " +210m.name() + m.flatSignature());211}212213private void prAnnotationType(int off, AnnotationTypeDoc at)214throws IOException {215prln(off, "@interface " + at);216prAnnotations(off + 2, at.annotations());217prLabel(off + 2, "elements");218for (AnnotationTypeElementDoc e : at.elements()) {219String def = (e.defaultValue() == null)220? ""221: " default " + e.defaultValue();222prln(off + 4, typeUseString(e.returnType()) + " " + e.name() +223e.flatSignature() + def);224}225}226227private void prField(int off, FieldDoc f) throws IOException {228prln(off, "field " + typeUseString(f.type()) + " " + f.name());229prAnnotations(off + 2, f.annotations());230}231232private void prParameter(int off, Parameter p) throws IOException {233prln(off, "parameter " + p);234prAnnotations(off + 2, p.annotations());235}236237private void prMethod(int off, MethodDoc m) throws IOException {238prExecutable(off, m);239prLabel(off + 2, "returns");240prln(off + 4, typeUseString(m.returnType()));241prLabel(off + 2, "overridden type");242prln(off + 4, m.overriddenType());243}244245private void prExecutable(int off, ExecutableMemberDoc m)246throws IOException {247if (!m.isAnnotationTypeElement()) {248prln(off + 2, "signature: " + m.flatSignature());249prln(off + 2, " " + m.signature());250}251prAnnotations(off + 2, m.annotations());252prParamTags(off + 2, m.typeParamTags());253prParamTags(off + 2, m.paramTags());254prLabel(off + 2, "type parameters");255for (Type t : m.typeParameters())256prln(off + 4, t);257prLabel(off + 2, "throws");258Type[] ts = m.thrownExceptionTypes();259Arrays.sort(ts);260for (Type t : ts)261prln(off + 4, t);262}263264private void prAnnotations(int off, AnnotationDesc[] as)265throws IOException {266prLabel(off, "annotations");267for (AnnotationDesc a : as)268prln(off + 2, a.toString());269}270271private void prParamTags(int off, ParamTag tags[]) throws IOException {272for (ParamTag tag : tags)273prParamTag(off, tag);274}275276private void prParamTag(int off, ParamTag tag) throws IOException {277String name = tag.parameterName();278if (tag.isTypeParameter()) name = "<" + name + ">";279prln(off, "@param " + name + " " + tag.parameterComment());280}281282283private String typeUseString(Type t) {284return (t instanceof ClassDoc || t instanceof TypeVariable)285? t.typeName()286: t.toString();287}288289290// Labels queued for possible printing. Innermost is first in list.291List<Line> labels = new ArrayList<Line>();292293// Print label if its section is nonempty.294void prLabel(int off, String s) {295while (!labels.isEmpty() && labels.get(0).off >= off)296labels.remove(0);297labels.add(0, new Line(off, s));298}299300// Print queued labels with offsets less than "off".301void popLabels(int off) throws IOException {302while (!labels.isEmpty()) {303Line label = labels.remove(0);304if (label.off < off)305prln(label.off, label.o + ":");306}307}308309// Print "o" at given offset.310void pr(int off, Object o) throws IOException {311popLabels(off);312for (int i = 0; i < off; i++)313out.write(' ');314if (o != null)315out.write(o.toString());316}317318// Print "o" (if non-null) at given offset, then newline.319void prln(int off, Object o) throws IOException {320if (o != null) {321pr(off, o);322prln();323}324}325326// Print newline.327void prln() throws IOException {328out.write('\n'); // don't want platform-dependent separator329}330331332static class Line {333int off;334Object o;335Line(int off, Object o) { this.off = off; this.o = o; }336}337}338339340