Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/bcel/checks/UnusedMethodCheck.java


1   //Tested with BCEL-5.1
2   //http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3   
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  /**
18   * Checks for unused methods
19   * @author Rick Giles
20   */
21  public class UnusedMethodCheck
22      extends AbstractReferenceCheck
23  {
24      /** @see AbstractReferenceCheck */
25      public void setScope(String aScopeName)
26      {
27          super.setScope(aScopeName);
28          ((ReferenceVisitor) getVisitor()).addMethodScope(
29              Scope.getInstance(aScopeName));
30      }
31  
32      /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
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 String methodName = methodDefs[i].getName();
44                      final Method method = methodDefs[i].getMethod();
45                      if (!ignore(className, method)) {
46                          log(
47                              0,
48                              "unused.method",
49                              new Object[] {className, methodDefs[i]});
50                      }
51                  }
52              }
53          }
54      }
55  
56      /** @see AbstractReferenceCheck */
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  }