KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package org.hammurapi.inspectors;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.hammurapi.HammurapiException;
31 import org.hammurapi.InspectorBase;
32
33 import com.pavelvlasov.jsel.expressions.AssignmentExpression;
34 import com.pavelvlasov.jsel.expressions.ConditionalExpression;
35 import com.pavelvlasov.jsel.expressions.LogicalOr;
36 import com.pavelvlasov.jsel.statements.IfStatement;
37 import com.pavelvlasov.review.SourceMarker;
38 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink;
39 import com.pavelvlasov.util.DispatchingVisitor;
40 import com.pavelvlasov.util.Visitable;
41
42 /**
43  * Assignment statements shouldn't be placed inside conditions of if statements.
44  *
45  * @author Pavel Vlasov
46  * @version $Revision: 1.4 $
47  */

48 public class AssignmentInsideConditionalRule extends InspectorBase {
49   
50   /**
51    * Inner class for making chained visit. It searches after
52    * assignment statements.
53    *
54    * @author Pavel Vlasov
55    */

56     public static class AssignmentSnooper {
57         List JavaDoc assignments=new LinkedList JavaDoc();
58         
59     /**
60      * Reviews the assignment expression.
61      *
62      * @param expression the expression to be reviewed.
63      */

64         public void visit(AssignmentExpression expression) {
65             if (!(expression instanceof LogicalOr || expression instanceof ConditionalExpression)) {
66                 assignments.add(expression);
67             }
68         }
69     }
70     
71     /**
72      * Reviews the if statement, if it contains in its condition
73      * an assignment statement.
74      *
75      * @param statement the if statement to be reviewed.
76      * @throws HammurapiException the exceptions occured in the Hammurapi helper classes.
77      */

78     public void visit(IfStatement statement) throws HammurapiException {
79         AssignmentSnooper snooper=new AssignmentSnooper();
80         AccumulatingVisitorExceptionSink es=new AccumulatingVisitorExceptionSink();
81         DispatchingVisitor dv=new DispatchingVisitor(snooper, es);
82         ((Visitable) statement.getExpression()).accept(dv);
83         
84         Iterator JavaDoc it=snooper.assignments.iterator();
85         while (it.hasNext()) {
86             context.reportViolation((SourceMarker) it.next());
87         }
88         
89         if (!es.getExceptions().isEmpty()) {
90             es.dump();
91             throw new HammurapiException("There have been exceptions (see above)");
92         }
93     }
94 }
95
Popular Tags