1 4 package com.puppycrawl.tools.checkstyle.bcel.classfile; 5 6 import java.util.HashMap ; 7 import java.util.HashSet ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import org.apache.bcel.Repository; 12 import org.apache.bcel.classfile.Field; 13 import org.apache.bcel.classfile.JavaClass; 14 import org.apache.bcel.classfile.Method; 15 import org.apache.bcel.generic.Type; 16 17 18 23 public class JavaClassDefinition 24 { 25 26 private JavaClass mJavaClass; 27 28 29 private MethodDefinition[] mMethodDefs; 30 31 32 private Map mFieldDefs; 33 34 42 public JavaClassDefinition( 43 JavaClass aJavaClass, 44 Set aFieldScopes, 45 Set aMethodScopes) 46 { 47 mJavaClass = aJavaClass; 48 49 final Method[] methods = aJavaClass.getMethods(); 51 final Set methodSet = new HashSet (); 52 mMethodDefs = new MethodDefinition[methods.length]; 53 for (int i = 0; i < methods.length; i++) { 54 if (Utils.inScope(methods[i], aMethodScopes)) { 55 methodSet.add(new MethodDefinition(methods[i])); 56 } 57 } 58 mMethodDefs = 59 (MethodDefinition[]) methodSet.toArray( 60 new MethodDefinition[methodSet.size()]); 61 62 final Field[] fields = aJavaClass.getFields(); 64 mFieldDefs = new HashMap (fields.length); 65 for (int i = 0; i < fields.length; i++) { 66 if (Utils.inScope(fields[i], aFieldScopes)) { 67 mFieldDefs.put( 68 fields[i].getName(), 69 new FieldDefinition(fields[i])); 70 } 71 } 72 } 73 74 78 public JavaClass getJavaClass() 79 { 80 return mJavaClass; 81 } 82 83 87 public MethodDefinition[] getMethodDefs() 88 { 89 return mMethodDefs; 90 } 91 92 96 public FieldDefinition[] getFieldDefs() 97 { 98 return (FieldDefinition[]) mFieldDefs.values().toArray( 99 new FieldDefinition[mFieldDefs.size()]); 100 } 101 102 111 public MethodDefinition findNarrowestMethod( 112 String aClassName, 113 String aMethodName, 114 Type[] aArgTypes) 115 { 116 MethodDefinition result = null; 117 final String javaClassName = mJavaClass.getClassName(); 118 if (Repository.instanceOf(aClassName, javaClassName)) { 119 for (int i = 0; i < mMethodDefs.length; i++) { 121 if (mMethodDefs[i].isCompatible(aMethodName, aArgTypes)) { 123 if (result == null) { 124 result = mMethodDefs[i]; 125 } 126 else if (result.isCompatible(mMethodDefs[i])) { 128 result = mMethodDefs[i]; 129 } 130 } 131 } 132 } 133 return result; 134 } 135 136 141 public FieldDefinition findFieldDef(String aFieldName) 142 { 143 return (FieldDefinition) mFieldDefs.get(aFieldName); 144 } 145 146 154 public boolean hasReference( 155 MethodDefinition aMethodDef, 156 ReferenceDAO aReferenceDAO) 157 { 158 final String methodName = aMethodDef.getName(); 159 final Type[] argTypes = aMethodDef.getArgumentTypes(); 160 161 JavaClass currentJavaClass = getJavaClass(); 163 while (currentJavaClass != null) { 164 final JavaClassDefinition javaClassDef = 165 aReferenceDAO.findJavaClassDef(currentJavaClass); 166 if (javaClassDef != null) { 167 final MethodDefinition methodDef = 168 javaClassDef.findNarrowestMethod( 169 getJavaClass().getClassName(), 170 methodName, 171 argTypes); 172 if ((methodDef != null) 173 && (methodDef.hasReference(getJavaClass()))) 174 { 175 return true; 176 } 177 } 178 currentJavaClass = currentJavaClass.getSuperClass(); 179 } 180 return false; 181 } 182 183 184 public String toString() 185 { 186 return getJavaClass().toString(); 187 } 188 } 189 | Popular Tags |