When extending certain classes, it can be helpful to delegate parts of implementation to another instance of said class. Given a class as follows
static class ClassyGreedyAnteater {
void doVeryImportantLogicThatIsRequiredToBeDoneCorrectlyToPreventCrashes() {
// very important logic that is specific to this instance :)
}
}
If I want to extend this class, but I still need the very important logic to take place, then I must follow a pattern like so:
@Extend(
targetClass = "ClassyGreedyAnteater",
unsafe = false
)
public interface AcidicCleverCamel {
@Field("delegate")
@Field.Final
Object delegate();
@Overrides("doVeryImportantLogicThatIsRequiredToBeDoneCorrectlyToPreventCrashes")
void doVeryImportantLogicThatIsRequiredToBeDoneCorrectlyToPreventCrashesImpl() {
callDoVeryImportantLogicThatIsRequiredToBeDoneCorrectlyToPreventCrashes(delegate());
}
@Open(
targetName = "ClassyGreedyAnteater",
name = "doVeryImportantLogicThatIsRequiredToBeDoneCorrectlyToPreventCrashes",
type = Open.Type.VIRTUAL
)
private static callDoVeryImportantLogicThatIsRequiredToBeDoneCorrectlyToPreventCrashes(
@Coerce(targetName = "ClassyGreedyAnteater") Object instance
) {
throw new AssertionError("Failed to transform class and replace @Open method");
}
}
it would be nice to have an easier way to implement this delegate pattern
When extending certain classes, it can be helpful to delegate parts of implementation to another instance of said class. Given a class as follows
If I want to extend this class, but I still need the very important logic to take place, then I must follow a pattern like so:
it would be nice to have an easier way to implement this delegate pattern