0N/A/*
553N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * A utility used to invoke and test the javadoc tool.
0N/A *
0N/A * @author Scott Seligman
0N/A */
0N/A
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport com.sun.javadoc.*;
0N/A
0N/A
0N/Apublic class Tester {
0N/A
0N/A protected final String TEST_SRC = System.getProperty("test.src", ".");
0N/A protected final String TEST_CLASSES = System.getProperty("test.classes",
0N/A ".");
0N/A private final String DEFAULT_ARGS[] = {
0N/A "-sourcepath", TEST_SRC,
0N/A };
0N/A
0N/A private final File outputFile = new File(TEST_CLASSES, "testrun.out");
0N/A private final File expectedOutputFile = new File(TEST_SRC, "expected.out");
0N/A// private final File bootstrapMarkerFile = new File("bootstrap");
0N/A
0N/A // True if we should "set expectations" by writing the expected output file
0N/A // rather than reading it and comparing.
0N/A// private final boolean bootstrap = bootstrapMarkerFile.isFile();
0N/A
0N/A private String docletName;
0N/A private String[] args;
0N/A private Writer out = null;
0N/A
0N/A
0N/A /*
0N/A * Individual tests can extend this to create generics-aware doclets.
0N/A */
0N/A public static abstract class Doclet extends com.sun.javadoc.Doclet {
0N/A public static LanguageVersion languageVersion() {
0N/A return LanguageVersion.JAVA_1_5;
0N/A }
0N/A }
0N/A
0N/A
0N/A public Tester(String docletName) {
0N/A this(docletName, new String[0]);
0N/A }
0N/A
0N/A public Tester(String docletName, String... additionalArgs) {
0N/A this.docletName = docletName;
0N/A
0N/A int len = DEFAULT_ARGS.length + additionalArgs.length;
0N/A args = new String[len];
0N/A System.arraycopy(DEFAULT_ARGS, 0, args, 0, DEFAULT_ARGS.length);
0N/A System.arraycopy(additionalArgs, 0, args, DEFAULT_ARGS.length,
0N/A additionalArgs.length);
0N/A
0N/A try {
0N/A out = new BufferedWriter(new FileWriter(outputFile));
0N/A } catch (IOException e) {
0N/A throw new Error("Could not open output file " + outputFile);
0N/A }
0N/A }
0N/A
0N/A public void run() throws IOException {
0N/A try {
0N/A if (com.sun.tools.javadoc.Main.execute("javadoc",
139N/A docletName,
139N/A getClass().getClassLoader(),
139N/A args) != 0) {
0N/A throw new Error("Javadoc errors encountered.");
0N/A }
0N/A System.out.println("--> Output written to " + outputFile);
0N/A } finally {
0N/A out.close();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Compare output of test run to expected output.
0N/A * Throw an Error if they don't match.
0N/A */
0N/A public void verify() throws IOException {
0N/A BufferedReader thisRun =
0N/A new BufferedReader(new FileReader(outputFile));
0N/A BufferedReader expected =
0N/A new BufferedReader(new FileReader(expectedOutputFile));
0N/A
0N/A for (int lineNum = 1; true; lineNum++) {
0N/A String line1 = thisRun.readLine();
0N/A String line2 = expected.readLine();
0N/A if (line1 == null && line2 == null) {
0N/A return; // EOF with all lines matching
0N/A }
0N/A if (line1 == null || !line1.equals(line2)) {
0N/A throw new Error(outputFile + ":" + lineNum +
0N/A ": output doesn't match");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A public void println(Object o) throws IOException {
0N/A prln(0, o);
0N/A }
0N/A
0N/A public void println() throws IOException {
0N/A prln();
0N/A }
0N/A
0N/A public void printPackage(PackageDoc p) throws IOException {
0N/A prPackage(0, p);
0N/A }
0N/A
0N/A public void printClass(ClassDoc cd) throws IOException {
0N/A if (cd.isAnnotationType())
0N/A printAnnotationType((AnnotationTypeDoc)cd);
0N/A else
0N/A prClass(0, cd);
0N/A }
0N/A
0N/A public void printAnnotationType(AnnotationTypeDoc at) throws IOException {
0N/A prAnnotationType(0, at);
0N/A }
0N/A
0N/A public void printField(FieldDoc f) throws IOException {
0N/A prField(0, f);
0N/A }
0N/A
0N/A public void printParameter(Parameter p) throws IOException {
0N/A prParameter(0, p);
0N/A }
0N/A
0N/A public void printMethod(MethodDoc m) throws IOException {
0N/A prln(0, "method " + m);
0N/A prMethod(0, m);
0N/A }
0N/A
0N/A public void printAnnotationTypeElement(AnnotationTypeElementDoc e)
0N/A throws IOException {
0N/A prln(0, "element " + e);
0N/A prMethod(0, e);
0N/A }
0N/A
0N/A public void printConstructor(ConstructorDoc c) throws IOException {
0N/A prln(0, "constructor " + c);
0N/A prExecutable(0, c);
0N/A }
0N/A
0N/A
0N/A private void prPackage(int off, PackageDoc p) throws IOException {
0N/A prln(off, "package " + p);
0N/A prAnnotations(off + 2, p.annotations());
0N/A }
0N/A
0N/A private void prClass(int off, ClassDoc cd) throws IOException {
0N/A prln(off,
0N/A (cd.isInterface() ? "interface" : cd.isEnum() ? "enum" : "class")
0N/A + " " + cd);
0N/A prln(off + 2, "name: " + cd.simpleTypeName() + " / " +
0N/A cd.typeName() + " / " + cd.qualifiedTypeName());
0N/A prAnnotations(off + 2, cd.annotations());
0N/A prLabel(off + 2, "type parameters");
0N/A for (Type t : cd.typeParameters())
0N/A prln(off + 4, t);
0N/A prParamTags(off + 2, cd.typeParamTags());
0N/A prLabel(off + 2, "nested in");
0N/A prln(off + 4, cd.containingClass());
0N/A prLabel(off + 2, "superclass");
0N/A prln(off + 4, cd.superclassType());
0N/A prLabel(off + 2, "interfaces");
0N/A Type[] ts = cd.interfaceTypes();
0N/A Arrays.sort(ts);
0N/A for (Type t : ts)
0N/A prln(off + 4, t);
0N/A prLabel(off + 2, "enum constants");
0N/A for (FieldDoc f : cd.enumConstants())
0N/A prln(off + 4, f.name());
0N/A prLabel(off + 2, "fields");
0N/A for (FieldDoc f : cd.fields())
0N/A prln(off + 4, f.type() + " " + f.name());
0N/A prLabel(off + 2, "constructors");
0N/A for (ConstructorDoc c : cd.constructors())
0N/A prln(off + 4, c.name() + c.flatSignature());
0N/A prLabel(off + 2, "methods");
0N/A for (MethodDoc m : cd.methods())
0N/A prln(off + 4, typeUseString(m.returnType()) + " " +
0N/A m.name() + m.flatSignature());
0N/A }
0N/A
0N/A private void prAnnotationType(int off, AnnotationTypeDoc at)
0N/A throws IOException {
0N/A prln(off, "@interface " + at);
0N/A prAnnotations(off + 2, at.annotations());
0N/A prLabel(off + 2, "elements");
0N/A for (AnnotationTypeElementDoc e : at.elements()) {
0N/A String def = (e.defaultValue() == null)
0N/A ? ""
0N/A : " default " + e.defaultValue();
0N/A prln(off + 4, typeUseString(e.returnType()) + " " + e.name() +
0N/A e.flatSignature() + def);
0N/A }
0N/A }
0N/A
0N/A private void prField(int off, FieldDoc f) throws IOException {
0N/A prln(off, "field " + typeUseString(f.type()) + " " + f.name());
0N/A prAnnotations(off + 2, f.annotations());
0N/A }
0N/A
0N/A private void prParameter(int off, Parameter p) throws IOException {
0N/A prln(off, "parameter " + p);
0N/A prAnnotations(off + 2, p.annotations());
0N/A }
0N/A
0N/A private void prMethod(int off, MethodDoc m) throws IOException {
0N/A prExecutable(off, m);
0N/A prLabel(off + 2, "returns");
0N/A prln(off + 4, typeUseString(m.returnType()));
0N/A prLabel(off + 2, "overridden type");
0N/A prln(off + 4, m.overriddenType());
0N/A }
0N/A
0N/A private void prExecutable(int off, ExecutableMemberDoc m)
0N/A throws IOException {
0N/A if (!m.isAnnotationTypeElement()) {
0N/A prln(off + 2, "signature: " + m.flatSignature());
0N/A prln(off + 2, " " + m.signature());
0N/A }
0N/A prAnnotations(off + 2, m.annotations());
0N/A prParamTags(off + 2, m.typeParamTags());
0N/A prParamTags(off + 2, m.paramTags());
0N/A prLabel(off + 2, "type parameters");
0N/A for (Type t : m.typeParameters())
0N/A prln(off + 4, t);
0N/A prLabel(off + 2, "throws");
0N/A Type[] ts = m.thrownExceptionTypes();
0N/A Arrays.sort(ts);
0N/A for (Type t : ts)
0N/A prln(off + 4, t);
0N/A }
0N/A
0N/A private void prAnnotations(int off, AnnotationDesc[] as)
0N/A throws IOException {
0N/A prLabel(off, "annotations");
0N/A for (AnnotationDesc a : as)
0N/A prln(off + 2, a.toString());
0N/A }
0N/A
0N/A private void prParamTags(int off, ParamTag tags[]) throws IOException {
0N/A for (ParamTag tag : tags)
0N/A prParamTag(off, tag);
0N/A }
0N/A
0N/A private void prParamTag(int off, ParamTag tag) throws IOException {
0N/A String name = tag.parameterName();
0N/A if (tag.isTypeParameter()) name = "<" + name + ">";
0N/A prln(off, "@param " + name + " " + tag.parameterComment());
0N/A }
0N/A
0N/A
0N/A private String typeUseString(Type t) {
0N/A return (t instanceof ClassDoc || t instanceof TypeVariable)
0N/A ? t.typeName()
0N/A : t.toString();
0N/A }
0N/A
0N/A
0N/A // Labels queued for possible printing. Innermost is first in list.
0N/A List<Line> labels = new ArrayList<Line>();
0N/A
0N/A // Print label if its section is nonempty.
0N/A void prLabel(int off, String s) {
0N/A while (!labels.isEmpty() && labels.get(0).off >= off)
0N/A labels.remove(0);
0N/A labels.add(0, new Line(off, s));
0N/A }
0N/A
0N/A // Print queued labels with offsets less than "off".
0N/A void popLabels(int off) throws IOException {
0N/A while (!labels.isEmpty()) {
0N/A Line label = labels.remove(0);
0N/A if (label.off < off)
0N/A prln(label.off, label.o + ":");
0N/A }
0N/A }
0N/A
0N/A // Print "o" at given offset.
0N/A void pr(int off, Object o) throws IOException {
0N/A popLabels(off);
0N/A for (int i = 0; i < off; i++)
0N/A out.write(' ');
0N/A if (o != null)
0N/A out.write(o.toString());
0N/A }
0N/A
0N/A // Print "o" (if non-null) at given offset, then newline.
0N/A void prln(int off, Object o) throws IOException {
0N/A if (o != null) {
0N/A pr(off, o);
0N/A prln();
0N/A }
0N/A }
0N/A
0N/A // Print newline.
0N/A void prln() throws IOException {
0N/A out.write('\n'); // don't want platform-dependent separator
0N/A }
0N/A
0N/A
0N/A static class Line {
0N/A int off;
0N/A Object o;
0N/A Line(int off, Object o) { this.off = off; this.o = o; }
0N/A }
0N/A}