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 org.hammurapi.HammurapiException;
26: import org.hammurapi.InspectorBase;
27:
28: import com.pavelvlasov.jsel.JselException;
29: import com.pavelvlasov.jsel.JselRuntimeException;
30: import com.pavelvlasov.jsel.LanguageElement;
31: import com.pavelvlasov.jsel.TypeDefinition;
32: import com.pavelvlasov.jsel.VariableDefinition;
33: import com.pavelvlasov.jsel.expressions.Ident;
34: import com.pavelvlasov.util.Visitable;
35: import com.pavelvlasov.util.Visitor;
36:
37: /**
38: * ER-131
39: * Unused private variables.
40: * @author Pavel Vlasov
41: * @version $Revision: 1.7 $
42: */
43: public class UnusedVariablesRule extends InspectorBase {
44: /**
45: * Reviews variable definition.
46: * @param element the type definition to be reviewed.
47: */
48: public void visit(final VariableDefinition element)
49: throws HammurapiException {
50:
51: LanguageElement parent = element.getParent();
52: if (element.getModifiers().contains("private")
53: || !(parent instanceof TypeDefinition)) {
54: class UsedException extends
55: com.pavelvlasov.RuntimeException {
56:
57: }
58: ;
59:
60: try {
61: Visitable toVisit = parent instanceof TypeDefinition ? element
62: .getCompilationUnit()
63: : (Visitable) element.getEnclosingScope();
64: toVisit.accept(new Visitor() {
65: public boolean visit(Object target) {
66: if (target instanceof Ident) {
67: Ident ident = (Ident) target;
68: try {
69: // First comparison is to save time.
70: if (ident.getText().equals(
71: element.getName())
72: && ((LanguageElement) ident)
73: .getParent() != element) {
74: Object provider = ident
75: .getProvider();
76: if (provider == element) {
77: throw new UsedException();
78: }
79: }
80: } catch (JselException e) {
81: throw new JselRuntimeException(e);
82: }
83: }
84: return true;
85: }
86:
87: });
88: context.reportViolation(element);
89: } catch (UsedException e) {
90: // It's OK. The way to exit.
91: }
92: }
93: }
94: }
|