Thu, 04 Jul 2013 10:35:33 +0100
8009924: some langtools tools do not accept -cp as an alias for -classpath
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2012, 2013, 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 */
26 package com.sun.tools.doclint;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.regex.Pattern;
35 import javax.lang.model.element.Name;
36 import javax.tools.StandardLocation;
38 import com.sun.source.doctree.DocCommentTree;
39 import com.sun.source.tree.ClassTree;
40 import com.sun.source.tree.CompilationUnitTree;
41 import com.sun.source.tree.MethodTree;
42 import com.sun.source.tree.Tree;
43 import com.sun.source.tree.VariableTree;
44 import com.sun.source.util.JavacTask;
45 import com.sun.source.util.Plugin;
46 import com.sun.source.util.TaskEvent;
47 import com.sun.source.util.TaskListener;
48 import com.sun.source.util.TreePath;
49 import com.sun.source.util.TreePathScanner;
50 import com.sun.tools.javac.api.JavacTaskImpl;
51 import com.sun.tools.javac.api.JavacTool;
52 import com.sun.tools.javac.file.JavacFileManager;
53 import com.sun.tools.javac.main.JavaCompiler;
54 import com.sun.tools.javac.util.Context;
56 /**
57 * Multi-function entry point for the doc check utility.
58 *
59 * This class can be invoked in the following ways:
60 * <ul>
61 * <li>From the command line
62 * <li>From javac, as a plugin
63 * <li>Directly, via a simple API
64 * </ul>
65 *
66 * <p><b>This is NOT part of any supported API.
67 * If you write code that depends on this, you do so at your own
68 * risk. This code and its internal interfaces are subject to change
69 * or deletion without notice.</b></p>
70 */
71 public class DocLint implements Plugin {
73 public static final String XMSGS_OPTION = "-Xmsgs";
74 public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:";
75 private static final String STATS = "-stats";
76 public static final String XIMPLICIT_HEADERS = "-XimplicitHeaders:";
78 // <editor-fold defaultstate="collapsed" desc="Command-line entry point">
79 public static void main(String... args) {
80 DocLint dl = new DocLint();
81 try {
82 dl.run(args);
83 } catch (BadArgs e) {
84 System.err.println(e.getMessage());
85 System.exit(1);
86 } catch (IOException e) {
87 System.err.println(dl.localize("dc.main.ioerror", e.getLocalizedMessage()));
88 System.exit(2);
89 }
90 }
92 // </editor-fold>
94 // <editor-fold defaultstate="collapsed" desc="Simple API">
96 public class BadArgs extends Exception {
97 private static final long serialVersionUID = 0;
98 BadArgs(String code, Object... args) {
99 super(localize(code, args));
100 this.code = code;
101 this.args = args;
102 }
104 final String code;
105 final Object[] args;
106 }
108 /**
109 * Simple API entry point.
110 */
111 public void run(String... args) throws BadArgs, IOException {
112 PrintWriter out = new PrintWriter(System.out);
113 try {
114 run(out, args);
115 } finally {
116 out.flush();
117 }
118 }
120 public void run(PrintWriter out, String... args) throws BadArgs, IOException {
121 env = new Env();
122 processArgs(args);
124 if (needHelp)
125 showHelp(out);
127 if (javacFiles.isEmpty()) {
128 if (!needHelp)
129 out.println(localize("dc.main.no.files.given"));
130 }
132 JavacTool tool = JavacTool.create();
134 JavacFileManager fm = new JavacFileManager(new Context(), false, null);
135 fm.setSymbolFileEnabled(false);
136 fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath);
137 fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath);
138 fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath);
140 JavacTask task = tool.getTask(out, fm, null, javacOpts, null,
141 fm.getJavaFileObjectsFromFiles(javacFiles));
142 Iterable<? extends CompilationUnitTree> units = task.parse();
143 ((JavacTaskImpl) task).enter();
145 env.init(task);
146 checker = new Checker(env);
148 DeclScanner ds = new DeclScanner() {
149 @Override
150 void visitDecl(Tree tree, Name name) {
151 TreePath p = getCurrentPath();
152 DocCommentTree dc = env.trees.getDocCommentTree(p);
154 checker.scan(dc, p);
155 }
156 };
158 ds.scan(units, null);
160 reportStats(out);
162 Context ctx = ((JavacTaskImpl) task).getContext();
163 JavaCompiler c = JavaCompiler.instance(ctx);
164 c.printCount("error", c.errorCount());
165 c.printCount("warn", c.warningCount());
166 }
168 void processArgs(String... args) throws BadArgs {
169 javacOpts = new ArrayList<String>();
170 javacFiles = new ArrayList<File>();
172 if (args.length == 0)
173 needHelp = true;
175 for (int i = 0; i < args.length; i++) {
176 String arg = args[i];
177 if (arg.matches("-Xmax(errs|warns)") && i + 1 < args.length) {
178 if (args[++i].matches("[0-9]+")) {
179 javacOpts.add(arg);
180 javacOpts.add(args[i]);
181 } else {
182 throw new BadArgs("dc.bad.value.for.option", arg, args[i]);
183 }
184 } else if (arg.equals(STATS)) {
185 env.messages.setStatsEnabled(true);
186 } else if (arg.equals("-bootclasspath") && i + 1 < args.length) {
187 javacBootClassPath = splitPath(args[++i]);
188 } else if (arg.equals("-classpath") && i + 1 < args.length) {
189 javacClassPath = splitPath(args[++i]);
190 } else if (arg.equals("-cp") && i + 1 < args.length) {
191 javacClassPath = splitPath(args[++i]);
192 } else if (arg.equals("-sourcepath") && i + 1 < args.length) {
193 javacSourcePath = splitPath(args[++i]);
194 } else if (arg.equals(XMSGS_OPTION)) {
195 env.messages.setOptions(null);
196 } else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
197 env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
198 } else if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")
199 || arg.equals("-?") || arg.equals("-usage")) {
200 needHelp = true;
201 } else if (arg.startsWith("-")) {
202 throw new BadArgs("dc.bad.option", arg);
203 } else {
204 while (i < args.length)
205 javacFiles.add(new File(args[i++]));
206 }
207 }
208 }
210 void showHelp(PrintWriter out) {
211 String msg = localize("dc.main.usage");
212 for (String line: msg.split("\n"))
213 out.println(line);
214 }
216 List<File> splitPath(String path) {
217 List<File> files = new ArrayList<File>();
218 for (String f: path.split(File.pathSeparator)) {
219 if (f.length() > 0)
220 files.add(new File(f));
221 }
222 return files;
223 }
225 List<File> javacBootClassPath;
226 List<File> javacClassPath;
227 List<File> javacSourcePath;
228 List<String> javacOpts;
229 List<File> javacFiles;
230 boolean needHelp = false;
232 // </editor-fold>
234 // <editor-fold defaultstate="collapsed" desc="javac Plugin">
236 @Override
237 public String getName() {
238 return "doclint";
239 }
241 @Override
242 public void init(JavacTask task, String... args) {
243 init(task, args, true);
244 }
246 // </editor-fold>
248 // <editor-fold defaultstate="collapsed" desc="Embedding API">
250 public void init(JavacTask task, String[] args, boolean addTaskListener) {
251 env = new Env();
252 for (int i = 0; i < args.length; i++) {
253 String arg = args[i];
254 if (arg.equals(XMSGS_OPTION)) {
255 env.messages.setOptions(null);
256 } else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
257 env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
258 } else if (arg.matches(XIMPLICIT_HEADERS + "[1-6]")) {
259 char ch = arg.charAt(arg.length() - 1);
260 env.setImplicitHeaders(Character.digit(ch, 10));
261 } else
262 throw new IllegalArgumentException(arg);
263 }
264 env.init(task);
266 checker = new Checker(env);
268 if (addTaskListener) {
269 final DeclScanner ds = new DeclScanner() {
270 @Override
271 void visitDecl(Tree tree, Name name) {
272 TreePath p = getCurrentPath();
273 DocCommentTree dc = env.trees.getDocCommentTree(p);
275 checker.scan(dc, p);
276 }
277 };
279 TaskListener tl = new TaskListener() {
280 @Override
281 public void started(TaskEvent e) {
282 return;
283 }
285 @Override
286 public void finished(TaskEvent e) {
287 switch (e.getKind()) {
288 case ENTER:
289 ds.scan(e.getCompilationUnit(), null);
290 }
291 }
292 };
294 task.addTaskListener(tl);
295 }
296 }
298 public void scan(TreePath p) {
299 DocCommentTree dc = env.trees.getDocCommentTree(p);
300 checker.scan(dc, p);
301 }
303 public void reportStats(PrintWriter out) {
304 env.messages.reportStats(out);
305 }
307 // </editor-fold>
309 Env env;
310 Checker checker;
312 public static boolean isValidOption(String opt) {
313 if (opt.equals(XMSGS_OPTION))
314 return true;
315 if (opt.startsWith(XMSGS_CUSTOM_PREFIX))
316 return Messages.Options.isValidOptions(opt.substring(XMSGS_CUSTOM_PREFIX.length()));
317 return false;
318 }
320 private String localize(String code, Object... args) {
321 Messages m = (env != null) ? env.messages : new Messages(null);
322 return m.localize(code, args);
323 }
325 // <editor-fold defaultstate="collapsed" desc="DeclScanner">
327 static abstract class DeclScanner extends TreePathScanner<Void, Void> {
328 abstract void visitDecl(Tree tree, Name name);
330 @Override
331 public Void visitClass(ClassTree tree, Void ignore) {
332 visitDecl(tree, tree.getSimpleName());
333 return super.visitClass(tree, ignore);
334 }
336 @Override
337 public Void visitMethod(MethodTree tree, Void ignore) {
338 visitDecl(tree, tree.getName());
339 //return super.visitMethod(tree, ignore);
340 return null;
341 }
343 @Override
344 public Void visitVariable(VariableTree tree, Void ignore) {
345 visitDecl(tree, tree.getName());
346 return super.visitVariable(tree, ignore);
347 }
348 }
350 // </editor-fold>
352 }