langtools/make/tools/crules/AssertCheckAnalyzer.java
author mcimadamore
Thu, 05 Mar 2015 13:10:49 +0000
changeset 29294 376a915b4ff0
parent 25848 3bc09f4676a9
child 38510 3f450a95c68f
permissions -rw-r--r--
8073645: Add lambda-based lazy eval versions of Assert.check methods Summary: Enhance Assert so that lazy string computation can occurr where needed; enhance static roding rule checkers to make sure the right version of the method is called. Reviewed-by: jlahoda
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
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    40
public class AssertCheckAnalyzer extends AbstractCodingRulesAnalyzer {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    41
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    42
    enum AssertOverloadKind {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    43
        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
    44
        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
    45
        NONE(null);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    46
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    47
        String errKey;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    48
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    49
        AssertOverloadKind(String errKey) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    50
            this.errKey = 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
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    53
        boolean simpleArgExpected() {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    54
            return this == AssertOverloadKind.EAGER;
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
    }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    57
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    58
    public AssertCheckAnalyzer(JavacTask task) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    59
        super(task);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    60
        treeVisitor = new AssertCheckVisitor();
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    61
        eventKind = Kind.ANALYZE;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    62
    }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    63
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    64
    class AssertCheckVisitor extends TreeScanner {
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
        @Override
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    67
        public void visitApply(JCMethodInvocation tree) {
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    68
            Symbol m = TreeInfo.symbolFor(tree);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    69
            AssertOverloadKind ak = assertOverloadKind(m);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    70
            if (ak != AssertOverloadKind.NONE &&
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    71
                !m.name.contentEquals("error")) {
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    72
                JCExpression lastParam = tree.args.last();
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    73
                if (isSimpleStringArg(lastParam) != ak.simpleArgExpected()) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    74
                    messages.error(lastParam, ak.errKey);
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    75
                }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    76
            }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    77
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    78
            super.visitApply(tree);
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
29294
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    81
        AssertOverloadKind assertOverloadKind(Symbol method) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    82
            if (method == null ||
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    83
                !method.owner.getQualifiedName().contentEquals(Assert.class.getName()) ||
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    84
                method.type.getParameterTypes().tail == null) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    85
                return AssertOverloadKind.NONE;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    86
            }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    87
            Type formal = method.type.getParameterTypes().last();
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    88
            if (types.isSameType(formal, syms.stringType)) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    89
                return AssertOverloadKind.EAGER;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    90
            } 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
    91
                return AssertOverloadKind.LAZY;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    92
            } else {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    93
                return AssertOverloadKind.NONE;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    94
            }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    95
        }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    96
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    97
        boolean isSimpleStringArg(JCExpression e) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    98
            switch (e.getTag()) {
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
    99
                case LAMBDA:
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   100
                    JCLambda lambda = (JCLambda)e;
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   101
                    return (lambda.getBodyKind() == BodyKind.EXPRESSION) &&
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   102
                            isSimpleStringArg((JCExpression)lambda.body);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   103
                default:
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   104
                    Symbol argSym = TreeInfo.symbolFor(e);
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   105
                    return (e.type.constValue() != null ||
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   106
                            (argSym != null && argSym.kind == Kinds.Kind.VAR));
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   107
            }
376a915b4ff0 8073645: Add lambda-based lazy eval versions of Assert.check methods
mcimadamore
parents: 25848
diff changeset
   108
        }
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   109
    }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
   110
}