mercredi 8 décembre 2021

Permanently change the java class private field default value

I have a following use case: assume that we have a class

public class MyClass {
   
   private boolean isEmpty = true; 

}

As you can see here, isEmpty get initialized as true by default for any new instance of MyClass. I do not have access to this class source code - this class is included into transitive dependency jar. I need to change the default value of isEmpty to false for every new object. Also note, that field has no public setters and other possible methods in order for me to set it even per object basis. Even worse, that default reflection approach:

MyClass.class.getDeclaredField("isEmpty")

and then change the value for each object is not an option in my use case, becuase not all of the creations of this object appeared in my source code, but also there are creations of MyClass instances in already compiled code of the included JAR. So, is it, from any stretch of the imagination, possible to change the default value of isEmpty value?

I am running out of ideasm really, any help will be appreciated





Aucun commentaire:

Enregistrer un commentaire