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

Mon, 03 Jun 2013 17:09:26 -0700

author
jjg
date
Mon, 03 Jun 2013 17:09:26 -0700
changeset 1796
242bcad5be74
parent 1668
991f11e13598
child 1885
d6158f8d7235
permissions
-rw-r--r--

8006615: [doclint] move remaining messages into resource bundle
Reviewed-by: mcimadamore, vromero

     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("-sourcepath") && i + 1 < args.length) {
   191                 javacSourcePath = splitPath(args[++i]);
   192             } else if (arg.equals(XMSGS_OPTION)) {
   193                 env.messages.setOptions(null);
   194             } else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
   195                 env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
   196             } else if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")
   197                     || arg.equals("-?") || arg.equals("-usage")) {
   198                 needHelp = true;
   199             } else if (arg.startsWith("-")) {
   200                 throw new BadArgs("dc.bad.option", arg);
   201             } else {
   202                 while (i < args.length)
   203                     javacFiles.add(new File(args[i++]));
   204             }
   205         }
   206     }
   208     void showHelp(PrintWriter out) {
   209         String msg = localize("dc.main.usage");
   210         for (String line: msg.split("\n"))
   211             out.println(line);
   212     }
   214     List<File> splitPath(String path) {
   215         List<File> files = new ArrayList<File>();
   216         for (String f: path.split(File.pathSeparator)) {
   217             if (f.length() > 0)
   218                 files.add(new File(f));
   219         }
   220         return files;
   221     }
   223     List<File> javacBootClassPath;
   224     List<File> javacClassPath;
   225     List<File> javacSourcePath;
   226     List<String> javacOpts;
   227     List<File> javacFiles;
   228     boolean needHelp = false;
   230     // </editor-fold>
   232     // <editor-fold defaultstate="collapsed" desc="javac Plugin">
   234     @Override
   235     public String getName() {
   236         return "doclint";
   237     }
   239     @Override
   240     public void init(JavacTask task, String... args) {
   241         init(task, args, true);
   242     }
   244     // </editor-fold>
   246     // <editor-fold defaultstate="collapsed" desc="Embedding API">
   248     public void init(JavacTask task, String[] args, boolean addTaskListener) {
   249         env = new Env();
   250         for (int i = 0; i < args.length; i++) {
   251             String arg = args[i];
   252             if (arg.equals(XMSGS_OPTION)) {
   253                 env.messages.setOptions(null);
   254             } else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
   255                 env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
   256             } else if (arg.matches(XIMPLICIT_HEADERS + "[1-6]")) {
   257                 char ch = arg.charAt(arg.length() - 1);
   258                 env.setImplicitHeaders(Character.digit(ch, 10));
   259             } else
   260                 throw new IllegalArgumentException(arg);
   261         }
   262         env.init(task);
   264         checker = new Checker(env);
   266         if (addTaskListener) {
   267             final DeclScanner ds = new DeclScanner() {
   268                 @Override
   269                 void visitDecl(Tree tree, Name name) {
   270                     TreePath p = getCurrentPath();
   271                     DocCommentTree dc = env.trees.getDocCommentTree(p);
   273                     checker.scan(dc, p);
   274                 }
   275             };
   277             TaskListener tl = new TaskListener() {
   278                 @Override
   279                 public void started(TaskEvent e) {
   280                     return;
   281                 }
   283                 @Override
   284                 public void finished(TaskEvent e) {
   285                     switch (e.getKind()) {
   286                         case ENTER:
   287                             ds.scan(e.getCompilationUnit(), null);
   288                     }
   289                 }
   290             };
   292             task.addTaskListener(tl);
   293         }
   294     }
   296     public void scan(TreePath p) {
   297         DocCommentTree dc = env.trees.getDocCommentTree(p);
   298         checker.scan(dc, p);
   299     }
   301     public void reportStats(PrintWriter out) {
   302         env.messages.reportStats(out);
   303     }
   305     // </editor-fold>
   307     Env env;
   308     Checker checker;
   310     public static boolean isValidOption(String opt) {
   311         if (opt.equals(XMSGS_OPTION))
   312            return true;
   313         if (opt.startsWith(XMSGS_CUSTOM_PREFIX))
   314            return Messages.Options.isValidOptions(opt.substring(XMSGS_CUSTOM_PREFIX.length()));
   315         return false;
   316     }
   318     private String localize(String code, Object... args) {
   319         Messages m = (env != null) ? env.messages : new Messages(null);
   320         return m.localize(code, args);
   321     }
   323     // <editor-fold defaultstate="collapsed" desc="DeclScanner">
   325     static abstract class DeclScanner extends TreePathScanner<Void, Void> {
   326         abstract void visitDecl(Tree tree, Name name);
   328         @Override
   329         public Void visitClass(ClassTree tree, Void ignore) {
   330             visitDecl(tree, tree.getSimpleName());
   331             return super.visitClass(tree, ignore);
   332         }
   334         @Override
   335         public Void visitMethod(MethodTree tree, Void ignore) {
   336             visitDecl(tree, tree.getName());
   337             //return super.visitMethod(tree, ignore);
   338             return null;
   339         }
   341         @Override
   342         public Void visitVariable(VariableTree tree, Void ignore) {
   343             visitDecl(tree, tree.getName());
   344             return super.visitVariable(tree, ignore);
   345         }
   346     }
   348     // </editor-fold>
   350 }

mercurial