1 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 43 public class UnusedVariablesRule extends InspectorBase { 44 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 target) { 60 if (target instanceof Ident) { 61 Ident ident=(Ident) target; 62 try { 63 if (ident.getText().equals(element.getName()) && ((LanguageElement) ident).getParent()!=element) { 65 Object 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 } 82 } 83 } 84 } 85 | Popular Tags |