KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > performance > StringConcatenationInspector


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors.performance;
24
25 import org.hammurapi.InspectorBase;
26
27 import com.pavelvlasov.jsel.JselException;
28 import com.pavelvlasov.jsel.LanguageElement;
29 import com.pavelvlasov.jsel.VariableDefinition;
30 import com.pavelvlasov.jsel.expressions.Expression;
31 import com.pavelvlasov.jsel.expressions.Parenthesis;
32 import com.pavelvlasov.jsel.expressions.Plus;
33 import com.pavelvlasov.jsel.expressions.PlusAssignment;
34 import com.pavelvlasov.jsel.expressions.StringConstant;
35 import com.pavelvlasov.jsel.expressions.TypeCast;
36 import com.pavelvlasov.review.SourceMarker;
37
38 /**
39  * @author Pavel Vlasov
40  * @version $Revision: 1.3 $
41  */

42 public class StringConcatenationInspector extends InspectorBase {
43     
44     public void visit(Plus element) throws JselException {
45         LanguageElement parent = ((LanguageElement) element).getParent();
46         if (parent instanceof VariableDefinition && ((VariableDefinition) parent).getModifiers().contains("static")) {
47             return;
48         }
49         
50         if (element.getTypeSpecification().isKindOf("java.lang.String")) {
51             analyze(element, (Expression) element.getOperand(0), (Expression) element.getOperand(1));
52         }
53     }
54     
55     public void visit(PlusAssignment element) throws JselException {
56         if (((Expression) element.getOperand(0)).getTypeSpecification().isKindOf("java.lang.String")) {
57             analyze(element, (Expression) element.getOperand(0), (Expression) element.getOperand(1));
58         }
59     }
60     
61     private void analyze(Expression e, Expression e1, Expression e2) {
62         // Do not report single plus like a+b, only more than one plus.
63
// and do not report constants concatenation like "a"+"b"+"c"
64
if ((e1 instanceof Plus || e2 instanceof Plus) && !(isConstant(e1) && isConstant(e2))) {
65             context.reportViolation((SourceMarker) e);
66         }
67     }
68     
69     private boolean isConstant(Expression expr) {
70         return peel(expr) instanceof StringConstant
71                 || (peel(expr) instanceof Plus
72                         && isConstant(expr.getOperand(0))
73                         && isConstant(expr.getOperand(1)));
74     }
75     
76     private Expression peel(Expression expr) {
77         if (expr instanceof TypeCast) {
78             return peel(((TypeCast) expr).getExpression());
79         } else if (expr instanceof Parenthesis) {
80             return peel((Expression) expr.getOperand(0));
81         } else {
82             return expr;
83         }
84     }
85 }
86
Popular Tags