make/tools/crules/AbstractCodingRulesAnalyzer.java

Thu, 22 Jan 2015 15:55:59 -0800

author
asaha
date
Thu, 22 Jan 2015 15:55:59 -0800
changeset 2759
77d7dd7f35d6
parent 0
959103a6100f
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 package crules;
aoqi@0 25
aoqi@0 26 import java.text.MessageFormat;
aoqi@0 27 import java.util.Locale;
aoqi@0 28 import java.util.ResourceBundle;
aoqi@0 29
aoqi@0 30 import javax.lang.model.element.TypeElement;
aoqi@0 31 import javax.tools.JavaFileObject;
aoqi@0 32
aoqi@0 33 import com.sun.source.tree.Tree;
aoqi@0 34 import com.sun.source.util.JavacTask;
aoqi@0 35 import com.sun.source.util.Plugin;
aoqi@0 36 import com.sun.source.util.TaskEvent;
aoqi@0 37 import com.sun.source.util.TaskListener;
aoqi@0 38 import com.sun.source.util.Trees;
aoqi@0 39 import com.sun.tools.javac.api.BasicJavacTask;
aoqi@0 40 import com.sun.tools.javac.tree.JCTree;
aoqi@0 41 import com.sun.tools.javac.tree.TreeScanner;
aoqi@0 42 import com.sun.tools.javac.util.Context;
aoqi@0 43 import com.sun.tools.javac.util.Log;
aoqi@0 44
aoqi@0 45 import static com.sun.source.util.TaskEvent.Kind;
aoqi@0 46
aoqi@0 47 public abstract class AbstractCodingRulesAnalyzer implements Plugin {
aoqi@0 48
aoqi@0 49 protected Log log;
aoqi@0 50 protected Trees trees;
aoqi@0 51 protected TreeScanner treeVisitor;
aoqi@0 52 protected Kind eventKind;
aoqi@0 53 protected Messages messages;
aoqi@0 54
aoqi@0 55 public void init(JavacTask task, String... args) {
aoqi@0 56 BasicJavacTask impl = (BasicJavacTask)task;
aoqi@0 57 Context context = impl.getContext();
aoqi@0 58 log = Log.instance(context);
aoqi@0 59 trees = Trees.instance(task);
aoqi@0 60 messages = new Messages();
aoqi@0 61 task.addTaskListener(new PostAnalyzeTaskListener());
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 public class PostAnalyzeTaskListener implements TaskListener {
aoqi@0 65
aoqi@0 66 @Override
aoqi@0 67 public void started(TaskEvent taskEvent) {}
aoqi@0 68
aoqi@0 69 @Override
aoqi@0 70 public void finished(TaskEvent taskEvent) {
aoqi@0 71 if (taskEvent.getKind().equals(eventKind)) {
aoqi@0 72 TypeElement typeElem = taskEvent.getTypeElement();
aoqi@0 73 Tree tree = trees.getTree(typeElem);
aoqi@0 74 if (tree != null) {
aoqi@0 75 JavaFileObject prevSource = log.currentSourceFile();
aoqi@0 76 try {
aoqi@0 77 log.useSource(taskEvent.getCompilationUnit().getSourceFile());
aoqi@0 78 treeVisitor.scan((JCTree)tree);
aoqi@0 79 } finally {
aoqi@0 80 log.useSource(prevSource);
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 class Messages {
aoqi@0 88 ResourceBundle bundle;
aoqi@0 89
aoqi@0 90 Messages() {
aoqi@0 91 String name = getClass().getPackage().getName() + ".resources.crules";
aoqi@0 92 bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 public void error(JCTree tree, String code, Object... args) {
aoqi@0 96 String msg = (code == null) ? (String) args[0] : localize(code, args);
aoqi@0 97 log.error(tree, "proc.messager", msg.toString());
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 private String localize(String code, Object... args) {
aoqi@0 101 String msg = bundle.getString(code);
aoqi@0 102 if (msg == null) {
aoqi@0 103 StringBuilder sb = new StringBuilder();
aoqi@0 104 sb.append("message file broken: code=").append(code);
aoqi@0 105 if (args.length > 0) {
aoqi@0 106 sb.append(" arguments={0}");
aoqi@0 107 for (int i = 1; i < args.length; i++) {
aoqi@0 108 sb.append(", {").append(i).append("}");
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111 msg = sb.toString();
aoqi@0 112 }
aoqi@0 113 return MessageFormat.format(msg, args);
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 }

mercurial