1 18 19 package alt.jiapi.instrumentor; 20 21 import org.apache.log4j.Category; 22 23 import alt.jiapi.Runtime; 24 import alt.jiapi.reflect.JiapiClass; 25 import alt.jiapi.reflect.JiapiField; 26 import alt.jiapi.reflect.JiapiMethod; 27 import alt.jiapi.reflect.Instruction; 28 import alt.jiapi.reflect.InstructionList; 29 30 import alt.jiapi.reflect.instruction.Invocation; 31 import alt.jiapi.reflect.instruction.OpcodeGroups; 32 import alt.jiapi.reflect.instruction.FieldAccess; 33 import alt.jiapi.reflect.instruction.ReferencingInstruction; 34 35 41 public class ChangeReferenceInstrumentor extends AbstractInstrumentor { 42 private static Category log = Runtime.getLogCategory(ChangeReferenceInstrumentor.class); 43 44 private String replacement; 46 private int mode; 47 private byte[] bytesToScan; 48 private String patternString; 50 51 54 public static final int CHANGE_FIELD_REFERENCES = 1; 55 56 59 public static final int CHANGE_METHODCALL_REFERENCES = 2; 60 61 65 public static final int CHANGE_ALL = 66 CHANGE_FIELD_REFERENCES + 67 CHANGE_METHODCALL_REFERENCES; 68 69 70 public ChangeReferenceInstrumentor(String pattern, String replacement) { 71 this(CHANGE_ALL, pattern, replacement); 72 } 73 74 public ChangeReferenceInstrumentor(int mode, String pattern, 75 String replacement) { 76 this.mode = mode; 77 this.patternString = pattern; 78 try { 79 } 85 catch(Exception e) { 86 log.error("Invalid regular expression: " + pattern); 87 } 88 89 this.replacement = replacement; 90 91 int length = 0; 92 if ((mode & CHANGE_FIELD_REFERENCES) == CHANGE_FIELD_REFERENCES) { 93 length = OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS.length; 94 } 95 if ((mode & CHANGE_METHODCALL_REFERENCES) == 96 CHANGE_METHODCALL_REFERENCES) { 97 length += OpcodeGroups.INVOKE_INSTRUCTIONS.length; 98 } 99 100 this.bytesToScan = new byte[length]; 101 int targetPosition = 0; 102 if ((mode & CHANGE_FIELD_REFERENCES) == CHANGE_FIELD_REFERENCES) { 103 System.arraycopy(OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS, 0, 104 bytesToScan, targetPosition, 105 OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS.length); 106 targetPosition = OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS.length; 107 } 108 if ((mode & CHANGE_METHODCALL_REFERENCES) == 109 CHANGE_METHODCALL_REFERENCES) { 110 System.arraycopy(OpcodeGroups.INVOKE_INSTRUCTIONS, 0, 111 bytesToScan, targetPosition, 112 OpcodeGroups.INVOKE_INSTRUCTIONS.length); 113 114 } 117 118 log.info("Pattern is " + pattern + ", replacement is " + replacement); 119 } 120 121 122 public void instrument(InstructionList il) { 123 int index = 0; 124 String __replacement = null; 125 if (this.replacement == null) { 126 __replacement = getCurrentClass().getName(); 127 } 128 else { 129 __replacement = this.replacement; 130 } 131 132 while ((index = il.indexOf(bytesToScan, index)) != -1) { 133 ReferencingInstruction i = (ReferencingInstruction)il.get(index); 134 135 if (((mode & CHANGE_FIELD_REFERENCES) == CHANGE_FIELD_REFERENCES)&& 136 (i instanceof FieldAccess)) { 137 changeReference(i, __replacement); 138 } 139 else if (((mode & CHANGE_METHODCALL_REFERENCES) == CHANGE_METHODCALL_REFERENCES) && 140 (i instanceof Invocation)) { 141 changeReference(i, __replacement); 142 } 143 144 index++; 145 } 146 147 forward(il); 148 } 149 150 151 private void changeReference(ReferencingInstruction i, 152 String __replacement) { 153 String referencedType = i.getReferencedTypeName(); 154 155 if (true ) { 156 159 throw new RuntimeException ("NOT IMPLEMENTED"); 163 } 164 else { 165 log.debug("Won't change reference to " + referencedType); 166 } 167 } 168 } 169 | Popular Tags |