langtools/make/tools/crules/AssertCheckAnalyzer.java
author jlahoda
Mon, 16 May 2016 15:41:20 +0200
changeset 38510 3f450a95c68f
parent 29294 376a915b4ff0
permissions -rw-r--r--
8054562: document skip results in RunCodingRules.java Summary: Documenting when RunCodingRules gets skipped; adding a short description of AssertCheckAnalyzer and MutableFieldsAnalyzer Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     1
/*
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     4
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     8
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    14
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    18
 *
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    21
 * questions.
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    22
 */
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    23
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    24
package crules;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    25
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    26
import com.sun.source.tree.LambdaExpressionTree.BodyKind;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    27
import com.sun.source.util.JavacTask;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    28
import com.sun.source.util.TaskEvent.Kind;
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    29
import com.sun.tools.javac.code.Kinds;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    30
import com.sun.tools.javac.code.Symbol;
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    31
import com.sun.tools.javac.code.Type;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    32
import com.sun.tools.javac.tree.JCTree.JCExpression;
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    33
import com.sun.tools.javac.tree.JCTree.JCLambda;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    34
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree.Tag;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    36
import com.sun.tools.javac.tree.TreeInfo;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    37
import com.sun.tools.javac.tree.TreeScanner;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    38
import com.sun.tools.javac.util.Assert;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    39
38510
3f450a95c68f 8054562: document skip results in RunCodingRules.java
jlahoda
parents: 29294
diff changeset
    40
/**This analyzer guards against complex messages (i.e. those that use string concatenation) passed
3f450a95c68f 8054562: document skip results in RunCodingRules.java
jlahoda
parents: 29294
diff changeset
    41
 * to various Assert.check methods.
3f450a95c68f 8054562: document skip results in RunCodingRules.java
jlahoda
parents: 29294
diff changeset
    42
 */
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    43
public class AssertCheckAnalyzer extends AbstractCodingRulesAnalyzer {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    44
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    45
    enum AssertOverloadKind {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    46
        EAGER("crules.should.not.use.eager.string.evaluation"),
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    47
        LAZY("crules.should.not.use.lazy.string.evaluation"),
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    48
        NONE(null);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    49
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    50
        String errKey;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    51
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    52
        AssertOverloadKind(String errKey) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    53
            this.errKey = errKey;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    54
        }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    55
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    56
        boolean simpleArgExpected() {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    57
            return this == AssertOverloadKind.EAGER;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    58
        }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    59
    }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    60
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    61
    public AssertCheckAnalyzer(JavacTask task) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    62
        super(task);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    63
        treeVisitor = new AssertCheckVisitor();
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    64
        eventKind = Kind.ANALYZE;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    65
    }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    66
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    67
    class AssertCheckVisitor extends TreeScanner {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    68
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    69
        @Override
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    70
        public void visitApply(JCMethodInvocation tree) {
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    71
            Symbol m = TreeInfo.symbolFor(tree);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    72
            AssertOverloadKind ak = assertOverloadKind(m);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    73
            if (ak != AssertOverloadKind.NONE &&
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    74
                !m.name.contentEquals("error")) {
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    75
                JCExpression lastParam = tree.args.last();
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    76
                if (isSimpleStringArg(lastParam) != ak.simpleArgExpected()) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    77
                    messages.error(lastParam, ak.errKey);
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    78
                }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    79
            }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    80
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    81
            super.visitApply(tree);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    82
        }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    83
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    84
        AssertOverloadKind assertOverloadKind(Symbol method) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    85
            if (method == null ||
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    86
                !method.owner.getQualifiedName().contentEquals(Assert.class.getName()) ||
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    87
                method.type.getParameterTypes().tail == null) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    88
                return AssertOverloadKind.NONE;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    89
            }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    90
            Type formal = method.type.getParameterTypes().last();
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    91
            if (types.isSameType(formal, syms.stringType)) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    92
                return AssertOverloadKind.EAGER;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    93
            } else if (types.isSameType(types.erasure(formal), types.erasure(syms.supplierType))) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    94
                return AssertOverloadKind.LAZY;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    95
            } else {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    96
                return AssertOverloadKind.NONE;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    97
            }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    98
        }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    99
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   100
        boolean isSimpleStringArg(JCExpression e) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   101
            switch (e.getTag()) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   102
                case LAMBDA:
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   103
                    JCLambda lambda = (JCLambda)e;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   104
                    return (lambda.getBodyKind() == BodyKind.EXPRESSION) &&
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   105
                            isSimpleStringArg((JCExpression)lambda.body);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   106
                default:
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   107
                    Symbol argSym = TreeInfo.symbolFor(e);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   108
                    return (e.type.constValue() != null ||
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   109
                            (argSym != null && argSym.kind == Kinds.Kind.VAR));
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   110
            }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   111
        }
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   112
    }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   113
}