make/tools/crules/AbstractCodingRulesAnalyzer.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/make/tools/crules/AbstractCodingRulesAnalyzer.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +package crules;
    1.28 +
    1.29 +import java.text.MessageFormat;
    1.30 +import java.util.Locale;
    1.31 +import java.util.ResourceBundle;
    1.32 +
    1.33 +import javax.lang.model.element.TypeElement;
    1.34 +import javax.tools.JavaFileObject;
    1.35 +
    1.36 +import com.sun.source.tree.Tree;
    1.37 +import com.sun.source.util.JavacTask;
    1.38 +import com.sun.source.util.Plugin;
    1.39 +import com.sun.source.util.TaskEvent;
    1.40 +import com.sun.source.util.TaskListener;
    1.41 +import com.sun.source.util.Trees;
    1.42 +import com.sun.tools.javac.api.BasicJavacTask;
    1.43 +import com.sun.tools.javac.tree.JCTree;
    1.44 +import com.sun.tools.javac.tree.TreeScanner;
    1.45 +import com.sun.tools.javac.util.Context;
    1.46 +import com.sun.tools.javac.util.Log;
    1.47 +
    1.48 +import static com.sun.source.util.TaskEvent.Kind;
    1.49 +
    1.50 +public abstract class AbstractCodingRulesAnalyzer implements Plugin {
    1.51 +
    1.52 +    protected Log log;
    1.53 +    protected Trees trees;
    1.54 +    protected TreeScanner treeVisitor;
    1.55 +    protected Kind eventKind;
    1.56 +    protected Messages messages;
    1.57 +
    1.58 +    public void init(JavacTask task, String... args) {
    1.59 +        BasicJavacTask impl = (BasicJavacTask)task;
    1.60 +        Context context = impl.getContext();
    1.61 +        log = Log.instance(context);
    1.62 +        trees = Trees.instance(task);
    1.63 +        messages = new Messages();
    1.64 +        task.addTaskListener(new PostAnalyzeTaskListener());
    1.65 +    }
    1.66 +
    1.67 +    public class PostAnalyzeTaskListener implements TaskListener {
    1.68 +
    1.69 +        @Override
    1.70 +        public void started(TaskEvent taskEvent) {}
    1.71 +
    1.72 +        @Override
    1.73 +        public void finished(TaskEvent taskEvent) {
    1.74 +            if (taskEvent.getKind().equals(eventKind)) {
    1.75 +                TypeElement typeElem = taskEvent.getTypeElement();
    1.76 +                Tree tree = trees.getTree(typeElem);
    1.77 +                if (tree != null) {
    1.78 +                    JavaFileObject prevSource = log.currentSourceFile();
    1.79 +                    try {
    1.80 +                        log.useSource(taskEvent.getCompilationUnit().getSourceFile());
    1.81 +                        treeVisitor.scan((JCTree)tree);
    1.82 +                    } finally {
    1.83 +                        log.useSource(prevSource);
    1.84 +                    }
    1.85 +                }
    1.86 +            }
    1.87 +        }
    1.88 +    }
    1.89 +
    1.90 +    class Messages {
    1.91 +        ResourceBundle bundle;
    1.92 +
    1.93 +        Messages() {
    1.94 +            String name = getClass().getPackage().getName() + ".resources.crules";
    1.95 +            bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);
    1.96 +        }
    1.97 +
    1.98 +        public void error(JCTree tree, String code, Object... args) {
    1.99 +            String msg = (code == null) ? (String) args[0] : localize(code, args);
   1.100 +            log.error(tree, "proc.messager", msg.toString());
   1.101 +        }
   1.102 +
   1.103 +        private String localize(String code, Object... args) {
   1.104 +            String msg = bundle.getString(code);
   1.105 +            if (msg == null) {
   1.106 +                StringBuilder sb = new StringBuilder();
   1.107 +                sb.append("message file broken: code=").append(code);
   1.108 +                if (args.length > 0) {
   1.109 +                    sb.append(" arguments={0}");
   1.110 +                    for (int i = 1; i < args.length; i++) {
   1.111 +                        sb.append(", {").append(i).append("}");
   1.112 +                    }
   1.113 +                }
   1.114 +                msg = sb.toString();
   1.115 +            }
   1.116 +            return MessageFormat.format(msg, args);
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +}

mercurial