KickJava   Java API By Example, From Geeks To Geeks.

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


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.LanguageElement;
30 import com.pavelvlasov.jsel.expressions.Dot;
31 import com.pavelvlasov.jsel.expressions.Ident;
32 import com.pavelvlasov.jsel.expressions.MethodCall;
33 import com.pavelvlasov.jsel.expressions.PrimaryExpression;
34 import com.pavelvlasov.jsel.statements.WhileStatement;
35 import com.pavelvlasov.review.SourceMarker;
36
37 /**
38  * ER-110
39  * Call 'wait ()' only inside a "while" loop
40  * @author Janos Czako
41  * @version $Revision: 1.2 $
42  */

43 public class WaitInLoopRule extends InspectorBase {
44     
45     /**
46      * The name of the operation, we are checking.
47      */

48     private static final String JavaDoc WAIT = "wait";
49     
50     /**
51      * Reviews the methodcalls if they violate against the rule.
52      *
53      * @param element the methodcall to be reviewed.
54      */

55     public void visit(MethodCall element) {
56         PrimaryExpression methodName = element.getName();
57         if (methodName instanceof Dot) {
58             List JavaDoc flatOperands = ((Dot) methodName).getFlatOperands();
59             methodName=(PrimaryExpression) flatOperands.get(flatOperands.size()-1);
60         }
61         
62         List JavaDoc parameters = element.getParameters();
63         if (methodName instanceof Ident && WAIT.equals(((Ident) methodName).getText()) && (parameters.size()==0 || parameters.size()==1)) {
64             if (!checkForWhileAsParent((LanguageElement) element)) {
65                 context.reportViolation((SourceMarker) element);
66             }
67         }
68     }
69
70     /**
71      * Checks if the element is inside of a while loop (or is one).
72      *
73      * @param le the element to be checked
74      * @return if it is inside a while loop
75      */

76     private boolean checkForWhileAsParent(LanguageElement le) {
77         boolean foundWhile = le instanceof WhileStatement;
78         
79         while (!foundWhile && le.getParent()!=null) {
80             le = le.getParent();
81             foundWhile = le instanceof WhileStatement;
82     }
83     
84         return foundWhile;
85     }
86 }
87
Popular Tags