1 19 package edu.umd.cs.findbugs.props; 20 21 import java.util.BitSet ; 22 import java.util.Iterator ; 23 24 import org.apache.bcel.Constants; 25 import org.apache.bcel.classfile.Method; 26 import org.apache.bcel.generic.Instruction; 27 import org.apache.bcel.generic.ReferenceType; 28 import org.apache.bcel.generic.Type; 29 30 import edu.umd.cs.findbugs.ba.CFG; 31 import edu.umd.cs.findbugs.ba.CFGBuilderException; 32 import edu.umd.cs.findbugs.ba.ClassContext; 33 import edu.umd.cs.findbugs.ba.DataflowAnalysisException; 34 import edu.umd.cs.findbugs.ba.Location; 35 import edu.umd.cs.findbugs.ba.ca.Call; 36 import edu.umd.cs.findbugs.ba.ca.CallList; 37 import edu.umd.cs.findbugs.ba.ca.CallListDataflow; 38 import edu.umd.cs.findbugs.ba.type.TypeDataflow; 39 import edu.umd.cs.findbugs.ba.type.TypeFrame; 40 41 46 public abstract class WarningPropertyUtil { 47 48 49 private static final BitSet receiverObjectInstructionSet= new BitSet (); 50 static { 51 receiverObjectInstructionSet.set(Constants.INVOKEINTERFACE); 52 receiverObjectInstructionSet.set(Constants.INVOKEVIRTUAL); 53 receiverObjectInstructionSet.set(Constants.INVOKESPECIAL); 54 receiverObjectInstructionSet.set(Constants.GETFIELD); 55 receiverObjectInstructionSet.set(Constants.PUTFIELD); 56 receiverObjectInstructionSet.set(Constants.CHECKCAST); 57 receiverObjectInstructionSet.set(Constants.INSTANCEOF); 58 } 59 60 73 private static Location pcToLocation( 74 ClassContext classContext, 75 Method method, 76 int pc) throws CFGBuilderException { 77 CFG cfg = classContext.getCFG(method); 78 for (Iterator <Location> i = cfg.locationIterator(); i.hasNext();) { 79 Location location = i.next(); 80 if (location.getHandle().getPosition() == pc) 81 return location; 82 } 83 return null; 84 } 85 86 95 private static void addReceiverObjectType( 96 WarningPropertySet propertySet, 97 ClassContext classContext, 98 Method method, 99 Location location) { 100 try { 101 Instruction ins = location.getHandle().getInstruction(); 102 103 if (!receiverObjectInstructionSet.get(ins.getOpcode())) 104 return; 105 106 TypeDataflow typeDataflow = classContext.getTypeDataflow(method); 107 TypeFrame frame = typeDataflow.getFactAtLocation(location); 108 if (frame.isValid()) { 109 Type type = frame.getInstance(ins, classContext.getConstantPoolGen()); 110 if (type instanceof ReferenceType) { 111 propertySet.setProperty(GeneralWarningProperty.RECEIVER_OBJECT_TYPE, type.toString()); 112 } 113 } 114 } catch (DataflowAnalysisException e) { 115 } catch (CFGBuilderException e) { 117 } 119 } 120 121 130 private static void addRecentlyCalledMethods( 131 WarningPropertySet propertySet, 132 ClassContext classContext, 133 Method method, 134 Location location) { 135 try { 136 CallListDataflow dataflow = classContext.getCallListDataflow(method); 137 CallList callList = dataflow.getFactAtLocation(location); 138 if (!callList.isValid()) 139 return; 140 int count = 0; 141 for (Iterator <Call> i = callList.callIterator(); count < 4 && i.hasNext(); ++count) { 142 Call call = i.next(); 143 WarningProperty prop = null; 144 switch (count) { 145 case 0: 146 prop = GeneralWarningProperty.CALLED_METHOD_1; break; 147 case 1: 148 prop = GeneralWarningProperty.CALLED_METHOD_2; break; 149 case 2: 150 prop = GeneralWarningProperty.CALLED_METHOD_3; break; 151 case 3: 152 prop = GeneralWarningProperty.CALLED_METHOD_4; break; 153 } 154 propertySet.setProperty(prop, call.getMethodName()); 156 } 157 } catch (CFGBuilderException e) { 158 } catch (DataflowAnalysisException e) { 160 } 162 } 163 164 173 public static void addPropertiesForLocation( 174 WarningPropertySet propertySet, 175 ClassContext classContext, 176 Method method, 177 Location location) { 178 addReceiverObjectType(propertySet, classContext, method, location); 179 addRecentlyCalledMethods(propertySet, classContext, method, location); 180 } 181 182 191 public static void addPropertiesForLocation( 192 WarningPropertySet propertySet, 193 ClassContext classContext, 194 Method method, 195 int pc) { 196 try { 197 Location location = pcToLocation(classContext, method, pc); 198 if (location != null) { 199 addPropertiesForLocation(propertySet, classContext, method, location); 200 } 201 } catch (CFGBuilderException e) { 202 } 204 } 205 } 206 | Popular Tags |