1 20 package edu.umd.cs.findbugs.detect; 21 22 23 import edu.umd.cs.findbugs.*; 24 import edu.umd.cs.findbugs.ba.ClassContext; 25 import org.apache.bcel.classfile.JavaClass; 26 import org.apache.bcel.generic.Type; 27 28 public class InefficientMemberAccess extends BytecodeScanningDetector implements StatelessDetector { 29 30 public static final String ACCESS_PREFIX = "access$"; 31 private BugReporter bugReporter; 32 private String clsName; 33 34 public InefficientMemberAccess(BugReporter bugReporter) { 35 this.bugReporter = bugReporter; 36 } 37 38 39 40 @Override 41 public void visitClassContext(ClassContext classContext) { 42 JavaClass cls = classContext.getJavaClass(); 43 clsName = cls.getClassName(); 44 if (clsName.indexOf("$") >= 0) 45 super.visitClassContext(classContext); 46 } 47 48 @Override 49 public void sawOpcode(int seen) { 50 int varSlot; 51 if (seen == INVOKESTATIC) { 52 String methodName = getNameConstantOperand(); 53 if (!methodName.startsWith(ACCESS_PREFIX)) 54 return; 55 try { 56 varSlot = Integer.parseInt(methodName.substring(ACCESS_PREFIX.length())); 57 } 58 catch (NumberFormatException nfe) { 59 return; 60 } 61 String methodSig = getSigConstantOperand(); 62 Type[] argTypes = Type.getArgumentTypes(methodSig); 63 if ((argTypes.length < 1) || (argTypes.length > 2)) 64 return; 65 String parCls = argTypes[0].getSignature(); 66 if (parCls.length() < 3) return; 67 parCls = parCls.substring(1, parCls.length() - 1); 68 if (!parCls.equals(getClassConstantOperand())) 69 return; 70 if ((argTypes.length == 2) && !argTypes[1].getSignature().equals(Type.getReturnType(methodSig).getSignature())) 71 return; 72 73 bugReporter.reportBug(new BugInstance(this, "IMA_INEFFICIENT_MEMBER_ACCESS", LOW_PRIORITY) 74 .addClassAndMethod(this) 75 .addSourceLine(this)); 76 } 77 } 78 79 80 81 82 } 83 | Popular Tags |