test/tools/javadoc/lib/Tester.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * A utility used to invoke and test the javadoc tool.
aoqi@0 26 *
aoqi@0 27 * @author Scott Seligman
aoqi@0 28 */
aoqi@0 29
aoqi@0 30
aoqi@0 31 import java.io.*;
aoqi@0 32 import java.util.*;
aoqi@0 33 import com.sun.javadoc.*;
aoqi@0 34
aoqi@0 35
aoqi@0 36 public class Tester {
aoqi@0 37
aoqi@0 38 protected final String TEST_SRC = System.getProperty("test.src", ".");
aoqi@0 39 protected final String TEST_CLASSES = System.getProperty("test.classes",
aoqi@0 40 ".");
aoqi@0 41 private final String DEFAULT_ARGS[] = {
aoqi@0 42 "-sourcepath", TEST_SRC,
aoqi@0 43 };
aoqi@0 44
aoqi@0 45 private final File outputFile = new File(TEST_CLASSES, "testrun.out");
aoqi@0 46 private final File expectedOutputFile = new File(TEST_SRC, "expected.out");
aoqi@0 47 // private final File bootstrapMarkerFile = new File("bootstrap");
aoqi@0 48
aoqi@0 49 // True if we should "set expectations" by writing the expected output file
aoqi@0 50 // rather than reading it and comparing.
aoqi@0 51 // private final boolean bootstrap = bootstrapMarkerFile.isFile();
aoqi@0 52
aoqi@0 53 private String docletName;
aoqi@0 54 private String[] args;
aoqi@0 55 private Writer out = null;
aoqi@0 56
aoqi@0 57
aoqi@0 58 /*
aoqi@0 59 * Individual tests can extend this to create generics-aware doclets.
aoqi@0 60 */
aoqi@0 61 public static abstract class Doclet extends com.sun.javadoc.Doclet {
aoqi@0 62 public static LanguageVersion languageVersion() {
aoqi@0 63 return LanguageVersion.JAVA_1_5;
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66
aoqi@0 67
aoqi@0 68 public Tester(String docletName) {
aoqi@0 69 this(docletName, new String[0]);
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 public Tester(String docletName, String... additionalArgs) {
aoqi@0 73 this.docletName = docletName;
aoqi@0 74
aoqi@0 75 int len = DEFAULT_ARGS.length + additionalArgs.length;
aoqi@0 76 args = new String[len];
aoqi@0 77 System.arraycopy(DEFAULT_ARGS, 0, args, 0, DEFAULT_ARGS.length);
aoqi@0 78 System.arraycopy(additionalArgs, 0, args, DEFAULT_ARGS.length,
aoqi@0 79 additionalArgs.length);
aoqi@0 80
aoqi@0 81 try {
aoqi@0 82 out = new BufferedWriter(new FileWriter(outputFile));
aoqi@0 83 } catch (IOException e) {
aoqi@0 84 throw new Error("Could not open output file " + outputFile);
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 public void run() throws IOException {
aoqi@0 89 try {
aoqi@0 90 if (com.sun.tools.javadoc.Main.execute("javadoc",
aoqi@0 91 docletName,
aoqi@0 92 getClass().getClassLoader(),
aoqi@0 93 args) != 0) {
aoqi@0 94 throw new Error("Javadoc errors encountered.");
aoqi@0 95 }
aoqi@0 96 System.out.println("--> Output written to " + outputFile);
aoqi@0 97 } finally {
aoqi@0 98 out.close();
aoqi@0 99 }
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 /*
aoqi@0 103 * Compare output of test run to expected output.
aoqi@0 104 * Throw an Error if they don't match.
aoqi@0 105 */
aoqi@0 106 public void verify() throws IOException {
aoqi@0 107 BufferedReader thisRun =
aoqi@0 108 new BufferedReader(new FileReader(outputFile));
aoqi@0 109 BufferedReader expected =
aoqi@0 110 new BufferedReader(new FileReader(expectedOutputFile));
aoqi@0 111
aoqi@0 112 for (int lineNum = 1; true; lineNum++) {
aoqi@0 113 String line1 = thisRun.readLine();
aoqi@0 114 String line2 = expected.readLine();
aoqi@0 115 if (line1 == null && line2 == null) {
aoqi@0 116 return; // EOF with all lines matching
aoqi@0 117 }
aoqi@0 118 if (line1 == null || !line1.equals(line2)) {
aoqi@0 119 throw new Error(outputFile + ":" + lineNum +
aoqi@0 120 ": output doesn't match");
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124
aoqi@0 125
aoqi@0 126 public void println(Object o) throws IOException {
aoqi@0 127 prln(0, o);
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public void println() throws IOException {
aoqi@0 131 prln();
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public void printPackage(PackageDoc p) throws IOException {
aoqi@0 135 prPackage(0, p);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public void printClass(ClassDoc cd) throws IOException {
aoqi@0 139 if (cd.isAnnotationType())
aoqi@0 140 printAnnotationType((AnnotationTypeDoc)cd);
aoqi@0 141 else
aoqi@0 142 prClass(0, cd);
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 public void printAnnotationType(AnnotationTypeDoc at) throws IOException {
aoqi@0 146 prAnnotationType(0, at);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 public void printField(FieldDoc f) throws IOException {
aoqi@0 150 prField(0, f);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 public void printParameter(Parameter p) throws IOException {
aoqi@0 154 prParameter(0, p);
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 public void printMethod(MethodDoc m) throws IOException {
aoqi@0 158 prln(0, "method " + m);
aoqi@0 159 prMethod(0, m);
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 public void printAnnotationTypeElement(AnnotationTypeElementDoc e)
aoqi@0 163 throws IOException {
aoqi@0 164 prln(0, "element " + e);
aoqi@0 165 prMethod(0, e);
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 public void printConstructor(ConstructorDoc c) throws IOException {
aoqi@0 169 prln(0, "constructor " + c);
aoqi@0 170 prExecutable(0, c);
aoqi@0 171 }
aoqi@0 172
aoqi@0 173
aoqi@0 174 private void prPackage(int off, PackageDoc p) throws IOException {
aoqi@0 175 prln(off, "package " + p);
aoqi@0 176 prAnnotations(off + 2, p.annotations());
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 private void prClass(int off, ClassDoc cd) throws IOException {
aoqi@0 180 prln(off,
aoqi@0 181 (cd.isInterface() ? "interface" : cd.isEnum() ? "enum" : "class")
aoqi@0 182 + " " + cd);
aoqi@0 183 prln(off + 2, "name: " + cd.simpleTypeName() + " / " +
aoqi@0 184 cd.typeName() + " / " + cd.qualifiedTypeName());
aoqi@0 185 prAnnotations(off + 2, cd.annotations());
aoqi@0 186 prLabel(off + 2, "type parameters");
aoqi@0 187 for (Type t : cd.typeParameters())
aoqi@0 188 prln(off + 4, t);
aoqi@0 189 prParamTags(off + 2, cd.typeParamTags());
aoqi@0 190 prLabel(off + 2, "nested in");
aoqi@0 191 prln(off + 4, cd.containingClass());
aoqi@0 192 prLabel(off + 2, "superclass");
aoqi@0 193 prln(off + 4, cd.superclassType());
aoqi@0 194 prLabel(off + 2, "interfaces");
aoqi@0 195 Type[] ts = cd.interfaceTypes();
aoqi@0 196 Arrays.sort(ts);
aoqi@0 197 for (Type t : ts)
aoqi@0 198 prln(off + 4, t);
aoqi@0 199 prLabel(off + 2, "enum constants");
aoqi@0 200 for (FieldDoc f : cd.enumConstants())
aoqi@0 201 prln(off + 4, f.name());
aoqi@0 202 prLabel(off + 2, "fields");
aoqi@0 203 for (FieldDoc f : cd.fields())
aoqi@0 204 prln(off + 4, f.type() + " " + f.name());
aoqi@0 205 prLabel(off + 2, "constructors");
aoqi@0 206 for (ConstructorDoc c : cd.constructors())
aoqi@0 207 prln(off + 4, c.name() + c.flatSignature());
aoqi@0 208 prLabel(off + 2, "methods");
aoqi@0 209 for (MethodDoc m : cd.methods())
aoqi@0 210 prln(off + 4, typeUseString(m.returnType()) + " " +
aoqi@0 211 m.name() + m.flatSignature());
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 private void prAnnotationType(int off, AnnotationTypeDoc at)
aoqi@0 215 throws IOException {
aoqi@0 216 prln(off, "@interface " + at);
aoqi@0 217 prAnnotations(off + 2, at.annotations());
aoqi@0 218 prLabel(off + 2, "elements");
aoqi@0 219 for (AnnotationTypeElementDoc e : at.elements()) {
aoqi@0 220 String def = (e.defaultValue() == null)
aoqi@0 221 ? ""
aoqi@0 222 : " default " + e.defaultValue();
aoqi@0 223 prln(off + 4, typeUseString(e.returnType()) + " " + e.name() +
aoqi@0 224 e.flatSignature() + def);
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 private void prField(int off, FieldDoc f) throws IOException {
aoqi@0 229 prln(off, "field " + typeUseString(f.type()) + " " + f.name());
aoqi@0 230 prAnnotations(off + 2, f.annotations());
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 private void prParameter(int off, Parameter p) throws IOException {
aoqi@0 234 prln(off, "parameter " + p);
aoqi@0 235 prAnnotations(off + 2, p.annotations());
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 private void prMethod(int off, MethodDoc m) throws IOException {
aoqi@0 239 prExecutable(off, m);
aoqi@0 240 prLabel(off + 2, "returns");
aoqi@0 241 prln(off + 4, typeUseString(m.returnType()));
aoqi@0 242 prLabel(off + 2, "overridden type");
aoqi@0 243 prln(off + 4, m.overriddenType());
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 private void prExecutable(int off, ExecutableMemberDoc m)
aoqi@0 247 throws IOException {
aoqi@0 248 if (!m.isAnnotationTypeElement()) {
aoqi@0 249 prln(off + 2, "signature: " + m.flatSignature());
aoqi@0 250 prln(off + 2, " " + m.signature());
aoqi@0 251 }
aoqi@0 252 prAnnotations(off + 2, m.annotations());
aoqi@0 253 prParamTags(off + 2, m.typeParamTags());
aoqi@0 254 prParamTags(off + 2, m.paramTags());
aoqi@0 255 prLabel(off + 2, "type parameters");
aoqi@0 256 for (Type t : m.typeParameters())
aoqi@0 257 prln(off + 4, t);
aoqi@0 258 prLabel(off + 2, "throws");
aoqi@0 259 Type[] ts = m.thrownExceptionTypes();
aoqi@0 260 Arrays.sort(ts);
aoqi@0 261 for (Type t : ts)
aoqi@0 262 prln(off + 4, t);
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 private void prAnnotations(int off, AnnotationDesc[] as)
aoqi@0 266 throws IOException {
aoqi@0 267 prLabel(off, "annotations");
aoqi@0 268 for (AnnotationDesc a : as)
aoqi@0 269 prln(off + 2, a.toString());
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 private void prParamTags(int off, ParamTag tags[]) throws IOException {
aoqi@0 273 for (ParamTag tag : tags)
aoqi@0 274 prParamTag(off, tag);
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 private void prParamTag(int off, ParamTag tag) throws IOException {
aoqi@0 278 String name = tag.parameterName();
aoqi@0 279 if (tag.isTypeParameter()) name = "<" + name + ">";
aoqi@0 280 prln(off, "@param " + name + " " + tag.parameterComment());
aoqi@0 281 }
aoqi@0 282
aoqi@0 283
aoqi@0 284 private String typeUseString(Type t) {
aoqi@0 285 return (t instanceof ClassDoc || t instanceof TypeVariable)
aoqi@0 286 ? t.typeName()
aoqi@0 287 : t.toString();
aoqi@0 288 }
aoqi@0 289
aoqi@0 290
aoqi@0 291 // Labels queued for possible printing. Innermost is first in list.
aoqi@0 292 List<Line> labels = new ArrayList<Line>();
aoqi@0 293
aoqi@0 294 // Print label if its section is nonempty.
aoqi@0 295 void prLabel(int off, String s) {
aoqi@0 296 while (!labels.isEmpty() && labels.get(0).off >= off)
aoqi@0 297 labels.remove(0);
aoqi@0 298 labels.add(0, new Line(off, s));
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 // Print queued labels with offsets less than "off".
aoqi@0 302 void popLabels(int off) throws IOException {
aoqi@0 303 while (!labels.isEmpty()) {
aoqi@0 304 Line label = labels.remove(0);
aoqi@0 305 if (label.off < off)
aoqi@0 306 prln(label.off, label.o + ":");
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 // Print "o" at given offset.
aoqi@0 311 void pr(int off, Object o) throws IOException {
aoqi@0 312 popLabels(off);
aoqi@0 313 for (int i = 0; i < off; i++)
aoqi@0 314 out.write(' ');
aoqi@0 315 if (o != null)
aoqi@0 316 out.write(o.toString());
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 // Print "o" (if non-null) at given offset, then newline.
aoqi@0 320 void prln(int off, Object o) throws IOException {
aoqi@0 321 if (o != null) {
aoqi@0 322 pr(off, o);
aoqi@0 323 prln();
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 // Print newline.
aoqi@0 328 void prln() throws IOException {
aoqi@0 329 out.write('\n'); // don't want platform-dependent separator
aoqi@0 330 }
aoqi@0 331
aoqi@0 332
aoqi@0 333 static class Line {
aoqi@0 334 int off;
aoqi@0 335 Object o;
aoqi@0 336 Line(int off, Object o) { this.off = off; this.o = o; }
aoqi@0 337 }
aoqi@0 338 }

mercurial