jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthias Nuessler <m.nuess...@web.de>
Subject Replacing inititial value of static final fields
Date Tue, 13 Dec 2005 17:40:22 GMT

I was trying to use BCEL to replace the initial value of static final fields of existing classes.
It seemed to work fine, but when the fields are referenced from other classes they seem to
contain the old values, eventhough the values in the changed class itself are updated to the
new value. Is there any way using BCEL to change these constant values in that way? Maybe
I'm not aware of certain problems regarding the constant pool?

Here is what I did. Let's say, in a class named "Hello" I have a field like this:

public static final String MESSAGE = "Hello";

Now I want to read in the class file and change the initial value:

ClassParser parser = new ClassParser("Hello.class");
JavaClass javaClass = parser.parse();
ClassGen cg = new ClassGen(javaClass);
ConstantPool cp = javaClass.getConstantPool();
ConstantPoolGen pg = new ConstantPoolGen(cp);

Field[] fields = classGen.getFields();
for (int i=0; i<fields.length; i++) {
if (fields[i].getName().equals("MESSAGE")) {
FieldGen fg = new FieldGen(ACC_PUBLIC | ACC_STATIC | ACC_FINAL, Type.STRING, "MESSAGE", pg)
fg.setInitialValue("New Message");
JavaClass changedClass = cg.getJavaClass();

Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt bei WEB.DE FreeMail: http://f.web.de/?mc=021193

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

View raw message