langtools/make/tools/crules/LegacyLogMethodAnalyzer.java
author jlahoda
Thu, 15 Jun 2017 13:44:42 +0200
changeset 45504 ea7475564d07
permissions -rw-r--r--
8170326: Inconsistencies between code, compiler.properties and comments Summary: Converting uses of Log and JCDiagnostic.Factory methods to use CompilerProperties instead of plain Strings, fixing inconsistencies, adding crules analyzer to ensure CompilerProperties are used whenever possible. Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     1
/*
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     4
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     8
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    14
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    18
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    21
 * questions.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    22
 */
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    23
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    24
package crules;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    25
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    26
import java.util.Arrays;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    27
import java.util.HashSet;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    28
import java.util.Set;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    29
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    30
import com.sun.source.util.JavacTask;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    31
import com.sun.source.util.TaskEvent.Kind;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    32
import com.sun.tools.javac.code.Kinds;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    33
import com.sun.tools.javac.code.Symbol;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    34
import com.sun.tools.javac.code.Symbol.MethodSymbol;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    35
import com.sun.tools.javac.code.Type;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree.JCExpression;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.Tag;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    40
import com.sun.tools.javac.tree.TreeInfo;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    41
import com.sun.tools.javac.tree.TreeScanner;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    42
import com.sun.tools.javac.util.AbstractLog;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.util.JCDiagnostic;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    44
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    45
/**This analyzer guards against legacy Log.error/warning/note methods that don't use the typed keys.*/
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    46
public class LegacyLogMethodAnalyzer extends AbstractCodingRulesAnalyzer {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    47
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    48
    public LegacyLogMethodAnalyzer(JavacTask task) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    49
        super(task);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    50
        treeVisitor = new LegacyLogMethodVisitor();
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    51
        eventKind = Kind.ANALYZE;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    52
    }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    53
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    54
    private static final Set<String> LEGACY_METHOD_NAMES = new HashSet<>(
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    55
            Arrays.asList("error", "mandatoryWarning", "warning", "mandatoryNote", "note", "fragment"));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    56
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    57
    class LegacyLogMethodVisitor extends TreeScanner {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    58
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    59
        @Override
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    60
        public void visitClassDef(JCClassDecl tree) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    61
            if (!tree.sym.packge().fullname.toString().startsWith("com.sun.tools.javac."))
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    62
                return ;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    63
            super.visitClassDef(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    64
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    65
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    66
        @Override
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    67
        public void visitApply(JCMethodInvocation tree) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    68
            checkLegacyLogMethod(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    69
            super.visitApply(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    70
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    71
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    72
        void checkLegacyLogMethod(JCMethodInvocation tree) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    73
            Symbol method = TreeInfo.symbolFor(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    74
            if (method == null ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    75
                method.kind != Kinds.Kind.MTH ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    76
                !typeToCheck(method.owner.type) ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    77
                !LEGACY_METHOD_NAMES.contains(method.name.toString()) ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    78
                !((MethodSymbol) method).isVarArgs() ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    79
                method.type.getParameterTypes().size() < 2) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    80
                return ;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    81
            }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    82
            JCExpression key = tree.args.get(method.type.getParameterTypes().size() - 2);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    83
            if (key.hasTag(Tag.LITERAL)) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    84
                messages.error(tree, "crules.use.of.legacy.log.method", tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    85
            }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    86
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    87
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    88
        boolean typeToCheck(Type type) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    89
            Symbol abstractLog = elements.getTypeElement(AbstractLog.class.getName());
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    90
            Symbol diagnosticFactory = elements.getTypeElement(JCDiagnostic.Factory.class.getName().replace('$', '.'));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    91
            return types.isSubtype(type, abstractLog.type) ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    92
                   types.isSubtype(type, diagnosticFactory.type);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    93
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    94
    }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    95
}