01: //Tested with BCEL-5.1
02: //http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
03:
04: package com.puppycrawl.tools.checkstyle.bcel.checks;
05:
06: import java.util.Iterator;
07: import java.util.Set;
08:
09: import org.apache.bcel.classfile.JavaClass;
10: import org.apache.bcel.classfile.Method;
11: import org.apache.bcel.generic.Type;
12: import com.puppycrawl.tools.checkstyle.api.Scope;
13: import com.puppycrawl.tools.checkstyle.bcel.ReferenceVisitor;
14:
15: /**
16: * Checks for fields that hide fields in superclasses
17: * @author Daniel Grenner
18: */
19: public class HiddenStaticMethodCheck extends AbstractReferenceCheck {
20: /** @see AbstractReferenceCheck */
21: public void setScope(String aFrom) {
22: super .setScope(aFrom);
23: ((ReferenceVisitor) getVisitor()).addFieldScope(Scope
24: .getInstance(aFrom));
25: }
26:
27: /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
28: public void visitObject(Object aJavaClass) {
29: final JavaClass javaClass = (JavaClass) aJavaClass;
30: final String className = javaClass.getClassName();
31: final JavaClass[] super Classes = javaClass.getSuperClasses();
32: final Method[] methods = javaClass.getMethods();
33: // Check all methods
34: for (int i = 0; i < methods.length; i++) {
35: final Method method = methods[i];
36: // Check that the method is a possible match
37: if (!method.isPrivate() && method.isStatic()) {
38: // Go through all their superclasses
39: for (int j = 0; j < super Classes.length; j++) {
40: final JavaClass super Class = super Classes[j];
41: final String super ClassName = super Class
42: .getClassName();
43: final Method[] super ClassMethods = super Class
44: .getMethods();
45: // Go through the methods of the superclasses
46: for (int k = 0; k < super ClassMethods.length; k++) {
47: final Method super ClassMethod = super ClassMethods[k];
48: if (super ClassMethod.getName().equals(
49: method.getName())
50: && !ignore(className, method)) {
51: Type[] methodTypes = method
52: .getArgumentTypes();
53: Type[] super Types = super ClassMethod
54: .getArgumentTypes();
55: if (methodTypes.length == super Types.length) {
56: boolean match = true;
57: for (int arg = 0; arg < methodTypes.length; arg++) {
58: if (!methodTypes[arg]
59: .equals(super Types[arg])) {
60: match = false;
61: }
62: }
63: // Same method parameters
64: if (match) {
65: log(javaClass, 0,
66: "hidden.static.method",
67: new Object[] { method,
68: super ClassName });
69: }
70: }
71: }
72: }
73: }
74: }
75: }
76: }
77:
78: /** @see AbstractReferenceCheck */
79: public boolean ignore(String aClassName, Method aMethod) {
80: final String methodName = aMethod.getName();
81: return (/*super.ignore(aClassName, aMethod)
82: || */methodName.equals("<init>")
83: || methodName.equals("<clinit>")
84: || methodName.equals("class$") || aMethod.toString()
85: .indexOf("[Synthetic]") > -1);
86: }
87:
88: }
|