File: OldToolTester.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 65,320 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (338 lines) | stat: -rw-r--r-- 11,494 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * A utility used to invoke and test the javadoc tool.
 *
 * @author Scott Seligman
 */


import java.io.*;
import java.util.*;
import com.sun.javadoc.*;


public class OldToolTester {

    protected final String TEST_SRC = System.getProperty("test.src", ".");
    protected final String TEST_CLASSES = System.getProperty("test.classes",
                                                             ".");
    private final String DEFAULT_ARGS[] = {
        "-sourcepath", TEST_SRC,
    };

    private final File outputFile = new File(TEST_CLASSES, "testrun.out");
    private final File expectedOutputFile = new File(TEST_SRC, "expected.out");
//  private final File bootstrapMarkerFile = new File("bootstrap");

    // True if we should "set expectations" by writing the expected output file
    // rather than reading it and comparing.
//  private final boolean bootstrap = bootstrapMarkerFile.isFile();

    private String docletName;
    private String[] args;
    private Writer out = null;


    /*
     * Individual tests can extend this to create generics-aware doclets.
     */
    public static abstract class Doclet extends com.sun.javadoc.Doclet {
        public static LanguageVersion languageVersion() {
            return LanguageVersion.JAVA_1_5;
        }
    }


    public OldToolTester(String docletName) {
        this(docletName, new String[0]);
    }

    public OldToolTester(String docletName, String... additionalArgs) {
        this.docletName = docletName;

        int len = DEFAULT_ARGS.length + additionalArgs.length;
        args = new String[len];
        System.arraycopy(DEFAULT_ARGS, 0, args, 0, DEFAULT_ARGS.length);
        System.arraycopy(additionalArgs, 0, args, DEFAULT_ARGS.length,
                         additionalArgs.length);

        try {
            out = new BufferedWriter(new FileWriter(outputFile));
        } catch (IOException e) {
            throw new Error("Could not open output file " + outputFile);
        }
    }

    public void run() throws IOException {
        try {
            if (com.sun.tools.javadoc.Main.execute("javadoc",
                                                   docletName,
                                                   getClass().getClassLoader(),
                                                   args) != 0) {
                throw new Error("Javadoc errors encountered.");
            }
            System.out.println("--> Output written to " + outputFile);
        } finally {
            out.close();
        }
    }

    /*
     * Compare output of test run to expected output.
     * Throw an Error if they don't match.
     */
    public void verify() throws IOException {
        BufferedReader thisRun =
            new BufferedReader(new FileReader(outputFile));
        BufferedReader expected =
            new BufferedReader(new FileReader(expectedOutputFile));

        for (int lineNum = 1; true; lineNum++) {
            String line1 = thisRun.readLine();
            String line2 = expected.readLine();
            if (line1 == null && line2 == null) {
                return;         // EOF with all lines matching
            }
            if (line1 == null || !line1.equals(line2)) {
                throw new Error(outputFile + ":" + lineNum +
                                ": output doesn't match");
            }
        }
    }


    public void println(Object o) throws IOException {
        prln(0, o);
    }

    public void println() throws IOException {
        prln();
    }

    public void printPackage(PackageDoc p) throws IOException {
        prPackage(0, p);
    }

    public void printClass(ClassDoc cd) throws IOException {
        if (cd.isAnnotationType())
            printAnnotationType((AnnotationTypeDoc)cd);
        else
            prClass(0, cd);
    }

    public void printAnnotationType(AnnotationTypeDoc at) throws IOException {
        prAnnotationType(0, at);
    }

    public void printField(FieldDoc f) throws IOException {
        prField(0, f);
    }

    public void printParameter(Parameter p) throws IOException {
        prParameter(0, p);
    }

    public void printMethod(MethodDoc m) throws IOException {
        prln(0, "method " + m);
        prMethod(0, m);
    }

    public void printAnnotationTypeElement(AnnotationTypeElementDoc e)
                                                        throws IOException {
        prln(0, "element " + e);
        prMethod(0, e);
    }

    public void printConstructor(ConstructorDoc c) throws IOException {
        prln(0, "constructor " + c);
        prExecutable(0, c);
    }


    private void prPackage(int off, PackageDoc p) throws IOException {
        prln(off, "package " + p);
        prAnnotations(off + 2, p.annotations());
    }

    private void prClass(int off, ClassDoc cd) throws IOException {
        prln(off,
             (cd.isInterface() ? "interface" : cd.isEnum() ? "enum" : "class")
             + " " + cd);
        prln(off + 2, "name: " + cd.simpleTypeName() + " / " +
             cd.typeName() + " / " + cd.qualifiedTypeName());
        prAnnotations(off + 2, cd.annotations());
        prLabel(off + 2, "type parameters");
        for (Type t : cd.typeParameters())
            prln(off + 4, t);
        prParamTags(off + 2, cd.typeParamTags());
        prLabel(off + 2, "nested in");
        prln(off + 4, cd.containingClass());
        prLabel(off + 2, "superclass");
        prln(off + 4, cd.superclassType());
        prLabel(off + 2, "interfaces");
        Type[] ts = cd.interfaceTypes();
        Arrays.sort(ts);
        for (Type t : ts)
            prln(off + 4, t);
        prLabel(off + 2, "enum constants");
        for (FieldDoc f : cd.enumConstants())
            prln(off + 4, f.name());
        prLabel(off + 2, "fields");
        for (FieldDoc f : cd.fields())
            prln(off + 4, f.type() + " " + f.name());
        prLabel(off + 2, "constructors");
        for (ConstructorDoc c : cd.constructors())
            prln(off + 4, c.name() + c.flatSignature());
        prLabel(off + 2, "methods");
        for (MethodDoc m : cd.methods())
            prln(off + 4, typeUseString(m.returnType()) + " " +
                          m.name() + m.flatSignature());
    }

    private void prAnnotationType(int off, AnnotationTypeDoc at)
                                                        throws IOException {
        prln(off, "@interface " + at);
        prAnnotations(off + 2, at.annotations());
        prLabel(off + 2, "elements");
        for (AnnotationTypeElementDoc e : at.elements()) {
            String def = (e.defaultValue() == null)
                                ? ""
                                : " default " + e.defaultValue();
            prln(off + 4, typeUseString(e.returnType()) + " " + e.name() +
                          e.flatSignature() + def);
        }
    }

    private void prField(int off, FieldDoc f) throws IOException {
        prln(off, "field " + typeUseString(f.type()) + " " + f.name());
        prAnnotations(off + 2, f.annotations());
    }

    private void prParameter(int off, Parameter p) throws IOException {
        prln(off, "parameter " + p);
        prAnnotations(off + 2, p.annotations());
    }

    private void prMethod(int off, MethodDoc m) throws IOException {
        prExecutable(off, m);
        prLabel(off + 2, "returns");
        prln(off + 4, typeUseString(m.returnType()));
        prLabel(off + 2, "overridden type");
        prln(off + 4, m.overriddenType());
    }

    private void prExecutable(int off, ExecutableMemberDoc m)
                                                        throws IOException {
        if (!m.isAnnotationTypeElement()) {
            prln(off + 2, "signature: " + m.flatSignature());
            prln(off + 2, "           " + m.signature());
        }
        prAnnotations(off + 2, m.annotations());
        prParamTags(off + 2, m.typeParamTags());
        prParamTags(off + 2, m.paramTags());
        prLabel(off + 2, "type parameters");
        for (Type t : m.typeParameters())
            prln(off + 4, t);
        prLabel(off + 2, "throws");
        Type[] ts = m.thrownExceptionTypes();
        Arrays.sort(ts);
        for (Type t : ts)
            prln(off + 4, t);
    }

    private void prAnnotations(int off, AnnotationDesc[] as)
                                                        throws IOException {
        prLabel(off, "annotations");
        for (AnnotationDesc a : as)
            prln(off + 2, a.toString());
    }

    private void prParamTags(int off, ParamTag tags[]) throws IOException {
        for (ParamTag tag : tags)
            prParamTag(off, tag);
    }

    private void prParamTag(int off, ParamTag tag) throws IOException {
        String name = tag.parameterName();
        if (tag.isTypeParameter()) name = "<" + name + ">";
        prln(off, "@param " + name + " " + tag.parameterComment());
    }


    private String typeUseString(Type t) {
        return (t instanceof ClassDoc || t instanceof TypeVariable)
                ? t.typeName()
                : t.toString();
    }


    // Labels queued for possible printing.  Innermost is first in list.
    List<Line> labels = new ArrayList<Line>();

    // Print label if its section is nonempty.
    void prLabel(int off, String s) {
        while (!labels.isEmpty() && labels.get(0).off >= off)
            labels.remove(0);
        labels.add(0, new Line(off, s));
    }

    // Print queued labels with offsets less than "off".
    void popLabels(int off) throws IOException {
        while (!labels.isEmpty()) {
            Line label = labels.remove(0);
            if (label.off < off)
                prln(label.off, label.o + ":");
        }
    }

    // Print "o" at given offset.
    void pr(int off, Object o) throws IOException {
        popLabels(off);
        for (int i = 0; i < off; i++)
            out.write(' ');
        if (o != null)
            out.write(o.toString());
    }

    // Print "o" (if non-null) at given offset, then newline.
    void prln(int off, Object o) throws IOException {
        if (o != null) {
            pr(off, o);
            prln();
        }
    }

    // Print newline.
    void prln() throws IOException {
        out.write('\n');        // don't want platform-dependent separator
    }


    static class Line {
        int off;
        Object o;
        Line(int off, Object o) { this.off = off; this.o = o; }
    }
}