Thanks for the response,
i've done things like this a few times, and i've always run into a bunch of weird corner cases if i screw around with the constant pool. the way i always end up doing it and getting it right is by replacing the field and then looking at every instruction and changing the instructions that reference the old field. BTW, if you use InstructionFactory, you don't have to screw around with the constant pool, which i always try to do through having been burned in the past.
What do you mean by 'changing the instructions that reference the old field'? My approach would be to use ClassGen.replaceField(old, new); And then change al references to this field at once:
if (cp.lookupFieldref(cg.getClassName(), fields[i].getName(), fields[i].getSignature()) != -1) { cp.setConstant(cp.lookupFieldref(cg.getClassName(), fields[i].getName(), fields[i].getSignature()), new ConstantFieldref(cg.getClassNameIndex(), cp.addNameAndType(fields[i].getName(), newfield.getSignature()))); }
i'm saying you should do ClassGen.replaceField, then:
for each class
for each method mg
for(InstructionHandle ih = mg.getInstructionList().getStart() ; ih != null ; ih = ih.getNext()) {
Instruction instr = ih.getInstruction();
...
FieldInstruction fi = (FieldInstruction)instr;
if(fi.getClassName().equals(className) && fi.getName().equals(oldField.getName()) && fi.getType().equals(oldField.getType()))
ih.setInstruction(InstructionFactory.createFieldAccess(blah));
}
with syntax corrected etc. Then you don't have to screw around with the constant pool at all (you let bcel do that for you), and i think that's safer. But if what you suggest above works for some fairly complicated test case (like specjvm), i say just forget about my paranoia and keep what you have.
although this is a direct modification of the constant pool, it's not as evil as my initial approach ;) But maybe I'm wrong and there are problems with this too..
PS When using replaceField, BCEL knows that the other is replaced and thus this constantpool modification could be done by BCEL..
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- "I say to you that the VCR is to the American film producer and the American public as the Boston strangler is to the woman home alone." -Jack Valenti, President, Motion Picture Association of America, Inc., before The House Subcommittee on Courts, Civil Liberties, and The Administration of Justice, August, 1982, http://cryptome.org/hrcw-hear.htm
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
