2

How do I rename things using BCEL? So far what I do is go through each method in a class and create a new NameAndType constant in the constant pool, then I replace the old NameAndType constant with that one

int nameRef = cpg.addNameAndType(newName, m.getSignature());
cpg.setConstant(m.getNameIndex(), cpg.getConstant(nameRef));

This seems like it should work but my decompiler tells me the constant pool is corrupt after this. Am I missing a step or something?

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
Contra
  • 1,691
  • 15
  • 14

1 Answers1

1

You're assuming that the ConstantPoolGen is just a table of indices and values. But I believe that actually, it has a much more complicated internal structure. Looking at the ConstantPoolGen method, it is clear that it also holds references to MethodGen objects which may or not me updated when you run your code.

I advise you not to manipulate the Constant Pool directly, but to get each method of the class (as a MethodGen object), and use the method setName(). The constant pool will be updated automatically.

H-H
  • 4,431
  • 6
  • 33
  • 41
  • Thank you :) Now I just have to figure out a way to update all of the method references in other classes – Contra Dec 13 '10 at 02:25
  • You can go through every invoke statement and change its method name. Most probably the new method will be added to the Constant Pool. However, you may have to manually clean the CP to remove the old method name. – H-H Dec 13 '10 at 11:28