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

Tue, 24 Sep 2013 14:20:33 -0700

author
mfang
date
Tue, 24 Sep 2013 14:20:33 -0700
changeset 2057
1332a99572c5
parent 2033
fdfbc5f0c4ed
child 2169
667843bd2193
permissions
-rw-r--r--

8025215: jdk8 l10n resource file translation update 4
Reviewed-by: naoto, yhuang

     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.HashSet;
    33 import java.util.LinkedList;
    34 import java.util.List;
    35 import java.util.Queue;
    36 import java.util.Set;
    38 import javax.lang.model.element.Name;
    39 import javax.tools.JavaFileObject;
    40 import javax.tools.StandardLocation;
    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;
    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 {
    77     public static final String XMSGS_OPTION = "-Xmsgs";
    78     public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:";
    79     private static final String STATS = "-stats";
    80     public static final String XIMPLICIT_HEADERS = "-XimplicitHeaders:";
    82     // <editor-fold defaultstate="collapsed" desc="Command-line entry point">
    83     public static void main(String... args) {
    84         DocLint dl = new DocLint();
    85         try {
    86             dl.run(args);
    87         } catch (BadArgs e) {
    88             System.err.println(e.getMessage());
    89             System.exit(1);
    90         } catch (IOException e) {
    91             System.err.println(dl.localize("dc.main.ioerror", e.getLocalizedMessage()));
    92             System.exit(2);
    93         }
    94     }
    96     // </editor-fold>
    98     // <editor-fold defaultstate="collapsed" desc="Simple API">
   100     public class BadArgs extends Exception {
   101         private static final long serialVersionUID = 0;
   102         BadArgs(String code, Object... args) {
   103             super(localize(code, args));
   104             this.code = code;
   105             this.args = args;
   106         }
   108         final String code;
   109         final Object[] args;
   110     }
   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     }
   124     public void run(PrintWriter out, String... args) throws BadArgs, IOException {
   125         env = new Env();
   126         processArgs(args);
   128         if (needHelp)
   129             showHelp(out);
   131         if (javacFiles.isEmpty()) {
   132             if (!needHelp)
   133                 out.println(localize("dc.main.no.files.given"));
   134         }
   136         JavacTool tool = JavacTool.create();
   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);
   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();
   149         env.init(task);
   150         checker = new Checker(env);
   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);
   158                 checker.scan(dc, p);
   159             }
   160         };
   162         ds.scan(units, null);
   164         reportStats(out);
   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     }
   172     void processArgs(String... args) throws BadArgs {
   173         javacOpts = new ArrayList<>();
   174         javacFiles = new ArrayList<>();
   176         if (args.length == 0)
   177             needHelp = true;
   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);
   190             } else if (arg.equals("-bootclasspath") && i + 1 < args.length) {
   191                 javacBootClassPath = splitPath(args[++i]);
   192             } else if (arg.equals("-classpath") && i + 1 < args.length) {
   193                 javacClassPath = splitPath(args[++i]);
   194             } else if (arg.equals("-cp") && i + 1 < args.length) {
   195                 javacClassPath = splitPath(args[++i]);
   196             } else if (arg.equals("-sourcepath") && i + 1 < args.length) {
   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     }
   214     void showHelp(PrintWriter out) {
   215         String msg = localize("dc.main.usage");
   216         for (String line: msg.split("\n"))
   217             out.println(line);
   218     }
   220     List<File> splitPath(String path) {
   221         List<File> files = new ArrayList<>();
   222         for (String f: path.split(File.pathSeparator)) {
   223             if (f.length() > 0)
   224                 files.add(new File(f));
   225         }
   226         return files;
   227     }
   229     List<File> javacBootClassPath;
   230     List<File> javacClassPath;
   231     List<File> javacSourcePath;
   232     List<String> javacOpts;
   233     List<File> javacFiles;
   234     boolean needHelp = false;
   236     // </editor-fold>
   238     // <editor-fold defaultstate="collapsed" desc="javac Plugin">
   240     @Override
   241     public String getName() {
   242         return "doclint";
   243     }
   245     @Override
   246     public void init(JavacTask task, String... args) {
   247         init(task, args, true);
   248     }
   250     // </editor-fold>
   252     // <editor-fold defaultstate="collapsed" desc="Embedding API">
   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));
   262             } else if (arg.matches(XIMPLICIT_HEADERS + "[1-6]")) {
   263                 char ch = arg.charAt(arg.length() - 1);
   264                 env.setImplicitHeaders(Character.digit(ch, 10));
   265             } else
   266                 throw new IllegalArgumentException(arg);
   267         }
   268         env.init(task);
   270         checker = new Checker(env);
   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);
   279                     checker.scan(dc, p);
   280                 }
   281             };
   283             TaskListener tl = new TaskListener() {
   284                 @Override
   285                 public void started(TaskEvent e) {
   286                     switch (e.getKind()) {
   287                         case ANALYZE:
   288                             CompilationUnitTree tree;
   289                             while ((tree = todo.poll()) != null)
   290                                 ds.scan(tree, null);
   291                             break;
   292                     }
   293                 }
   295                 @Override
   296                 public void finished(TaskEvent e) {
   297                     switch (e.getKind()) {
   298                         case PARSE:
   299                             todo.add(e.getCompilationUnit());
   300                             break;
   301                     }
   302                 }
   304                 Queue<CompilationUnitTree> todo = new LinkedList<CompilationUnitTree>();
   305             };
   307             task.addTaskListener(tl);
   308         }
   309     }
   311     public void scan(TreePath p) {
   312         DocCommentTree dc = env.trees.getDocCommentTree(p);
   313         checker.scan(dc, p);
   314     }
   316     public void reportStats(PrintWriter out) {
   317         env.messages.reportStats(out);
   318     }
   320     // </editor-fold>
   322     Env env;
   323     Checker checker;
   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     }
   333     private String localize(String code, Object... args) {
   334         Messages m = (env != null) ? env.messages : new Messages(null);
   335         return m.localize(code, args);
   336     }
   338     // <editor-fold defaultstate="collapsed" desc="DeclScanner">
   340     static abstract class DeclScanner extends TreePathScanner<Void, Void> {
   341         abstract void visitDecl(Tree tree, Name name);
   343         @Override
   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         }
   351         @Override
   352         public Void visitClass(ClassTree tree, Void ignore) {
   353             visitDecl(tree, tree.getSimpleName());
   354             return super.visitClass(tree, ignore);
   355         }
   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         }
   364         @Override
   365         public Void visitVariable(VariableTree tree, Void ignore) {
   366             visitDecl(tree, tree.getName());
   367             return super.visitVariable(tree, ignore);
   368         }
   369     }
   371     // </editor-fold>
   373 }

mercurial