1 4 package com.puppycrawl.tools.checkstyle.bcel.classfile; 5 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import java.util.Set ; 9 10 import org.apache.bcel.Repository; 11 import org.apache.bcel.classfile.JavaClass; 12 import org.apache.bcel.classfile.Method; 13 import org.apache.bcel.generic.Type; 14 15 import com.puppycrawl.tools.checkstyle.bcel.generic.InvokeReference; 16 import com.puppycrawl.tools.checkstyle.bcel.generic.Utils; 17 18 22 public class MethodDefinition 23 extends FieldOrMethodDefinition 24 { 25 26 private Set mReferences = new HashSet (); 27 28 32 public MethodDefinition(Method aMethod) 33 { 34 super(aMethod); 35 } 36 37 41 public Set getReferences() 42 { 43 return mReferences; 44 } 45 46 50 public int getReferenceCount() 51 { 52 return mReferences.size(); 53 } 54 55 59 public Method getMethod() 60 { 61 return (Method) getFieldOrMethod(); 62 } 63 64 68 69 public void addReference(InvokeReference aRef) 70 { 71 mReferences.add(aRef); 72 } 73 74 78 public Type[] getArgumentTypes() 79 { 80 return getMethod().getArgumentTypes(); 81 } 82 83 90 public boolean isCompatible(Method aMethod) 91 { 92 return isCompatible(aMethod.getName(), aMethod.getArgumentTypes()); 93 } 94 95 102 public boolean isCompatible(MethodDefinition aMethodDef) 103 { 104 return isCompatible(aMethodDef.getMethod()); 105 } 106 107 116 public boolean isAsNarrow(MethodDefinition aMethodDef) 117 { 118 return aMethodDef.isCompatible(this); 119 } 128 129 137 public boolean isCompatible(String aMethodName, Type[] aArgTypes) 138 { 139 if (!getName().equals(aMethodName)) { 141 return false; 142 } 143 final Type[] methodTypes = getArgumentTypes(); 145 if (methodTypes.length != aArgTypes.length) { 146 return false; 147 } 148 for (int i = 0; i < aArgTypes.length; i++) { 149 if (!Utils.isCompatible(aArgTypes[i], methodTypes[i])) { 150 return false; 151 } 152 } 153 return true; 154 } 155 156 163 public boolean hasReference(JavaClass aJavaClass) 164 { 165 final Iterator it = getReferences().iterator(); 166 while (it.hasNext()) { 167 final InvokeReference invokeRef = (InvokeReference) it.next(); 168 final String invokeClassName = invokeRef.getClassName(); 169 if (Repository.instanceOf(aJavaClass, invokeClassName)) { 170 return true; 171 } 172 } 173 return false; 174 } 175 } 176 | Popular Tags |