jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Huntwork <...@huntwork.net>
Subject Re: changing types
Date Wed, 07 Apr 2004 19:28:21 GMT
dweimers@students.cs.uu.nl wrote:
> 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())

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: bcel-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-user-help@jakarta.apache.org

"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,

To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org

View raw message