KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > bcel > checks > UnusedMethodCheck


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 JavaDoc;
7 import java.util.Set JavaDoc;
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 JavaDoc 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 JavaDoc aJavaClasses)
34     {
35         final Iterator JavaDoc it = aJavaClasses.iterator();
36         while (it.hasNext()) {
37             final JavaClass javaClass = (JavaClass) it.next();
38             final String JavaDoc 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 JavaDoc[] {methodDefs[i]});
50                     }
51                 }
52             }
53         }
54     }
55
56     /** @see AbstractReferenceCheck */
57     public boolean ignore(String JavaDoc aClassName, Method aMethod)
58     {
59         final String JavaDoc methodName = aMethod.getName();
60         return (super.ignore(aClassName, aMethod)
61             || methodName.equals("<init>")
62             || methodName.equals("<clinit>"));
63     }
64 }
65
Popular Tags