Skip to content

Add a system to make creating a delegate-style class easier. #46

@cputnam-a11y

Description

@cputnam-a11y

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions