vendredi 14 décembre 2018

Is it possible to replace an implementation of a method of a dependency library in Java?

We have a situation where we are using some library and we would like to customize the logic of a tiny private method used in the library. Unfortunately, the library isn't well written to be extended.

So basically, imagine if we have a library with the class ClassA like this one:

public class ClassA {

    public String buildMessage(){
        return "Hello, "+ buildSuffixString();
    }

    private String buildSuffixString(){
        return "World!";
    }
}

The class ClassA is used in the library by the class ClassB. ClassB is used in the library by ClassC and so on. ClassB creates its dependency in the constructor with the code like classA = new ClassA(); (Unfortunatelly). ClassC creates its dependency in the constructor with the code like classB = new ClassB();. And so on.

We would like to force everyone who uses a method buildMessage() of ClassA to obtain Hello, Kitty! message instead of Hello, World! (i.e. we need to replace an implementation of the private method buildSuffixString() to our own implementation). Is there a way to change a logic of a private method by using reflection or Java compiler or something else?





Aucun commentaire:

Enregistrer un commentaire