src/share/classes/com/sun/tools/doclint/DocLint.java

Thu, 28 Mar 2013 10:49:39 -0700

author
jjg
date
Thu, 28 Mar 2013 10:49:39 -0700
changeset 1668
991f11e13598
parent 1506
4a3cfc970c6f
child 1796
242bcad5be74
permissions
-rw-r--r--

8006346: doclint should make allowance for headers generated by standard doclet
Reviewed-by: mcimadamore

jjg@1455 1 /*
jjg@1506 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1455 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1455 4 *
jjg@1455 5 * This code is free software; you can redistribute it and/or modify it
jjg@1455 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1455 7 * published by the Free Software Foundation. Oracle designates this
jjg@1455 8 * particular file as subject to the "Classpath" exception as provided
jjg@1455 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1455 10 *
jjg@1455 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1455 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1455 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1455 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1455 15 * accompanied this code).
jjg@1455 16 *
jjg@1455 17 * You should have received a copy of the GNU General Public License version
jjg@1455 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1455 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1455 20 *
jjg@1455 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1455 22 * or visit www.oracle.com if you need additional information or have any
jjg@1455 23 * questions.
jjg@1455 24 */
jjg@1455 25
jjg@1455 26 package com.sun.tools.doclint;
jjg@1455 27
jjg@1455 28 import java.io.File;
jjg@1455 29 import java.io.IOException;
jjg@1455 30 import java.io.PrintWriter;
jjg@1455 31 import java.util.ArrayList;
jjg@1455 32 import java.util.List;
jjg@1668 33 import java.util.regex.Pattern;
jjg@1455 34
jjg@1455 35 import javax.lang.model.element.Name;
jjg@1455 36 import javax.tools.StandardLocation;
jjg@1455 37
jjg@1455 38 import com.sun.source.doctree.DocCommentTree;
jjg@1455 39 import com.sun.source.tree.ClassTree;
jjg@1455 40 import com.sun.source.tree.CompilationUnitTree;
jjg@1455 41 import com.sun.source.tree.MethodTree;
jjg@1455 42 import com.sun.source.tree.Tree;
jjg@1455 43 import com.sun.source.tree.VariableTree;
jjg@1455 44 import com.sun.source.util.JavacTask;
jjg@1455 45 import com.sun.source.util.Plugin;
jjg@1455 46 import com.sun.source.util.TaskEvent;
jjg@1455 47 import com.sun.source.util.TaskListener;
jjg@1455 48 import com.sun.source.util.TreePath;
jjg@1455 49 import com.sun.source.util.TreePathScanner;
jjg@1455 50 import com.sun.tools.javac.api.JavacTaskImpl;
jjg@1455 51 import com.sun.tools.javac.api.JavacTool;
jjg@1455 52 import com.sun.tools.javac.file.JavacFileManager;
jjg@1455 53 import com.sun.tools.javac.main.JavaCompiler;
jjg@1455 54 import com.sun.tools.javac.util.Context;
jjg@1455 55
jjg@1455 56 /**
jjg@1455 57 * Multi-function entry point for the doc check utility.
jjg@1455 58 *
jjg@1455 59 * This class can be invoked in the following ways:
jjg@1455 60 * <ul>
jjg@1455 61 * <li>From the command line
jjg@1455 62 * <li>From javac, as a plugin
jjg@1455 63 * <li>Directly, via a simple API
jjg@1455 64 * </ul>
jjg@1455 65 *
jjg@1455 66 * <p><b>This is NOT part of any supported API.
jjg@1455 67 * If you write code that depends on this, you do so at your own
jjg@1455 68 * risk. This code and its internal interfaces are subject to change
jjg@1455 69 * or deletion without notice.</b></p>
jjg@1455 70 */
jjg@1455 71 public class DocLint implements Plugin {
jjg@1455 72
jjg@1455 73 public static final String XMSGS_OPTION = "-Xmsgs";
jjg@1455 74 public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:";
jjg@1455 75 private static final String STATS = "-stats";
jjg@1668 76 public static final String XIMPLICIT_HEADERS = "-XimplicitHeaders:";
jjg@1455 77
jjg@1455 78 // <editor-fold defaultstate="collapsed" desc="Command-line entry point">
jjg@1455 79 public static void main(String... args) {
jjg@1455 80 try {
jjg@1455 81 new DocLint().run(args);
jjg@1455 82 } catch (BadArgs e) {
jjg@1455 83 System.err.println(e.getMessage());
jjg@1455 84 System.exit(1);
jjg@1455 85 } catch (IOException e) {
jjg@1455 86 System.err.println(e);
jjg@1455 87 System.exit(2);
jjg@1455 88 }
jjg@1455 89 }
jjg@1455 90
jjg@1455 91 // </editor-fold>
jjg@1455 92
jjg@1455 93 // <editor-fold defaultstate="collapsed" desc="Simple API">
jjg@1455 94
jjg@1455 95 public static class BadArgs extends Exception {
jjg@1455 96 private static final long serialVersionUID = 0;
jjg@1455 97 BadArgs(String code, Object... args) {
jjg@1455 98 this.code = code;
jjg@1455 99 this.args = args;
jjg@1455 100 }
jjg@1455 101
jjg@1455 102 final String code;
jjg@1455 103 final Object[] args;
jjg@1455 104 }
jjg@1455 105
jjg@1455 106 /**
jjg@1455 107 * Simple API entry point.
jjg@1455 108 */
jjg@1455 109 public void run(String... args) throws BadArgs, IOException {
jjg@1455 110 PrintWriter out = new PrintWriter(System.out);
jjg@1455 111 try {
jjg@1455 112 run(out, args);
jjg@1455 113 } finally {
jjg@1455 114 out.flush();
jjg@1455 115 }
jjg@1455 116 }
jjg@1455 117
jjg@1455 118 public void run(PrintWriter out, String... args) throws BadArgs, IOException {
jjg@1455 119 env = new Env();
jjg@1455 120 processArgs(args);
jjg@1455 121
jjg@1455 122 if (needHelp)
jjg@1455 123 showHelp(out);
jjg@1455 124
jjg@1455 125 if (javacFiles.isEmpty()) {
jjg@1455 126 if (!needHelp)
jjg@1506 127 out.println("no files given");
jjg@1455 128 }
jjg@1455 129
jjg@1455 130 JavacTool tool = JavacTool.create();
jjg@1455 131
jjg@1455 132 JavacFileManager fm = new JavacFileManager(new Context(), false, null);
jjg@1455 133 fm.setSymbolFileEnabled(false);
jjg@1455 134 fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath);
jjg@1455 135 fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath);
jjg@1455 136 fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath);
jjg@1455 137
jjg@1455 138 JavacTask task = tool.getTask(out, fm, null, javacOpts, null,
jjg@1455 139 fm.getJavaFileObjectsFromFiles(javacFiles));
jjg@1455 140 Iterable<? extends CompilationUnitTree> units = task.parse();
jjg@1455 141 ((JavacTaskImpl) task).enter();
jjg@1455 142
jjg@1455 143 env.init(task);
jjg@1455 144 checker = new Checker(env);
jjg@1455 145
jjg@1455 146 DeclScanner ds = new DeclScanner() {
jjg@1455 147 @Override
jjg@1455 148 void visitDecl(Tree tree, Name name) {
jjg@1455 149 TreePath p = getCurrentPath();
jjg@1455 150 DocCommentTree dc = env.trees.getDocCommentTree(p);
jjg@1455 151
jjg@1455 152 checker.scan(dc, p);
jjg@1455 153 }
jjg@1455 154 };
jjg@1455 155
jjg@1455 156 ds.scan(units, null);
jjg@1455 157
jjg@1455 158 reportStats(out);
jjg@1455 159
jjg@1455 160 Context ctx = ((JavacTaskImpl) task).getContext();
jjg@1455 161 JavaCompiler c = JavaCompiler.instance(ctx);
jjg@1455 162 c.printCount("error", c.errorCount());
jjg@1455 163 c.printCount("warn", c.warningCount());
jjg@1455 164 }
jjg@1455 165
jjg@1455 166 void processArgs(String... args) throws BadArgs {
jjg@1455 167 javacOpts = new ArrayList<String>();
jjg@1455 168 javacFiles = new ArrayList<File>();
jjg@1455 169
jjg@1455 170 if (args.length == 0)
jjg@1455 171 needHelp = true;
jjg@1455 172
jjg@1455 173 for (int i = 0; i < args.length; i++) {
jjg@1455 174 String arg = args[i];
jjg@1455 175 if (arg.matches("-Xmax(errs|warns)") && i + 1 < args.length) {
jjg@1455 176 if (args[++i].matches("[0-9]+")) {
jjg@1455 177 javacOpts.add(arg);
jjg@1455 178 javacOpts.add(args[i]);
jjg@1455 179 } else {
jjg@1455 180 throw new BadArgs("dc.bad.value.for.option", arg, args[i]);
jjg@1455 181 }
jjg@1455 182 } else if (arg.equals(STATS)) {
jjg@1455 183 env.messages.setStatsEnabled(true);
jjg@1506 184 } else if (arg.equals("-bootclasspath") && i + 1 < args.length) {
jjg@1455 185 javacBootClassPath = splitPath(args[++i]);
jjg@1506 186 } else if (arg.equals("-classpath") && i + 1 < args.length) {
jjg@1455 187 javacClassPath = splitPath(args[++i]);
jjg@1506 188 } else if (arg.equals("-sourcepath") && i + 1 < args.length) {
jjg@1455 189 javacSourcePath = splitPath(args[++i]);
jjg@1455 190 } else if (arg.equals(XMSGS_OPTION)) {
jjg@1455 191 env.messages.setOptions(null);
jjg@1455 192 } else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
jjg@1455 193 env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
jjg@1455 194 } else if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")
jjg@1455 195 || arg.equals("-?") || arg.equals("-usage")) {
jjg@1455 196 needHelp = true;
jjg@1455 197 } else if (arg.startsWith("-")) {
jjg@1455 198 throw new BadArgs("dc.bad.option", arg);
jjg@1455 199 } else {
jjg@1455 200 while (i < args.length)
jjg@1455 201 javacFiles.add(new File(args[i++]));
jjg@1455 202 }
jjg@1455 203 }
jjg@1455 204 }
jjg@1455 205
jjg@1455 206 void showHelp(PrintWriter out) {
jjg@1455 207 out.println("Usage:");
jjg@1455 208 out.println(" doclint [options] source-files...");
jjg@1455 209 out.println("");
jjg@1455 210 out.println("Options:");
jjg@1455 211 out.println(" -Xmsgs ");
jjg@1455 212 out.println(" Same as -Xmsgs:all");
jjg@1455 213 out.println(" -Xmsgs:values");
jjg@1455 214 out.println(" Specify categories of issues to be checked, where 'values'");
jjg@1455 215 out.println(" is a comma-separated list of any of the following:");
jjg@1455 216 out.println(" reference show places where comments contain incorrect");
jjg@1455 217 out.println(" references to Java source code elements");
jjg@1455 218 out.println(" syntax show basic syntax errors within comments");
jjg@1455 219 out.println(" html show issues with HTML tags and attributes");
jjg@1455 220 out.println(" accessibility show issues for accessibility");
jjg@1455 221 out.println(" missing show issues with missing documentation");
jjg@1455 222 out.println(" all all of the above");
jjg@1455 223 out.println(" Precede a value with '-' to negate it");
jjg@1455 224 out.println(" Categories may be qualified by one of:");
jjg@1455 225 out.println(" /public /protected /package /private");
jjg@1455 226 out.println(" For positive categories (not beginning with '-')");
jjg@1455 227 out.println(" the qualifier applies to that access level and above.");
jjg@1455 228 out.println(" For negative categories (beginning with '-')");
jjg@1455 229 out.println(" the qualifier applies to that access level and below.");
jjg@1455 230 out.println(" If a qualifier is missing, the category applies to");
jjg@1455 231 out.println(" all access levels.");
jjg@1455 232 out.println(" For example, -Xmsgs:all,-syntax/private");
jjg@1455 233 out.println(" This will enable all messages, except syntax errors");
jjg@1455 234 out.println(" in the doc comments of private methods.");
jjg@1455 235 out.println(" If no -Xmsgs options are provided, the default is");
jjg@1455 236 out.println(" equivalent to -Xmsgs:all/protected, meaning that");
jjg@1455 237 out.println(" all messages are reported for protected and public");
jjg@1455 238 out.println(" declarations only. ");
jjg@1506 239 out.println(" -stats");
jjg@1506 240 out.println(" Report statistics on the reported issues.");
jjg@1455 241 out.println(" -h -help --help -usage -?");
jjg@1455 242 out.println(" Show this message.");
jjg@1455 243 out.println("");
jjg@1455 244 out.println("The following javac options are also supported");
jjg@1455 245 out.println(" -bootclasspath, -classpath, -sourcepath, -Xmaxerrs, -Xmaxwarns");
jjg@1455 246 out.println("");
jjg@1455 247 out.println("To run doclint on part of a project, put the compiled classes for your");
jjg@1455 248 out.println("project on the classpath (or bootclasspath), then specify the source files");
jjg@1455 249 out.println("to be checked on the command line.");
jjg@1455 250 }
jjg@1455 251
jjg@1455 252 List<File> splitPath(String path) {
jjg@1455 253 List<File> files = new ArrayList<File>();
jjg@1506 254 for (String f: path.split(File.pathSeparator)) {
jjg@1455 255 if (f.length() > 0)
jjg@1455 256 files.add(new File(f));
jjg@1455 257 }
jjg@1455 258 return files;
jjg@1455 259 }
jjg@1455 260
jjg@1455 261 List<File> javacBootClassPath;
jjg@1455 262 List<File> javacClassPath;
jjg@1455 263 List<File> javacSourcePath;
jjg@1455 264 List<String> javacOpts;
jjg@1455 265 List<File> javacFiles;
jjg@1455 266 boolean needHelp = false;
jjg@1455 267
jjg@1455 268 // </editor-fold>
jjg@1455 269
jjg@1455 270 // <editor-fold defaultstate="collapsed" desc="javac Plugin">
jjg@1455 271
jjg@1455 272 @Override
jjg@1455 273 public String getName() {
jjg@1455 274 return "doclint";
jjg@1455 275 }
jjg@1455 276
jjg@1455 277 @Override
mchung@1458 278 public void init(JavacTask task, String... args) {
jjg@1455 279 init(task, args, true);
jjg@1455 280 }
jjg@1455 281
jjg@1455 282 // </editor-fold>
jjg@1455 283
jjg@1455 284 // <editor-fold defaultstate="collapsed" desc="Embedding API">
jjg@1455 285
jjg@1455 286 public void init(JavacTask task, String[] args, boolean addTaskListener) {
jjg@1455 287 env = new Env();
jjg@1455 288 for (int i = 0; i < args.length; i++) {
jjg@1455 289 String arg = args[i];
jjg@1455 290 if (arg.equals(XMSGS_OPTION)) {
jjg@1455 291 env.messages.setOptions(null);
jjg@1455 292 } else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
jjg@1455 293 env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
jjg@1668 294 } else if (arg.matches(XIMPLICIT_HEADERS + "[1-6]")) {
jjg@1668 295 char ch = arg.charAt(arg.length() - 1);
jjg@1668 296 env.setImplicitHeaders(Character.digit(ch, 10));
jjg@1455 297 } else
jjg@1455 298 throw new IllegalArgumentException(arg);
jjg@1455 299 }
jjg@1455 300 env.init(task);
jjg@1455 301
jjg@1455 302 checker = new Checker(env);
jjg@1455 303
jjg@1455 304 if (addTaskListener) {
jjg@1455 305 final DeclScanner ds = new DeclScanner() {
jjg@1455 306 @Override
jjg@1455 307 void visitDecl(Tree tree, Name name) {
jjg@1455 308 TreePath p = getCurrentPath();
jjg@1455 309 DocCommentTree dc = env.trees.getDocCommentTree(p);
jjg@1455 310
jjg@1455 311 checker.scan(dc, p);
jjg@1455 312 }
jjg@1455 313 };
jjg@1455 314
jjg@1455 315 TaskListener tl = new TaskListener() {
jjg@1455 316 @Override
jjg@1455 317 public void started(TaskEvent e) {
jjg@1455 318 return;
jjg@1455 319 }
jjg@1455 320
jjg@1455 321 @Override
jjg@1455 322 public void finished(TaskEvent e) {
jjg@1455 323 switch (e.getKind()) {
jjg@1455 324 case ENTER:
jjg@1455 325 ds.scan(e.getCompilationUnit(), null);
jjg@1455 326 }
jjg@1455 327 }
jjg@1455 328 };
jjg@1455 329
jjg@1455 330 task.addTaskListener(tl);
jjg@1455 331 }
jjg@1455 332 }
jjg@1455 333
jjg@1455 334 public void scan(TreePath p) {
jjg@1455 335 DocCommentTree dc = env.trees.getDocCommentTree(p);
jjg@1455 336 checker.scan(dc, p);
jjg@1455 337 }
jjg@1455 338
jjg@1455 339 public void reportStats(PrintWriter out) {
jjg@1455 340 env.messages.reportStats(out);
jjg@1455 341 }
jjg@1455 342
jjg@1455 343 // </editor-fold>
jjg@1455 344
jjg@1455 345 Env env;
jjg@1455 346 Checker checker;
jjg@1455 347
jjg@1455 348 public static boolean isValidOption(String opt) {
jjg@1455 349 if (opt.equals(XMSGS_OPTION))
jjg@1455 350 return true;
jjg@1455 351 if (opt.startsWith(XMSGS_CUSTOM_PREFIX))
jjg@1455 352 return Messages.Options.isValidOptions(opt.substring(XMSGS_CUSTOM_PREFIX.length()));
jjg@1455 353 return false;
jjg@1455 354 }
jjg@1455 355
jjg@1455 356 // <editor-fold defaultstate="collapsed" desc="DeclScanner">
jjg@1455 357
jjg@1455 358 static abstract class DeclScanner extends TreePathScanner<Void, Void> {
jjg@1455 359 abstract void visitDecl(Tree tree, Name name);
jjg@1455 360
jjg@1455 361 @Override
jjg@1455 362 public Void visitClass(ClassTree tree, Void ignore) {
jjg@1455 363 visitDecl(tree, tree.getSimpleName());
jjg@1455 364 return super.visitClass(tree, ignore);
jjg@1455 365 }
jjg@1455 366
jjg@1455 367 @Override
jjg@1455 368 public Void visitMethod(MethodTree tree, Void ignore) {
jjg@1455 369 visitDecl(tree, tree.getName());
jjg@1455 370 //return super.visitMethod(tree, ignore);
jjg@1455 371 return null;
jjg@1455 372 }
jjg@1455 373
jjg@1455 374 @Override
jjg@1455 375 public Void visitVariable(VariableTree tree, Void ignore) {
jjg@1455 376 visitDecl(tree, tree.getName());
jjg@1455 377 return super.visitVariable(tree, ignore);
jjg@1455 378 }
jjg@1455 379 }
jjg@1455 380
jjg@1455 381 // </editor-fold>
jjg@1455 382
jjg@1455 383 }

mercurial