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;
24:
25: import java.util.List;
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-111
39: * Use 'wait ()' and 'notifyAll ()' instead of polling loops
40: * @author Janos Czako
41: * @version $Revision: 1.2 $
42: */
43: public class WaitAndNotifyInsteadOfPollingRule extends InspectorBase {
44:
45: /**
46: * The name of the operation, we are checking.
47: */
48: private static final String SLEEP = "sleep";
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 flatOperands = ((Dot) methodName).getFlatOperands();
59: methodName = (PrimaryExpression) flatOperands
60: .get(flatOperands.size() - 1);
61: }
62:
63: List parameters = element.getParameters();
64: if (methodName instanceof Ident
65: && SLEEP.equals(((Ident) methodName).getText())
66: && parameters.size() == 1) {
67: if (checkForWhileAsParent((LanguageElement) element)) {
68: context.reportViolation((SourceMarker) element);
69: }
70: }
71: }
72:
73: /**
74: * Checks if the element is inside of a while loop (or is one).
75: *
76: * @param le the element to be checked
77: * @return if it is inside a while loop
78: */
79: private boolean checkForWhileAsParent(LanguageElement le) {
80: boolean foundWhile = le instanceof WhileStatement;
81:
82: while (!foundWhile && le.getParent() != null) {
83: le = le.getParent();
84: foundWhile = le instanceof WhileStatement;
85: }
86:
87: return foundWhile;
88: }
89: }
|