KickJava   Java API By Example, From Geeks To Geeks.

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


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 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) throws HammurapiException {
49         
50         LanguageElement parent = element.getParent();
51         if (element.getModifiers().contains("private") || !(parent instanceof TypeDefinition)) {
52             class UsedException extends com.pavelvlasov.RuntimeException {
53                 
54             };
55                         
56             try {
57                 Visitable toVisit = parent instanceof TypeDefinition ? element.getCompilationUnit() : (Visitable) element.getEnclosingScope();
58                 toVisit.accept(new Visitor() {
59                     public boolean visit(Object JavaDoc target) {
60                         if (target instanceof Ident) {
61                             Ident ident=(Ident) target;
62                             try {
63                                 // First comparison is to save time.
64
if (ident.getText().equals(element.getName()) && ((LanguageElement) ident).getParent()!=element) {
65                                     Object JavaDoc provider = ident.getProvider();
66                                     if (provider==element) {
67                                         throw new UsedException();
68                                     }
69                                 }
70                             } catch (JselException e) {
71                                 throw new JselRuntimeException(e);
72                             }
73                         }
74                         return true;
75                     }
76                     
77                 });
78                 context.reportViolation(element);
79             } catch (UsedException e) {
80                 // It's OK. The way to exit.
81
}
82         }
83     }
84 }
85
Popular Tags