1 19 20 package edu.umd.cs.findbugs.visitclass; 21 22 import org.apache.bcel.Repository; 23 import org.apache.bcel.classfile.Attribute; 24 import org.apache.bcel.classfile.Code; 25 import org.apache.bcel.classfile.CodeException; 26 import org.apache.bcel.classfile.Constant; 27 import org.apache.bcel.classfile.ConstantClass; 28 import org.apache.bcel.classfile.ConstantPool; 29 import org.apache.bcel.classfile.InnerClass; 30 import org.apache.bcel.classfile.InnerClasses; 31 import org.apache.bcel.classfile.JavaClass; 32 import org.apache.bcel.classfile.LineNumber; 33 import org.apache.bcel.classfile.LineNumberTable; 34 35 import edu.umd.cs.findbugs.annotations.CheckForNull; 36 37 40 public class Util { 41 47 48 @CheckForNull public static JavaClass getOuterClass(JavaClass obj) throws ClassNotFoundException { 49 for(Attribute a : obj.getAttributes()) 50 if (a instanceof InnerClasses) { 51 for(InnerClass ic : ((InnerClasses) a).getInnerClasses()) { 52 if (obj.getClassNameIndex() == ic.getInnerClassIndex()) { 53 ConstantClass oc = (ConstantClass) obj.getConstantPool().getConstant(ic.getOuterClassIndex()); 55 String ocName = oc.getBytes(obj.getConstantPool()); 56 return Repository.lookupClass(ocName); 57 } 58 } 59 } 60 return null; 61 } 62 63 public static int getSizeOfSurroundingTryBlock(ConstantPool constantPool, Code code, String vmNameOfExceptionClass, int pc) { 64 int size = Integer.MAX_VALUE; 65 int tightStartPC = 0; 66 int tightEndPC = Integer.MAX_VALUE; 67 if (code.getExceptionTable() == null) return size; 68 for (CodeException catchBlock : code.getExceptionTable()) { 69 if (vmNameOfExceptionClass != null) { 70 Constant catchType = constantPool.getConstant(catchBlock.getCatchType()); 71 if (catchType == null || catchType instanceof ConstantClass 72 && !((ConstantClass)catchType).getBytes(constantPool).equals(vmNameOfExceptionClass)) continue; 73 } 74 int startPC = catchBlock.getStartPC(); 75 int endPC = catchBlock.getEndPC(); 76 if (pc >= startPC && pc <= endPC) { 77 int thisSize = endPC - startPC; 78 if (size > thisSize) { 79 size = thisSize; 80 tightStartPC = startPC; 81 tightEndPC = endPC; 82 } 83 } 84 } 85 if (size == Integer.MAX_VALUE) return size; 86 87 size = (size+7) / 8; 89 LineNumberTable lineNumberTable = code.getLineNumberTable(); 90 if (lineNumberTable == null) return size; 91 92 int count = 0; 93 for(LineNumber line : lineNumberTable.getLineNumberTable()) { 94 if (line.getStartPC() > tightEndPC) break; 95 if (line.getStartPC() >= tightStartPC) count++; 96 } 97 return count; 98 99 } 100 101 102 } 103 | Popular Tags |