01: /*
02: * Hammurapi
03: * Automated Java code review system.
04: * Copyright (C) 2004 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (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)
46: .getParent();
47: if (parent instanceof VariableDefinition
48: && ((VariableDefinition) parent).getModifiers()
49: .contains("static")) {
50: return;
51: }
52:
53: if (element.getTypeSpecification().isKindOf("java.lang.String")) {
54: analyze(element, (Expression) element.getOperand(0),
55: (Expression) element.getOperand(1));
56: }
57: }
58:
59: public void visit(PlusAssignment element) throws JselException {
60: if (((Expression) element.getOperand(0)).getTypeSpecification()
61: .isKindOf("java.lang.String")) {
62: analyze(element, (Expression) element.getOperand(0),
63: (Expression) element.getOperand(1));
64: }
65: }
66:
67: private void analyze(Expression e, Expression e1, Expression e2) {
68: // Do not report single plus like a+b, only more than one plus.
69: // and do not report constants concatenation like "a"+"b"+"c"
70: if ((e1 instanceof Plus || e2 instanceof Plus)
71: && !(isConstant(e1) && isConstant(e2))) {
72: context.reportViolation((SourceMarker) e);
73: }
74: }
75:
76: private boolean isConstant(Expression expr) {
77: return peel(expr) instanceof StringConstant
78: || (peel(expr) instanceof Plus
79: && isConstant(expr.getOperand(0)) && isConstant(expr
80: .getOperand(1)));
81: }
82:
83: private Expression peel(Expression expr) {
84: if (expr instanceof TypeCast) {
85: return peel(((TypeCast) expr).getExpression());
86: } else if (expr instanceof Parenthesis) {
87: return peel((Expression) expr.getOperand(0));
88: } else {
89: return expr;
90: }
91: }
92: }
|