|
1 /* |
|
2 * Copyright (c) 2017, 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 |
|
24 package crules; |
|
25 |
|
26 import java.util.Arrays; |
|
27 import java.util.HashSet; |
|
28 import java.util.Set; |
|
29 |
|
30 import com.sun.source.util.JavacTask; |
|
31 import com.sun.source.util.TaskEvent.Kind; |
|
32 import com.sun.tools.javac.code.Kinds; |
|
33 import com.sun.tools.javac.code.Symbol; |
|
34 import com.sun.tools.javac.code.Symbol.MethodSymbol; |
|
35 import com.sun.tools.javac.code.Type; |
|
36 import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
|
37 import com.sun.tools.javac.tree.JCTree.JCExpression; |
|
38 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; |
|
39 import com.sun.tools.javac.tree.JCTree.Tag; |
|
40 import com.sun.tools.javac.tree.TreeInfo; |
|
41 import com.sun.tools.javac.tree.TreeScanner; |
|
42 import com.sun.tools.javac.util.AbstractLog; |
|
43 import com.sun.tools.javac.util.JCDiagnostic; |
|
44 |
|
45 /**This analyzer guards against legacy Log.error/warning/note methods that don't use the typed keys.*/ |
|
46 public class LegacyLogMethodAnalyzer extends AbstractCodingRulesAnalyzer { |
|
47 |
|
48 public LegacyLogMethodAnalyzer(JavacTask task) { |
|
49 super(task); |
|
50 treeVisitor = new LegacyLogMethodVisitor(); |
|
51 eventKind = Kind.ANALYZE; |
|
52 } |
|
53 |
|
54 private static final Set<String> LEGACY_METHOD_NAMES = new HashSet<>( |
|
55 Arrays.asList("error", "mandatoryWarning", "warning", "mandatoryNote", "note", "fragment")); |
|
56 |
|
57 class LegacyLogMethodVisitor extends TreeScanner { |
|
58 |
|
59 @Override |
|
60 public void visitClassDef(JCClassDecl tree) { |
|
61 if (!tree.sym.packge().fullname.toString().startsWith("com.sun.tools.javac.")) |
|
62 return ; |
|
63 super.visitClassDef(tree); |
|
64 } |
|
65 |
|
66 @Override |
|
67 public void visitApply(JCMethodInvocation tree) { |
|
68 checkLegacyLogMethod(tree); |
|
69 super.visitApply(tree); |
|
70 } |
|
71 |
|
72 void checkLegacyLogMethod(JCMethodInvocation tree) { |
|
73 Symbol method = TreeInfo.symbolFor(tree); |
|
74 if (method == null || |
|
75 method.kind != Kinds.Kind.MTH || |
|
76 !typeToCheck(method.owner.type) || |
|
77 !LEGACY_METHOD_NAMES.contains(method.name.toString()) || |
|
78 !((MethodSymbol) method).isVarArgs() || |
|
79 method.type.getParameterTypes().size() < 2) { |
|
80 return ; |
|
81 } |
|
82 JCExpression key = tree.args.get(method.type.getParameterTypes().size() - 2); |
|
83 if (key.hasTag(Tag.LITERAL)) { |
|
84 messages.error(tree, "crules.use.of.legacy.log.method", tree); |
|
85 } |
|
86 } |
|
87 |
|
88 boolean typeToCheck(Type type) { |
|
89 Symbol abstractLog = elements.getTypeElement(AbstractLog.class.getName()); |
|
90 Symbol diagnosticFactory = elements.getTypeElement(JCDiagnostic.Factory.class.getName().replace('$', '.')); |
|
91 return types.isSubtype(type, abstractLog.type) || |
|
92 types.isSubtype(type, diagnosticFactory.type); |
|
93 } |
|
94 } |
|
95 } |