Closed rnoennig closed 3 years ago
You are out of luck in this particular scenario. A public static final primitive or string value is considered a conpile time constant by javac if assigned directly. It will be resolved to this value by javac anywhere the field is accessed. At rumtime, the fields value does therefore no longer matter as the compile time constant is filled in.
You can validate this by replacing the boolean value by Boolean.parseBoolean("false").
Thank you so much for explaining this. Good to know what's going on!
Well, I guess I have to take a look where the field is being accessed and try to modify those methods using advices. Thanks for putting in the work in making this library and giving talks about these topics!
I'm closing this issue, please get in touch if you have any further questions.
@raphw how can i access a property of a final class. Any tips? I just need to access the property of a final sub class and intercept a method of a superclass
You need to access it? From the outside? I would use method handles or reflection for this.
I just need read access to the field. I need this field to add the value as a header in the superclass method, but the method is in the superclass and the field is in the subclass that is final
You can intercept the method with advice, and make a type check in thaf advice for the subclass. Then cast and access the field from there if the type check applies.
I'd like to change a static field's value using byte-buddy in a javaagent: Original class:
I'd like to replace this class, so that all access to the field
value
will evaluate totrue
. I simulate the thirdpartylibrary with this class:My attempt using byte-buddy:
Log output:
I build the javaagent via maven and call it using
-javaagent:/path/to/TestAgent-0.0.1-SNAPSHOT-jar-with-dependencies.jar
in my eclipse run configuration.I verified via debugging that during transformation and when I access my field that the classloaders have the same ID.
Is it even possible to redefine a class so that the public static field's value has a different value?
It seems like a simple enough use case, but I might be missing some information.