1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.util.ArrayList ; 23 import java.util.BitSet ; 24 import java.util.List ; 25 import java.util.StringTokenizer ; 26 27 import org.apache.bcel.Constants; 28 import org.apache.bcel.classfile.ClassFormatException; 29 import org.apache.bcel.classfile.Constant; 30 import org.apache.bcel.classfile.ConstantMethodref; 31 import org.apache.bcel.classfile.ConstantNameAndType; 32 import org.apache.bcel.classfile.ConstantPool; 33 import org.apache.bcel.classfile.ConstantUtf8; 34 import org.apache.bcel.classfile.JavaClass; 35 import org.apache.bcel.generic.InvokeInstruction; 36 37 import edu.umd.cs.findbugs.SystemProperties; 38 39 46 public class AssertionMethods implements Constants { 47 48 private static final boolean DEBUG = SystemProperties.getBoolean("assertionmethods.debug"); 49 50 53 private BitSet assertionMethodRefSet; 54 55 private static class UserAssertionMethod { 56 private String className; 57 private String methodName; 58 59 public UserAssertionMethod(String className, String methodName) { 60 this.className = className; 61 this.methodName = methodName; 62 } 63 64 public String getClassName() { 65 return className; 66 } 67 68 public String getMethodName() { 69 return methodName; 70 } 71 } 72 73 private static final List <UserAssertionMethod> userAssertionMethodList = new ArrayList <UserAssertionMethod>(); 74 75 static { 76 String userProperty = SystemProperties.getProperty("findbugs.assertionmethods"); 77 if (userProperty != null) { 78 StringTokenizer tok = new StringTokenizer (userProperty, ","); 79 while (tok.hasMoreTokens()) { 80 String fullyQualifiedName = tok.nextToken(); 81 int lastDot = fullyQualifiedName.lastIndexOf('.'); 82 if (lastDot < 0) 83 continue; 84 String className = fullyQualifiedName.substring(0, lastDot); 85 String methodName = fullyQualifiedName.substring(lastDot + 1); 86 userAssertionMethodList.add(new UserAssertionMethod(className, methodName)); 87 } 88 } 89 } 90 91 96 public AssertionMethods(JavaClass jclass) { 97 this.assertionMethodRefSet = new BitSet (); 98 init(jclass); 99 } 100 101 private void init(JavaClass jclass) { 102 ConstantPool cp = jclass.getConstantPool(); 103 int numConstants = cp.getLength(); 104 for (int i = 0; i < numConstants; ++i) { 105 try { 106 Constant c = cp.getConstant(i); 107 if (c instanceof ConstantMethodref) { 108 ConstantMethodref cmr = (ConstantMethodref) c; 109 ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex(), CONSTANT_NameAndType); 110 String methodName = ((ConstantUtf8) cp.getConstant(cnat.getNameIndex(), CONSTANT_Utf8)).getBytes(); 111 String className = cp.getConstantString(cmr.getClassIndex(), CONSTANT_Class).replace('/', '.'); 112 String methodSig = ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex(), CONSTANT_Utf8)).getBytes(); 113 114 String classNameLC = className.toLowerCase(); 115 String methodNameLC = methodName.toLowerCase(); 116 boolean voidReturnType = methodSig.endsWith(")V"); 117 118 if (DEBUG) { 119 System.out.print("Is " + className + "." + methodName + " assertion method: " + voidReturnType); 120 } 121 122 if (isUserAssertionMethod(className, methodName) 123 || className.endsWith("Assert") 124 && methodName.startsWith("is") 125 || voidReturnType 126 && (classNameLC.indexOf("assert") >= 0 127 || methodNameLC.startsWith("throw") 128 || methodName.startsWith("affirm") 129 || methodName.startsWith("panic") 130 || methodNameLC.equals("insist") 131 || methodNameLC.equals("usage") 132 || methodNameLC.startsWith("fail") 133 || methodNameLC.startsWith("fatal") 134 || methodNameLC.indexOf("assert") >= 0 135 || methodNameLC.indexOf("legal") >= 0 136 || methodNameLC.indexOf("error") >= 0 137 || methodNameLC.indexOf("abort") >= 0 138 || methodNameLC.indexOf("check") >= 0 || methodNameLC.indexOf("failed") >= 0)) { 139 assertionMethodRefSet.set(i); 140 if (DEBUG) { 141 System.out.println("==> YES"); 142 } 143 } else { 144 if (DEBUG) { 145 System.out.println("==> NO"); 146 } 147 } 148 } 149 } catch (ClassFormatException e) { 150 } 152 } 153 } 154 155 private static boolean isUserAssertionMethod(String className, String methodName) { 156 for (UserAssertionMethod uam : userAssertionMethodList) { 157 if (className.equals(uam.getClassName()) && methodName.equals(uam.getMethodName())) 158 return true; 159 } 160 return false; 161 } 162 163 169 public boolean isAssertionCall(InvokeInstruction inv) { 170 boolean isAssertionMethod = assertionMethodRefSet.get(inv.getIndex()); 174 return isAssertionMethod; 178 } 179 } 180 181 | Popular Tags |