aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package crules; aoqi@0: aoqi@0: import java.text.MessageFormat; aoqi@0: import java.util.Locale; aoqi@0: import java.util.ResourceBundle; aoqi@0: aoqi@0: import javax.lang.model.element.TypeElement; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: aoqi@0: import com.sun.source.tree.Tree; aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.source.util.Plugin; aoqi@0: import com.sun.source.util.TaskEvent; aoqi@0: import com.sun.source.util.TaskListener; aoqi@0: import com.sun.source.util.Trees; aoqi@0: import com.sun.tools.javac.api.BasicJavacTask; aoqi@0: import com.sun.tools.javac.tree.JCTree; aoqi@0: import com.sun.tools.javac.tree.TreeScanner; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: import com.sun.tools.javac.util.Log; aoqi@0: aoqi@0: import static com.sun.source.util.TaskEvent.Kind; aoqi@0: aoqi@0: public abstract class AbstractCodingRulesAnalyzer implements Plugin { aoqi@0: aoqi@0: protected Log log; aoqi@0: protected Trees trees; aoqi@0: protected TreeScanner treeVisitor; aoqi@0: protected Kind eventKind; aoqi@0: protected Messages messages; aoqi@0: aoqi@0: public void init(JavacTask task, String... args) { aoqi@0: BasicJavacTask impl = (BasicJavacTask)task; aoqi@0: Context context = impl.getContext(); aoqi@0: log = Log.instance(context); aoqi@0: trees = Trees.instance(task); aoqi@0: messages = new Messages(); aoqi@0: task.addTaskListener(new PostAnalyzeTaskListener()); aoqi@0: } aoqi@0: aoqi@0: public class PostAnalyzeTaskListener implements TaskListener { aoqi@0: aoqi@0: @Override aoqi@0: public void started(TaskEvent taskEvent) {} aoqi@0: aoqi@0: @Override aoqi@0: public void finished(TaskEvent taskEvent) { aoqi@0: if (taskEvent.getKind().equals(eventKind)) { aoqi@0: TypeElement typeElem = taskEvent.getTypeElement(); aoqi@0: Tree tree = trees.getTree(typeElem); aoqi@0: if (tree != null) { aoqi@0: JavaFileObject prevSource = log.currentSourceFile(); aoqi@0: try { aoqi@0: log.useSource(taskEvent.getCompilationUnit().getSourceFile()); aoqi@0: treeVisitor.scan((JCTree)tree); aoqi@0: } finally { aoqi@0: log.useSource(prevSource); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class Messages { aoqi@0: ResourceBundle bundle; aoqi@0: aoqi@0: Messages() { aoqi@0: String name = getClass().getPackage().getName() + ".resources.crules"; aoqi@0: bundle = ResourceBundle.getBundle(name, Locale.ENGLISH); aoqi@0: } aoqi@0: aoqi@0: public void error(JCTree tree, String code, Object... args) { aoqi@0: String msg = (code == null) ? (String) args[0] : localize(code, args); aoqi@0: log.error(tree, "proc.messager", msg.toString()); aoqi@0: } aoqi@0: aoqi@0: private String localize(String code, Object... args) { aoqi@0: String msg = bundle.getString(code); aoqi@0: if (msg == null) { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: sb.append("message file broken: code=").append(code); aoqi@0: if (args.length > 0) { aoqi@0: sb.append(" arguments={0}"); aoqi@0: for (int i = 1; i < args.length; i++) { aoqi@0: sb.append(", {").append(i).append("}"); aoqi@0: } aoqi@0: } aoqi@0: msg = sb.toString(); aoqi@0: } aoqi@0: return MessageFormat.format(msg, args); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }