Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/make/tools/crules/AbstractCodingRulesAnalyzer.java
32285 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package crules;2425import java.text.MessageFormat;26import java.util.Locale;27import java.util.ResourceBundle;2829import javax.lang.model.element.TypeElement;30import javax.tools.JavaFileObject;3132import com.sun.source.tree.Tree;33import com.sun.source.util.JavacTask;34import com.sun.source.util.Plugin;35import com.sun.source.util.TaskEvent;36import com.sun.source.util.TaskListener;37import com.sun.source.util.Trees;38import com.sun.tools.javac.api.BasicJavacTask;39import com.sun.tools.javac.tree.JCTree;40import com.sun.tools.javac.tree.TreeScanner;41import com.sun.tools.javac.util.Context;42import com.sun.tools.javac.util.Log;4344import static com.sun.source.util.TaskEvent.Kind;4546public abstract class AbstractCodingRulesAnalyzer implements Plugin {4748protected Log log;49protected Trees trees;50protected TreeScanner treeVisitor;51protected Kind eventKind;52protected Messages messages;5354public void init(JavacTask task, String... args) {55BasicJavacTask impl = (BasicJavacTask)task;56Context context = impl.getContext();57log = Log.instance(context);58trees = Trees.instance(task);59messages = new Messages();60task.addTaskListener(new PostAnalyzeTaskListener());61}6263public class PostAnalyzeTaskListener implements TaskListener {6465@Override66public void started(TaskEvent taskEvent) {}6768@Override69public void finished(TaskEvent taskEvent) {70if (taskEvent.getKind().equals(eventKind)) {71TypeElement typeElem = taskEvent.getTypeElement();72Tree tree = trees.getTree(typeElem);73if (tree != null) {74JavaFileObject prevSource = log.currentSourceFile();75try {76log.useSource(taskEvent.getCompilationUnit().getSourceFile());77treeVisitor.scan((JCTree)tree);78} finally {79log.useSource(prevSource);80}81}82}83}84}8586class Messages {87ResourceBundle bundle;8889Messages() {90String name = getClass().getPackage().getName() + ".resources.crules";91bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);92}9394public void error(JCTree tree, String code, Object... args) {95String msg = (code == null) ? (String) args[0] : localize(code, args);96log.error(tree, "proc.messager", msg.toString());97}9899private String localize(String code, Object... args) {100String msg = bundle.getString(code);101if (msg == null) {102StringBuilder sb = new StringBuilder();103sb.append("message file broken: code=").append(code);104if (args.length > 0) {105sb.append(" arguments={0}");106for (int i = 1; i < args.length; i++) {107sb.append(", {").append(i).append("}");108}109}110msg = sb.toString();111}112return MessageFormat.format(msg, args);113}114}115116}117118119