KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > StringLiteralEqualsRule


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;
24
25 import java.util.List JavaDoc;
26
27 import org.hammurapi.InspectorBase;
28
29 import com.pavelvlasov.jsel.JselException;
30 import com.pavelvlasov.jsel.LanguageElement;
31 import com.pavelvlasov.jsel.TypeSpecification;
32 import com.pavelvlasov.jsel.VariableDefinition;
33 import com.pavelvlasov.jsel.expressions.Dot;
34 import com.pavelvlasov.jsel.expressions.Expression;
35 import com.pavelvlasov.jsel.expressions.Ident;
36 import com.pavelvlasov.jsel.expressions.MethodCall;
37 import com.pavelvlasov.jsel.expressions.PrimaryExpression;
38 import com.pavelvlasov.jsel.expressions.StringConstant;
39 import com.pavelvlasov.review.SourceMarker;
40
41
42 /**
43  * ER-123
44  * If you have to compare with a string do not use degree.equals("1")) but "1".equals(degree)
45  * @author Janos Czako
46  * @version $Revision: 1.2 $
47  */

48 public class StringLiteralEqualsRule extends InspectorBase {
49     
50     /**
51      * The name of the operation, we are checking.
52      */

53     private static final String JavaDoc EQUALS = "equals";
54     
55     /**
56      * Reviews the methodcalls if they violate against the rule.
57      *
58      * @param element the methodcall to be reviewed.
59      */

60     public void visit(MethodCall element) {
61         PrimaryExpression methodName = element.getName();
62         if (methodName instanceof Dot) {
63             List JavaDoc flatOperands = ((Dot) methodName).getFlatOperands();
64             methodName=(PrimaryExpression) flatOperands.get(flatOperands.size()-1);
65         }
66         
67         List JavaDoc parameters = element.getParameters();
68         if (methodName instanceof Ident && EQUALS.equals(((Ident) methodName).getText()) && parameters.size()==1) {
69             Expression parameter = (Expression) parameters.get(0);
70             if (parameter instanceof StringConstant) {
71                 context.reportViolation((SourceMarker) parameter);
72             } else if (parameter instanceof Ident) {
73                 try {
74                     // TODO analyze if parameter is a final string constant from another type like MyClass.MY_CONSTANT
75
Object JavaDoc r=((LanguageElement) parameter).getEnclosingScope().getVariableNamespace().find(((Ident) parameter).getText());
76                     if (r instanceof VariableDefinition) {
77                         VariableDefinition vd=(VariableDefinition) r;
78                         TypeSpecification typeSpecification = vd.getTypeSpecification();
79                         try {
80                             if (vd.getModifiers().contains("final") && vd.getInitializer()!=null && typeSpecification.getDimensions()==0 && typeSpecification.getType().isKindOf("java.lang.String")) {
81                                 context.reportViolation((SourceMarker) parameter);
82                             }
83                         } catch (JselException e) {
84                             context.warn((SourceMarker) typeSpecification, e);
85                         }
86                     }
87                 } catch (JselException e) {
88                     context.warn((SourceMarker) parameter, e);
89                 }
90             }
91         }
92     }
93     
94 }
95
Popular Tags