make/langtools/tools/crules/LegacyLogMethodAnalyzer.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 49158 f62d1d1c2d9c
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
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
49158
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    10
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    11
 * 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
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    14
 * 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
    15
 * accompanied this code).
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    16
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    17
 * 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
    18
 * 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
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    20
 *
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    22
 * 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
    23
 * questions.
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    24
 */
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
package crules;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    27
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    28
import java.util.Arrays;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    29
import java.util.HashSet;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    30
import java.util.Set;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    31
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    32
import com.sun.source.util.JavacTask;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    33
import com.sun.source.util.TaskEvent.Kind;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    34
import com.sun.tools.javac.code.Kinds;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    35
import com.sun.tools.javac.code.Symbol;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    36
import com.sun.tools.javac.code.Symbol.MethodSymbol;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    37
import com.sun.tools.javac.code.Type;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.JCExpression;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    41
import com.sun.tools.javac.tree.JCTree.Tag;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    42
import com.sun.tools.javac.tree.TreeInfo;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.tree.TreeScanner;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    44
import com.sun.tools.javac.util.AbstractLog;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    45
import com.sun.tools.javac.util.JCDiagnostic;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    46
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    47
/**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
    48
public class LegacyLogMethodAnalyzer extends AbstractCodingRulesAnalyzer {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    49
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    50
    public LegacyLogMethodAnalyzer(JavacTask task) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    51
        super(task);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    52
        treeVisitor = new LegacyLogMethodVisitor();
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    53
        eventKind = Kind.ANALYZE;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    54
    }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    55
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    56
    private static final Set<String> LEGACY_METHOD_NAMES = new HashSet<>(
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    57
            Arrays.asList("error", "mandatoryWarning", "warning", "mandatoryNote", "note", "fragment"));
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
    class LegacyLogMethodVisitor extends TreeScanner {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    60
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    61
        @Override
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    62
        public void visitClassDef(JCClassDecl tree) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    63
            if (!tree.sym.packge().fullname.toString().startsWith("com.sun.tools.javac."))
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    64
                return ;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    65
            super.visitClassDef(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    66
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    67
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    68
        @Override
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    69
        public void visitApply(JCMethodInvocation tree) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    70
            checkLegacyLogMethod(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    71
            super.visitApply(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    72
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    73
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    74
        void checkLegacyLogMethod(JCMethodInvocation tree) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    75
            Symbol method = TreeInfo.symbolFor(tree);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    76
            if (method == null ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    77
                method.kind != Kinds.Kind.MTH ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    78
                !typeToCheck(method.owner.type) ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    79
                !LEGACY_METHOD_NAMES.contains(method.name.toString()) ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    80
                !((MethodSymbol) method).isVarArgs() ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    81
                method.type.getParameterTypes().size() < 2) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    82
                return ;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    83
            }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    84
            JCExpression key = tree.args.get(method.type.getParameterTypes().size() - 2);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    85
            if (key.hasTag(Tag.LITERAL)) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    86
                messages.error(tree, "crules.use.of.legacy.log.method", tree);
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
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    89
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    90
        boolean typeToCheck(Type type) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    91
            Symbol abstractLog = elements.getTypeElement(AbstractLog.class.getName());
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    92
            Symbol diagnosticFactory = elements.getTypeElement(JCDiagnostic.Factory.class.getName().replace('$', '.'));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    93
            return types.isSubtype(type, abstractLog.type) ||
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    94
                   types.isSubtype(type, diagnosticFactory.type);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    95
        }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    96
    }
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
diff changeset
    97
}