Re: Trying to change value of static final field
Godmar Back <[email protected]>
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
I believe the compiler is allowed to propagate static finals. Check that the original bytecode actually accesses the field via GETSTATIC (rather than directly using its value via LDC #....) - Godmar On Tue, Feb 23, 2010 at 12:40 AM, <[email protected]> wrote: > Hi, > > I'm trying to change the value of a static final field in a class using a > java > agent that uses ASM. > > In the code being changed, if I access the field using reflection then the > value has changed, but if I access the field using the variable name then > it > still has the old value. > > An example of some output where I've changed the value of a static final > String > is as follows: > > Value of the static field is: Initial value for static field > Accessed via reflection, field name is: str With Value: Modified value for > static field > > Am I doing something wrong when I'm trying to change the field value? Here > are > some code samples to show what I'm doing. > > The class that is being modified is as follows: > > package test.javaagent; > > import java.lang.reflect.Field; > > import org.junit.Test; > > import junit.framework.TestCase; > > public class ClassWithStaticField extends TestCase { > private final static String str = "Initial value for static field"; > > @Test > public void test() throws IllegalArgumentException, > IllegalAccessException { > ClassWithStaticField.printValue(); > } > > public static void printValue() throws IllegalArgumentException, > IllegalAccessException { > System.out.println("Value of the static field is: " + str); > > Field[] fields = > ClassWithStaticField.class.getDeclaredFields(); > for (Field f : fields) { > System.out.println("Accessed via reflection, field > name > is: " + f.getName() > + " With Value: " + > f.get(ClassWithStaticField.class)); > } > } > } > > > > The classes making up the Java Agent and Class Adapters etc: > > package test.javaagent; > > import java.lang.instrument.ClassFileTransformer; > import java.lang.instrument.IllegalClassFormatException; > import java.lang.instrument.Instrumentation; > import java.security.ProtectionDomain; > > import org.objectweb.asm.ClassAdapter; > import org.objectweb.asm.ClassReader; > import org.objectweb.asm.ClassVisitor; > import org.objectweb.asm.ClassWriter; > import org.objectweb.asm.FieldVisitor; > > /* > * Java Agent entry > */ > public class ModifyFieldExample { > public static void premain(String agentArgs, Instrumentation inst) { > inst.addTransformer(new MyClassTransformer()); > } > } > > class MyClassTransformer implements ClassFileTransformer { > > public byte[] transform(ClassLoader arg0, String arg1, Class<?> > arg2, > ProtectionDomain arg3, byte[] arg4) > throws IllegalClassFormatException { > if (arg1.endsWith("ClassWithStaticField")) { > byte[] transformClass = arg4.clone(); > ClassReader cr = new ClassReader(transformClass); > ClassWriter cw = new ClassWriter(cr, 0); > > MyClassAdapter ca = new MyClassAdapter(cw); > > cr.accept(ca, 0); > > byte[] transformedBytes = cw.toByteArray(); > return transformedBytes; > } > return null; > } > > } > > class MyClassAdapter extends ClassAdapter { > > public MyClassAdapter(ClassVisitor arg0) { > super(arg0); > } > > @Override > public FieldVisitor visitField(int arg0, String arg1, String arg2, > String arg3, Object arg4) { > if (arg4.equals("Initial value for static field")) { > return cv.visitField(arg0, arg1, arg2, arg3, > "Modified value for static field"); > } > return cv.visitField(arg0, arg1, arg2, arg3, arg4); > } > > } > > Thanks in advance. > > Cristy > > > -- > You receive this message as a subscriber of the [email protected] mailing list. > To unsubscribe: mailto:[email protected] > For general help: mailto:[email protected]?subject=help > OW2 mailing lists service home page: http://www.ow2.org/wws > >
message-footer.txt
(text/plain, 238 B)
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws