langtools/make/tools/crules/AssertCheckAnalyzer.java
author jlahoda
Fri, 01 Aug 2014 11:09:40 -0700
changeset 25848 3bc09f4676a9
child 29294 376a915b4ff0
permissions -rw-r--r--
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks. Summary: Generalizing the crules infrastructure, adding a new analyzer to check String concatenation in Assert calls. Reviewed-by: jjg, vromero
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
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    26
import com.sun.source.util.JavacTask;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    27
import com.sun.source.util.TaskEvent.Kind;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    28
import com.sun.tools.javac.code.Symbol;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    29
import com.sun.tools.javac.tree.JCTree.JCExpression;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    30
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
    31
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
    32
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
    33
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
    34
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
    35
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    36
public class AssertCheckAnalyzer extends AbstractCodingRulesAnalyzer {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    37
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    38
    public AssertCheckAnalyzer(JavacTask task) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    39
        super(task);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    40
        treeVisitor = new AssertCheckVisitor();
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    41
        eventKind = Kind.ANALYZE;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    42
    }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    43
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    44
    class AssertCheckVisitor extends TreeScanner {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    45
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    46
        @Override
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    47
        public void visitApply(JCMethodInvocation tree) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    48
            Symbol method = TreeInfo.symbolFor(tree);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    49
            if (method != null &&
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    50
                method.owner.getQualifiedName().contentEquals(Assert.class.getName()) &&
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    51
                !method.name.contentEquals("error")) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    52
                JCExpression lastParam = tree.args.last();
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    53
                if (lastParam != null &&
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    54
                    lastParam.type.tsym == syms.stringType.tsym &&
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    55
                    lastParam.hasTag(Tag.PLUS)) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    56
                    messages.error(tree, "crules.should.not.use.string.concatenation");
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    57
                }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    58
            }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    59
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    60
            super.visitApply(tree);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
diff changeset
    61
        }
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
}