author | jjg |
Tue, 17 Sep 2013 14:17:13 -0700 | |
changeset 20235 | 7834123e35dc |
parent 19123 | 50733e28eaca |
child 21500 | 475e59d3b40c |
permissions | -rw-r--r-- |
14952 | 1 |
/* |
15370 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
14952 | 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 com.sun.tools.doclint; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.PrintWriter; |
|
31 |
import java.util.ArrayList; |
|
20235
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
32 |
import java.util.HashSet; |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
33 |
import java.util.LinkedList; |
14952 | 34 |
import java.util.List; |
20235
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
35 |
import java.util.Queue; |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
36 |
import java.util.Set; |
14952 | 37 |
|
38 |
import javax.lang.model.element.Name; |
|
20235
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
39 |
import javax.tools.JavaFileObject; |
14952 | 40 |
import javax.tools.StandardLocation; |
41 |
||
42 |
import com.sun.source.doctree.DocCommentTree; |
|
43 |
import com.sun.source.tree.ClassTree; |
|
44 |
import com.sun.source.tree.CompilationUnitTree; |
|
45 |
import com.sun.source.tree.MethodTree; |
|
46 |
import com.sun.source.tree.Tree; |
|
47 |
import com.sun.source.tree.VariableTree; |
|
48 |
import com.sun.source.util.JavacTask; |
|
49 |
import com.sun.source.util.Plugin; |
|
50 |
import com.sun.source.util.TaskEvent; |
|
51 |
import com.sun.source.util.TaskListener; |
|
52 |
import com.sun.source.util.TreePath; |
|
53 |
import com.sun.source.util.TreePathScanner; |
|
54 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
55 |
import com.sun.tools.javac.api.JavacTool; |
|
56 |
import com.sun.tools.javac.file.JavacFileManager; |
|
57 |
import com.sun.tools.javac.main.JavaCompiler; |
|
58 |
import com.sun.tools.javac.util.Context; |
|
59 |
||
60 |
/** |
|
61 |
* Multi-function entry point for the doc check utility. |
|
62 |
* |
|
63 |
* This class can be invoked in the following ways: |
|
64 |
* <ul> |
|
65 |
* <li>From the command line |
|
66 |
* <li>From javac, as a plugin |
|
67 |
* <li>Directly, via a simple API |
|
68 |
* </ul> |
|
69 |
* |
|
70 |
* <p><b>This is NOT part of any supported API. |
|
71 |
* If you write code that depends on this, you do so at your own |
|
72 |
* risk. This code and its internal interfaces are subject to change |
|
73 |
* or deletion without notice.</b></p> |
|
74 |
*/ |
|
75 |
public class DocLint implements Plugin { |
|
76 |
||
77 |
public static final String XMSGS_OPTION = "-Xmsgs"; |
|
78 |
public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:"; |
|
79 |
private static final String STATS = "-stats"; |
|
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
15370
diff
changeset
|
80 |
public static final String XIMPLICIT_HEADERS = "-XimplicitHeaders:"; |
14952 | 81 |
|
82 |
// <editor-fold defaultstate="collapsed" desc="Command-line entry point"> |
|
83 |
public static void main(String... args) { |
|
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
84 |
DocLint dl = new DocLint(); |
14952 | 85 |
try { |
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
86 |
dl.run(args); |
14952 | 87 |
} catch (BadArgs e) { |
88 |
System.err.println(e.getMessage()); |
|
89 |
System.exit(1); |
|
90 |
} catch (IOException e) { |
|
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
91 |
System.err.println(dl.localize("dc.main.ioerror", e.getLocalizedMessage())); |
14952 | 92 |
System.exit(2); |
93 |
} |
|
94 |
} |
|
95 |
||
96 |
// </editor-fold> |
|
97 |
||
98 |
// <editor-fold defaultstate="collapsed" desc="Simple API"> |
|
99 |
||
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
100 |
public class BadArgs extends Exception { |
14952 | 101 |
private static final long serialVersionUID = 0; |
102 |
BadArgs(String code, Object... args) { |
|
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
103 |
super(localize(code, args)); |
14952 | 104 |
this.code = code; |
105 |
this.args = args; |
|
106 |
} |
|
107 |
||
108 |
final String code; |
|
109 |
final Object[] args; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Simple API entry point. |
|
114 |
*/ |
|
115 |
public void run(String... args) throws BadArgs, IOException { |
|
116 |
PrintWriter out = new PrintWriter(System.out); |
|
117 |
try { |
|
118 |
run(out, args); |
|
119 |
} finally { |
|
120 |
out.flush(); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
public void run(PrintWriter out, String... args) throws BadArgs, IOException { |
|
125 |
env = new Env(); |
|
126 |
processArgs(args); |
|
127 |
||
128 |
if (needHelp) |
|
129 |
showHelp(out); |
|
130 |
||
131 |
if (javacFiles.isEmpty()) { |
|
132 |
if (!needHelp) |
|
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
133 |
out.println(localize("dc.main.no.files.given")); |
14952 | 134 |
} |
135 |
||
136 |
JavacTool tool = JavacTool.create(); |
|
137 |
||
138 |
JavacFileManager fm = new JavacFileManager(new Context(), false, null); |
|
139 |
fm.setSymbolFileEnabled(false); |
|
140 |
fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath); |
|
141 |
fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath); |
|
142 |
fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath); |
|
143 |
||
144 |
JavacTask task = tool.getTask(out, fm, null, javacOpts, null, |
|
145 |
fm.getJavaFileObjectsFromFiles(javacFiles)); |
|
146 |
Iterable<? extends CompilationUnitTree> units = task.parse(); |
|
147 |
((JavacTaskImpl) task).enter(); |
|
148 |
||
149 |
env.init(task); |
|
150 |
checker = new Checker(env); |
|
151 |
||
152 |
DeclScanner ds = new DeclScanner() { |
|
153 |
@Override |
|
154 |
void visitDecl(Tree tree, Name name) { |
|
155 |
TreePath p = getCurrentPath(); |
|
156 |
DocCommentTree dc = env.trees.getDocCommentTree(p); |
|
157 |
||
158 |
checker.scan(dc, p); |
|
159 |
} |
|
160 |
}; |
|
161 |
||
162 |
ds.scan(units, null); |
|
163 |
||
164 |
reportStats(out); |
|
165 |
||
166 |
Context ctx = ((JavacTaskImpl) task).getContext(); |
|
167 |
JavaCompiler c = JavaCompiler.instance(ctx); |
|
168 |
c.printCount("error", c.errorCount()); |
|
169 |
c.printCount("warn", c.warningCount()); |
|
170 |
} |
|
171 |
||
172 |
void processArgs(String... args) throws BadArgs { |
|
19121
c626ed0c6ab0
8020664: doclint gives incorrect warnings on normal package statements
jjg
parents:
18908
diff
changeset
|
173 |
javacOpts = new ArrayList<>(); |
c626ed0c6ab0
8020664: doclint gives incorrect warnings on normal package statements
jjg
parents:
18908
diff
changeset
|
174 |
javacFiles = new ArrayList<>(); |
14952 | 175 |
|
176 |
if (args.length == 0) |
|
177 |
needHelp = true; |
|
178 |
||
179 |
for (int i = 0; i < args.length; i++) { |
|
180 |
String arg = args[i]; |
|
181 |
if (arg.matches("-Xmax(errs|warns)") && i + 1 < args.length) { |
|
182 |
if (args[++i].matches("[0-9]+")) { |
|
183 |
javacOpts.add(arg); |
|
184 |
javacOpts.add(args[i]); |
|
185 |
} else { |
|
186 |
throw new BadArgs("dc.bad.value.for.option", arg, args[i]); |
|
187 |
} |
|
188 |
} else if (arg.equals(STATS)) { |
|
189 |
env.messages.setStatsEnabled(true); |
|
15370 | 190 |
} else if (arg.equals("-bootclasspath") && i + 1 < args.length) { |
14952 | 191 |
javacBootClassPath = splitPath(args[++i]); |
15370 | 192 |
} else if (arg.equals("-classpath") && i + 1 < args.length) { |
14952 | 193 |
javacClassPath = splitPath(args[++i]); |
18898
0eab5f5e3d1d
8009924: some langtools tools do not accept -cp as an alias for -classpath
vromero
parents:
18004
diff
changeset
|
194 |
} else if (arg.equals("-cp") && i + 1 < args.length) { |
0eab5f5e3d1d
8009924: some langtools tools do not accept -cp as an alias for -classpath
vromero
parents:
18004
diff
changeset
|
195 |
javacClassPath = splitPath(args[++i]); |
15370 | 196 |
} else if (arg.equals("-sourcepath") && i + 1 < args.length) { |
14952 | 197 |
javacSourcePath = splitPath(args[++i]); |
198 |
} else if (arg.equals(XMSGS_OPTION)) { |
|
199 |
env.messages.setOptions(null); |
|
200 |
} else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) { |
|
201 |
env.messages.setOptions(arg.substring(arg.indexOf(":") + 1)); |
|
202 |
} else if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help") |
|
203 |
|| arg.equals("-?") || arg.equals("-usage")) { |
|
204 |
needHelp = true; |
|
205 |
} else if (arg.startsWith("-")) { |
|
206 |
throw new BadArgs("dc.bad.option", arg); |
|
207 |
} else { |
|
208 |
while (i < args.length) |
|
209 |
javacFiles.add(new File(args[i++])); |
|
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
void showHelp(PrintWriter out) { |
|
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
215 |
String msg = localize("dc.main.usage"); |
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
216 |
for (String line: msg.split("\n")) |
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
217 |
out.println(line); |
14952 | 218 |
} |
219 |
||
220 |
List<File> splitPath(String path) { |
|
19121
c626ed0c6ab0
8020664: doclint gives incorrect warnings on normal package statements
jjg
parents:
18908
diff
changeset
|
221 |
List<File> files = new ArrayList<>(); |
15370 | 222 |
for (String f: path.split(File.pathSeparator)) { |
14952 | 223 |
if (f.length() > 0) |
224 |
files.add(new File(f)); |
|
225 |
} |
|
226 |
return files; |
|
227 |
} |
|
228 |
||
229 |
List<File> javacBootClassPath; |
|
230 |
List<File> javacClassPath; |
|
231 |
List<File> javacSourcePath; |
|
232 |
List<String> javacOpts; |
|
233 |
List<File> javacFiles; |
|
234 |
boolean needHelp = false; |
|
235 |
||
236 |
// </editor-fold> |
|
237 |
||
238 |
// <editor-fold defaultstate="collapsed" desc="javac Plugin"> |
|
239 |
||
240 |
@Override |
|
241 |
public String getName() { |
|
242 |
return "doclint"; |
|
243 |
} |
|
244 |
||
245 |
@Override |
|
14955
8fe779a6d616
8005137: Rename DocLint.call to DocLint.init which overrides Plugin.init
mchung
parents:
14952
diff
changeset
|
246 |
public void init(JavacTask task, String... args) { |
14952 | 247 |
init(task, args, true); |
248 |
} |
|
249 |
||
250 |
// </editor-fold> |
|
251 |
||
252 |
// <editor-fold defaultstate="collapsed" desc="Embedding API"> |
|
253 |
||
254 |
public void init(JavacTask task, String[] args, boolean addTaskListener) { |
|
255 |
env = new Env(); |
|
256 |
for (int i = 0; i < args.length; i++) { |
|
257 |
String arg = args[i]; |
|
258 |
if (arg.equals(XMSGS_OPTION)) { |
|
259 |
env.messages.setOptions(null); |
|
260 |
} else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) { |
|
261 |
env.messages.setOptions(arg.substring(arg.indexOf(":") + 1)); |
|
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
15370
diff
changeset
|
262 |
} else if (arg.matches(XIMPLICIT_HEADERS + "[1-6]")) { |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
15370
diff
changeset
|
263 |
char ch = arg.charAt(arg.length() - 1); |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
15370
diff
changeset
|
264 |
env.setImplicitHeaders(Character.digit(ch, 10)); |
14952 | 265 |
} else |
266 |
throw new IllegalArgumentException(arg); |
|
267 |
} |
|
268 |
env.init(task); |
|
269 |
||
270 |
checker = new Checker(env); |
|
271 |
||
272 |
if (addTaskListener) { |
|
273 |
final DeclScanner ds = new DeclScanner() { |
|
274 |
@Override |
|
275 |
void visitDecl(Tree tree, Name name) { |
|
276 |
TreePath p = getCurrentPath(); |
|
277 |
DocCommentTree dc = env.trees.getDocCommentTree(p); |
|
278 |
||
279 |
checker.scan(dc, p); |
|
280 |
} |
|
281 |
}; |
|
282 |
||
283 |
TaskListener tl = new TaskListener() { |
|
284 |
@Override |
|
285 |
public void started(TaskEvent e) { |
|
20235
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
286 |
switch (e.getKind()) { |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
287 |
case ANALYZE: |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
288 |
CompilationUnitTree tree; |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
289 |
while ((tree = todo.poll()) != null) |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
290 |
ds.scan(tree, null); |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
291 |
break; |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
292 |
} |
14952 | 293 |
} |
294 |
||
295 |
@Override |
|
296 |
public void finished(TaskEvent e) { |
|
297 |
switch (e.getKind()) { |
|
20235
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
298 |
case PARSE: |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
299 |
todo.add(e.getCompilationUnit()); |
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
300 |
break; |
14952 | 301 |
} |
302 |
} |
|
20235
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
303 |
|
7834123e35dc
8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
jjg
parents:
19123
diff
changeset
|
304 |
Queue<CompilationUnitTree> todo = new LinkedList<CompilationUnitTree>(); |
14952 | 305 |
}; |
306 |
||
307 |
task.addTaskListener(tl); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
public void scan(TreePath p) { |
|
312 |
DocCommentTree dc = env.trees.getDocCommentTree(p); |
|
313 |
checker.scan(dc, p); |
|
314 |
} |
|
315 |
||
316 |
public void reportStats(PrintWriter out) { |
|
317 |
env.messages.reportStats(out); |
|
318 |
} |
|
319 |
||
320 |
// </editor-fold> |
|
321 |
||
322 |
Env env; |
|
323 |
Checker checker; |
|
324 |
||
325 |
public static boolean isValidOption(String opt) { |
|
326 |
if (opt.equals(XMSGS_OPTION)) |
|
327 |
return true; |
|
328 |
if (opt.startsWith(XMSGS_CUSTOM_PREFIX)) |
|
329 |
return Messages.Options.isValidOptions(opt.substring(XMSGS_CUSTOM_PREFIX.length())); |
|
330 |
return false; |
|
331 |
} |
|
332 |
||
18004
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
333 |
private String localize(String code, Object... args) { |
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
334 |
Messages m = (env != null) ? env.messages : new Messages(null); |
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
335 |
return m.localize(code, args); |
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
336 |
} |
dcf9d59b1635
8006615: [doclint] move remaining messages into resource bundle
jjg
parents:
16799
diff
changeset
|
337 |
|
14952 | 338 |
// <editor-fold defaultstate="collapsed" desc="DeclScanner"> |
339 |
||
340 |
static abstract class DeclScanner extends TreePathScanner<Void, Void> { |
|
341 |
abstract void visitDecl(Tree tree, Name name); |
|
342 |
||
343 |
@Override |
|
18908 | 344 |
public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { |
345 |
if (tree.getPackageName() != null) { |
|
346 |
visitDecl(tree, null); |
|
347 |
} |
|
348 |
return super.visitCompilationUnit(tree, ignore); |
|
349 |
} |
|
350 |
||
351 |
@Override |
|
14952 | 352 |
public Void visitClass(ClassTree tree, Void ignore) { |
353 |
visitDecl(tree, tree.getSimpleName()); |
|
354 |
return super.visitClass(tree, ignore); |
|
355 |
} |
|
356 |
||
357 |
@Override |
|
358 |
public Void visitMethod(MethodTree tree, Void ignore) { |
|
359 |
visitDecl(tree, tree.getName()); |
|
360 |
//return super.visitMethod(tree, ignore); |
|
361 |
return null; |
|
362 |
} |
|
363 |
||
364 |
@Override |
|
365 |
public Void visitVariable(VariableTree tree, Void ignore) { |
|
366 |
visitDecl(tree, tree.getName()); |
|
367 |
return super.visitVariable(tree, ignore); |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
// </editor-fold> |
|
372 |
||
373 |
} |