1 18 19 package alt.jiapi.instrumentor; 20 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 24 import org.apache.log4j.Category; 26 27 import alt.jiapi.Runtime; 28 import alt.jiapi.reflect.InstructionFactory; 29 import alt.jiapi.reflect.InstructionList; 30 import alt.jiapi.reflect.JiapiClass; 31 import alt.jiapi.reflect.JiapiField; 32 import alt.jiapi.reflect.JiapiMethod; 33 import alt.jiapi.reflect.JiapiRuntimeException; 34 import alt.jiapi.reflect.Loader; 35 36 37 44 public class FieldAssignInstrumentor extends AbstractInstrumentor { 45 private static Category log = Runtime.getLogCategory(FieldAssignInstrumentor.class); 47 48 private String fieldName; 49 private Object value; 50 private JiapiMethod getFieldValue; 51 52 public FieldAssignInstrumentor(String fieldName, Object value) { 53 this.fieldName = fieldName; 54 this.value = value; 55 56 Runtime.setFieldValue(fieldName, value); 57 58 try { 59 Loader l = new Loader(); 60 JiapiClass jc = l.loadClass("alt.jiapi.Runtime"); 61 getFieldValue = jc.getDeclaredMethod("getFieldValue", new String [] {"java.lang.String"}); 64 } 65 catch (Exception e) { 66 e.printStackTrace(); 68 throw new RuntimeException (e.toString()); 69 } 70 } 71 72 public void instrument(InstructionList il) { 73 int modifiers = il.getDeclaringMethod().getModifiers(); 75 if (Modifier.isNative(modifiers) || Modifier.isAbstract(modifiers)) { 76 log.info("Skipping abstract or native method: " + 77 il.getDeclaringMethod().getName()); 78 forward(il); 79 return; 80 } 81 82 InstructionFactory factory = 83 il.getDeclaringMethod().getInstructionFactory(); 84 85 JiapiClass jc = getCurrentClass(); 86 87 JiapiField field = null; 88 try { 89 field = jc.getDeclaredField(fieldName); 90 } 91 catch (NoSuchFieldException e) { 92 throw new JiapiRuntimeException("No such field: " + e.getMessage()); 93 } 94 95 il.add(factory.pushConstant(fieldName)); 97 il.add(factory.invoke(getFieldValue)); 98 99 il.add(factory.cast(field.getTypeName())); 101 102 il.add(factory.setField(field)); 104 105 forward(il); 106 } 107 } 108 | Popular Tags |