Before you read this: I know that the following is a pretty nasty piece of code. I am ashamed of it and will eliminate it as soon as possible but for the time beeing I need to make it work.
In my project (project A) I depend on another project (project B) which is imported via gradle from jcenter.
somewhere in project B there is this class
public class Postfixer{
static final string postfix = " bad postfix";
postfix(String input){
input += postfix;
/*do something with input*/
}
}
in project A there ist this functioncall
/*do something*/
System.out.println(new Postfixer().postfix("Hello there"));
/*do even more*/
I really have to replace the postfix before /*do something with input*/
So I did the following
Field postfixField = Postfixer.class.getDeclaredField("postfix");
postfixField .setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(postfixField, field.getModifiers() & ~Modifier.FINAL);
postfixField.set(null, " better postfix");
It seems to have worked, when i run the code there is no exception at all but the execution of Postfixer is not affected at all. The output of System.out.println(new Postfixer().postfix("Hello there"));
is still "Hello there bad postfix"
Do you see any mistake that i could have made? PS: This is horribly simplified. Nearly all of the code I am trying to change is in the form of binaries and all of this happens deep deep down in the callstack far from my own code.
Aucun commentaire:
Enregistrer un commentaire