Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/DocLint.java
41161 views
1
/*
2
* Copyright (c) 2012, 2020, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package jdk.javadoc.internal.doclint;
27
28
import java.io.File;
29
import java.io.IOException;
30
import java.io.PrintWriter;
31
import java.util.ArrayList;
32
import java.util.LinkedList;
33
import java.util.List;
34
import java.util.Queue;
35
36
import javax.lang.model.element.Name;
37
import javax.lang.model.util.Elements;
38
import javax.lang.model.util.Types;
39
import javax.tools.StandardLocation;
40
41
import com.sun.source.doctree.DocCommentTree;
42
import com.sun.source.tree.BlockTree;
43
import com.sun.source.tree.ClassTree;
44
import com.sun.source.tree.CompilationUnitTree;
45
import com.sun.source.tree.LambdaExpressionTree;
46
import com.sun.source.tree.ModuleTree;
47
import com.sun.source.tree.PackageTree;
48
import com.sun.source.tree.MethodTree;
49
import com.sun.source.tree.Tree;
50
import com.sun.source.tree.VariableTree;
51
import com.sun.source.util.DocTrees;
52
import com.sun.source.util.JavacTask;
53
import com.sun.source.util.TaskEvent;
54
import com.sun.source.util.TaskListener;
55
import com.sun.source.util.TreePath;
56
import com.sun.source.util.TreePathScanner;
57
import com.sun.tools.javac.api.JavacTaskImpl;
58
import com.sun.tools.javac.api.JavacTool;
59
import com.sun.tools.javac.file.JavacFileManager;
60
import com.sun.tools.javac.main.JavaCompiler;
61
import com.sun.tools.javac.util.Context;
62
import com.sun.tools.javac.util.DefinedBy;
63
import com.sun.tools.javac.util.DefinedBy.Api;
64
65
/**
66
* Multi-function entry point for the doc check utility.
67
*
68
* This class can be invoked in the following ways:
69
* <ul>
70
* <li>From the command line
71
* <li>From javac, as a plugin
72
* <li>Directly, via a simple API
73
* </ul>
74
*
75
* <p><b>This is NOT part of any supported API.
76
* If you write code that depends on this, you do so at your own
77
* risk. This code and its internal interfaces are subject to change
78
* or deletion without notice.</b></p>
79
*/
80
public class DocLint extends com.sun.tools.doclint.DocLint {
81
82
public static final String XMSGS_OPTION = "-Xmsgs";
83
public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:";
84
private static final String STATS = "-stats";
85
public static final String XCUSTOM_TAGS_PREFIX = "-XcustomTags:";
86
public static final String XCHECK_PACKAGE = "-XcheckPackage:";
87
public static final String SEPARATOR = ",";
88
89
// <editor-fold defaultstate="collapsed" desc="Command-line entry point">
90
public static void main(String... args) {
91
DocLint dl = new DocLint();
92
try {
93
dl.run(args);
94
} catch (BadArgs e) {
95
System.err.println(e.getMessage());
96
System.exit(1);
97
} catch (IOException e) {
98
System.err.println(dl.localize("dc.main.ioerror", e.getLocalizedMessage()));
99
System.exit(2);
100
}
101
}
102
103
// </editor-fold>
104
105
// <editor-fold defaultstate="collapsed" desc="Simple API">
106
107
public class BadArgs extends Exception {
108
private static final long serialVersionUID = 0;
109
BadArgs(String code, Object... args) {
110
super(localize(code, args));
111
this.code = code;
112
this.args = args;
113
}
114
115
final String code;
116
final transient Object[] args;
117
}
118
119
/**
120
* Simple API entry point.
121
* @param args Options and operands for doclint
122
* @throws BadArgs if an error is detected in any args
123
* @throws IOException if there are problems with any of the file arguments
124
*/
125
public void run(String... args) throws BadArgs, IOException {
126
PrintWriter out = new PrintWriter(System.out);
127
try {
128
run(out, args);
129
} finally {
130
out.flush();
131
}
132
}
133
134
public void run(PrintWriter out, String... args) throws BadArgs, IOException {
135
env = new Env();
136
processArgs(args);
137
138
boolean noFiles = javacFiles.isEmpty();
139
if (needHelp) {
140
showHelp(out);
141
if (noFiles)
142
return;
143
} else if (noFiles) {
144
out.println(localize("dc.main.no.files.given"));
145
return;
146
}
147
148
JavacTool tool = JavacTool.create();
149
150
JavacFileManager fm = new JavacFileManager(new Context(), false, null);
151
fm.setSymbolFileEnabled(false);
152
if (javacBootClassPath != null) {
153
fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath);
154
}
155
if (javacClassPath != null) {
156
fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath);
157
}
158
if (javacSourcePath != null) {
159
fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath);
160
}
161
162
JavacTask task = tool.getTask(out, fm, null, javacOpts, null,
163
fm.getJavaFileObjectsFromFiles(javacFiles));
164
Iterable<? extends CompilationUnitTree> units = task.parse();
165
((JavacTaskImpl) task).enter();
166
167
env.init(task);
168
checker = new Checker(env);
169
170
DeclScanner ds = new DeclScanner(env) {
171
@Override
172
void visitDecl(Tree tree, Name name) {
173
TreePath p = getCurrentPath();
174
DocCommentTree dc = env.trees.getDocCommentTree(p);
175
176
checker.scan(dc, p);
177
}
178
};
179
180
ds.scan(units, null);
181
182
reportStats(out);
183
184
Context ctx = ((JavacTaskImpl) task).getContext();
185
JavaCompiler c = JavaCompiler.instance(ctx);
186
c.printCount("error", c.errorCount());
187
c.printCount("warn", c.warningCount());
188
}
189
190
void processArgs(String... args) throws BadArgs {
191
javacOpts = new ArrayList<>();
192
javacFiles = new ArrayList<>();
193
194
if (args.length == 0)
195
needHelp = true;
196
197
for (int i = 0; i < args.length; i++) {
198
String arg = args[i];
199
if (arg.matches("-Xmax(errs|warns)") && i + 1 < args.length) {
200
if (args[++i].matches("[0-9]+")) {
201
javacOpts.add(arg);
202
javacOpts.add(args[i]);
203
} else {
204
throw new BadArgs("dc.bad.value.for.option", arg, args[i]);
205
}
206
} else if ((arg.equals("-target") || arg.equals("-source")) && i + 1 < args.length) {
207
javacOpts.add(arg);
208
javacOpts.add(args[++i]);
209
} else if (arg.equals(STATS)) {
210
env.messages.setStatsEnabled(true);
211
} else if (arg.equals("-bootclasspath") && i + 1 < args.length) {
212
javacBootClassPath = splitPath(args[++i]);
213
} else if (arg.equals("-classpath") && i + 1 < args.length) {
214
javacClassPath = splitPath(args[++i]);
215
} else if (arg.equals("-cp") && i + 1 < args.length) {
216
javacClassPath = splitPath(args[++i]);
217
} else if (arg.equals("-sourcepath") && i + 1 < args.length) {
218
javacSourcePath = splitPath(args[++i]);
219
} else if (arg.equals(XMSGS_OPTION)) {
220
env.messages.setOptions(null);
221
} else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
222
env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
223
} else if (arg.startsWith(XCUSTOM_TAGS_PREFIX)) {
224
env.setCustomTags(arg.substring(arg.indexOf(":") + 1));
225
} else if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")
226
|| arg.equals("-?") || arg.equals("-usage")) {
227
needHelp = true;
228
} else if (arg.startsWith("-")) {
229
throw new BadArgs("dc.bad.option", arg);
230
} else {
231
while (i < args.length)
232
javacFiles.add(new File(args[i++]));
233
}
234
}
235
}
236
237
void showHelp(PrintWriter out) {
238
String msg = localize("dc.main.usage");
239
for (String line: msg.split("\n"))
240
out.println(line);
241
}
242
243
List<File> splitPath(String path) {
244
List<File> files = new ArrayList<>();
245
for (String f: path.split(File.pathSeparator)) {
246
if (f.length() > 0)
247
files.add(new File(f));
248
}
249
return files;
250
}
251
252
List<File> javacBootClassPath;
253
List<File> javacClassPath;
254
List<File> javacSourcePath;
255
List<String> javacOpts;
256
List<File> javacFiles;
257
boolean needHelp = false;
258
259
// </editor-fold>
260
261
// <editor-fold defaultstate="collapsed" desc="javac Plugin">
262
263
@Override @DefinedBy(Api.COMPILER_TREE)
264
public String getName() {
265
return "doclint";
266
}
267
268
@Override @DefinedBy(Api.COMPILER_TREE)
269
public void init(JavacTask task, String... args) {
270
init(task, args, true);
271
}
272
273
// </editor-fold>
274
275
// <editor-fold defaultstate="collapsed" desc="Embedding API">
276
277
public void init(JavacTask task, String[] args, boolean addTaskListener) {
278
env = new Env();
279
env.init(task);
280
processArgs(env, args);
281
282
checker = new Checker(env);
283
284
if (addTaskListener) {
285
final DeclScanner ds = new DeclScanner(env) {
286
@Override
287
void visitDecl(Tree tree, Name name) {
288
TreePath p = getCurrentPath();
289
DocCommentTree dc = env.trees.getDocCommentTree(p);
290
291
checker.scan(dc, p);
292
}
293
};
294
295
TaskListener tl = new TaskListener() {
296
@Override @DefinedBy(Api.COMPILER_TREE)
297
public void started(TaskEvent e) {
298
switch (e.getKind()) {
299
case ANALYZE:
300
CompilationUnitTree tree;
301
while ((tree = todo.poll()) != null)
302
ds.scan(tree, null);
303
break;
304
}
305
}
306
307
@Override @DefinedBy(Api.COMPILER_TREE)
308
public void finished(TaskEvent e) {
309
switch (e.getKind()) {
310
case PARSE:
311
todo.add(e.getCompilationUnit());
312
break;
313
}
314
}
315
316
Queue<CompilationUnitTree> todo = new LinkedList<>();
317
};
318
319
task.addTaskListener(tl);
320
}
321
}
322
323
public void init(DocTrees trees, Elements elements, Types types, String... args) {
324
env = new Env();
325
env.init(trees, elements, types);
326
processArgs(env, args);
327
328
checker = new Checker(env);
329
}
330
331
private void processArgs(Env env, String... args) {
332
for (String arg : args) {
333
if (arg.equals(XMSGS_OPTION)) {
334
env.messages.setOptions(null);
335
} else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
336
env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
337
} else if (arg.startsWith(XCUSTOM_TAGS_PREFIX)) {
338
env.setCustomTags(arg.substring(arg.indexOf(":") + 1));
339
} else if (arg.startsWith(XCHECK_PACKAGE)) {
340
env.setCheckPackages(arg.substring(arg.indexOf(":") + 1));
341
} else
342
throw new IllegalArgumentException(arg);
343
}
344
}
345
346
public void scan(TreePath p) {
347
DocCommentTree dc = env.trees.getDocCommentTree(p);
348
checker.scan(dc, p);
349
}
350
351
public boolean shouldCheck(CompilationUnitTree unit) {
352
return env.shouldCheck(unit);
353
}
354
355
public void reportStats(PrintWriter out) {
356
env.messages.reportStats(out);
357
}
358
359
// </editor-fold>
360
361
Env env;
362
Checker checker;
363
364
public boolean isValidOption(String opt) {
365
if (opt.equals(XMSGS_OPTION))
366
return true;
367
if (opt.startsWith(XMSGS_CUSTOM_PREFIX))
368
return Messages.Options.isValidOptions(opt.substring(XMSGS_CUSTOM_PREFIX.length()));
369
if (opt.startsWith(XCHECK_PACKAGE)) {
370
return Env.validatePackages(opt.substring(opt.indexOf(":") + 1));
371
}
372
return false;
373
}
374
375
private String localize(String code, Object... args) {
376
Messages m = (env != null) ? env.messages : new Messages(null);
377
return m.localize(code, args);
378
}
379
380
// <editor-fold defaultstate="collapsed" desc="DeclScanner">
381
382
static abstract class DeclScanner extends TreePathScanner<Void, Void> {
383
final Env env;
384
385
public DeclScanner(Env env) {
386
this.env = env;
387
}
388
389
abstract void visitDecl(Tree tree, Name name);
390
391
@Override @DefinedBy(Api.COMPILER_TREE)
392
public Void visitPackage(PackageTree tree, Void ignore) {
393
visitDecl(tree, null);
394
return super.visitPackage(tree, ignore);
395
}
396
397
@Override @DefinedBy(Api.COMPILER_TREE)
398
public Void visitClass(ClassTree tree, Void ignore) {
399
visitDecl(tree, tree.getSimpleName());
400
return super.visitClass(tree, ignore);
401
}
402
403
@Override @DefinedBy(Api.COMPILER_TREE)
404
public Void visitMethod(MethodTree tree, Void ignore) {
405
visitDecl(tree, tree.getName());
406
return null;
407
}
408
409
@Override @DefinedBy(Api.COMPILER_TREE)
410
public Void visitModule(ModuleTree tree, Void ignore) {
411
visitDecl(tree, null);
412
return super.visitModule(tree, ignore);
413
}
414
415
@Override @DefinedBy(Api.COMPILER_TREE)
416
public Void visitVariable(VariableTree tree, Void ignore) {
417
visitDecl(tree, tree.getName());
418
return super.visitVariable(tree, ignore);
419
}
420
421
@Override @DefinedBy(Api.COMPILER_TREE)
422
public Void visitCompilationUnit(CompilationUnitTree node, Void p) {
423
if (!env.shouldCheck(node)) {
424
return null;
425
}
426
return super.visitCompilationUnit(node, p);
427
}
428
429
@Override @DefinedBy(Api.COMPILER_TREE)
430
public Void visitBlock(BlockTree tree, Void ignore) {
431
return null;
432
}
433
434
@Override @DefinedBy(Api.COMPILER_TREE)
435
public Void visitLambdaExpression(LambdaExpressionTree tree, Void ignore) {
436
return null;
437
}
438
439
}
440
441
// </editor-fold>
442
443
}
444
445