AbstractCodingRulesAnalyzer.java revision 2601:8e638f046bf0
1/* 2 * Copyright (c) 2013, 2014, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24package crules; 25 26import java.text.MessageFormat; 27import java.util.Locale; 28import java.util.ResourceBundle; 29 30import com.sun.source.util.JavacTask; 31import com.sun.tools.javac.api.BasicJavacTask; 32import com.sun.tools.javac.code.Symtab; 33import com.sun.tools.javac.model.JavacElements; 34import com.sun.tools.javac.model.JavacTypes; 35import com.sun.tools.javac.tree.JCTree; 36import com.sun.tools.javac.tree.TreeScanner; 37import com.sun.tools.javac.util.Context; 38import com.sun.tools.javac.util.JCDiagnostic; 39import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; 40import com.sun.tools.javac.util.Log; 41import com.sun.tools.javac.util.Options; 42import com.sun.tools.javac.util.RawDiagnosticFormatter; 43 44import static com.sun.source.util.TaskEvent.Kind; 45 46public abstract class AbstractCodingRulesAnalyzer { 47 48 private final Log log; 49 private final boolean rawDiagnostics; 50 private final JCDiagnostic.Factory diags; 51 private final Options options; 52 protected final Messages messages; 53 protected final Symtab syms; 54 protected final JavacElements elements; 55 protected final JavacTypes types; 56 protected TreeScanner treeVisitor; 57 protected Kind eventKind; 58 59 public AbstractCodingRulesAnalyzer(JavacTask task) { 60 BasicJavacTask impl = (BasicJavacTask)task; 61 Context context = impl.getContext(); 62 log = Log.instance(context); 63 options = Options.instance(context); 64 rawDiagnostics = options.isSet("rawDiagnostics"); 65 diags = JCDiagnostic.Factory.instance(context); 66 messages = new Messages(); 67 syms = Symtab.instance(context); 68 elements = JavacElements.instance(context); 69 types = JavacTypes.instance(context); 70 } 71 72 protected class Messages { 73 ResourceBundle bundle; 74 75 Messages() { 76 String name = getClass().getPackage().getName() + ".resources.crules"; 77 bundle = ResourceBundle.getBundle(name, Locale.ENGLISH); 78 } 79 80 public void error(JCTree tree, String code, Object... args) { 81 String msg; 82 if (rawDiagnostics) { 83 RawDiagnosticFormatter f = new RawDiagnosticFormatter(options); 84 msg = f.formatMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), 85 tree.pos(), code, args), null); 86 } else { 87 msg = (code == null) ? (String) args[0] : localize(code, args); 88 } 89 log.error(tree, "proc.messager", msg.toString()); 90 } 91 92 private String localize(String code, Object... args) { 93 String msg = bundle.getString(code); 94 if (msg == null) { 95 StringBuilder sb = new StringBuilder(); 96 sb.append("message file broken: code=").append(code); 97 if (args.length > 0) { 98 sb.append(" arguments={0}"); 99 for (int i = 1; i < args.length; i++) { 100 sb.append(", {").append(i).append("}"); 101 } 102 } 103 msg = sb.toString(); 104 } 105 return MessageFormat.format(msg, args); 106 } 107 } 108 109} 110