1 4 package com.puppycrawl.tools.checkstyle.bcel.checks; 5 6 import java.util.Iterator ; 7 import java.util.Set ; 8 9 import org.apache.bcel.classfile.JavaClass; 10 import org.apache.bcel.classfile.Method; 11 12 import com.puppycrawl.tools.checkstyle.api.Scope; 13 import com.puppycrawl.tools.checkstyle.bcel.ReferenceVisitor; 14 import com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition; 15 import com.puppycrawl.tools.checkstyle.bcel.classfile.MethodDefinition; 16 17 21 public class UnusedMethodCheck 22 extends AbstractReferenceCheck 23 { 24 25 public void setScope(String aScopeName) 26 { 27 super.setScope(aScopeName); 28 ((ReferenceVisitor) getVisitor()).addMethodScope( 29 Scope.getInstance(aScopeName)); 30 } 31 32 33 public void leaveSet(Set aJavaClasses) 34 { 35 final Iterator it = aJavaClasses.iterator(); 36 while (it.hasNext()) { 37 final JavaClass javaClass = (JavaClass) it.next(); 38 final String className = javaClass.getClassName(); 39 final JavaClassDefinition classDef = findJavaClassDef(javaClass); 40 final MethodDefinition[] methodDefs = classDef.getMethodDefs(); 41 for (int i = 0; i < methodDefs.length; i++) { 42 if (!classDef.hasReference(methodDefs[i], getReferenceDAO())) { 43 final Method method = methodDefs[i].getMethod(); 44 if (!ignore(className, method)) { 45 log( 46 javaClass, 47 0, 48 "unused.method", 49 new Object [] {methodDefs[i]}); 50 } 51 } 52 } 53 } 54 } 55 56 57 public boolean ignore(String aClassName, Method aMethod) 58 { 59 final String methodName = aMethod.getName(); 60 return (super.ignore(aClassName, aMethod) 61 || methodName.equals("<init>") 62 || methodName.equals("<clinit>")); 63 } 64 } 65 | Popular Tags |