question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] extend any class with `@Graft.*` API

See original GitHub issue

Extend existing classes by grafting bytecode from Transplant classes. Transplants are plain java classes; annotations define interactions with the target class.

A custom classloader is responsible for applying the transformations at load-time. The classloader is setup either using a javaagent or prior to referencing any targeted classes.

Sprung from #570, refer to it for the discussion leading up to this.

Use-cases

  • Functionality for editors: callbacks and tighter tooling integration
  • Additional debugging capabilities: logging, record stack traces, additional callbacks
  • Extending existing functionality for final or otherwise non-extendable classes
  • The odd bug fix in imported dependencies

Example Transplant for ComponentMapper

@Graft.Target(ComponentMapper.class)
public class ComponentMapperTransplant<A> {

    @Graft.Mock // only needed for compilation
    private World world;

    @Graft.Fuse // inject into original method; 
    public A create(int entityId) {
        LifecyclePlugin.dispatcher.onComponentPreCreate(world, entityId);
        A result = create(entityId); // create(entity) refers to original method
        LifecyclePlugin.dispatcher.onComponentPostCreate(world, entityId);
        return result;
    }
}

API

  • @Graft.Target specifies which class to transplant to.
  • @Graft.Fuse transplants bytecode over to @Graft.Target, translating any references to ComponentMapperTransplant -> ComponentMapper.
  • @Graft.Mock to keep the compiler happy when you need to reference fields or methods in the target class. Mocked references point to target class after transplant.
  • Any non-annotated methods or fields are copied over as-is.

Nice to have:

  • @Graft.Replace: Like @Graft.Fuse but removes the original method. Better to resolve it when applying @Graft.Fuse.
  • @Graft.Remove: Remove field or method from target class.

Caveats

  • You’re working against internal implementation; there are no semver guarantees
  • No retrofitting of interfaces or parent type on target class
  • Annotations aren’t fused (is there a use-case?)
  • No @Graft.Fuse for constructors; nice to have, but not initially
  • No GWT support
  • No android support (possible but not planned)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
junkdogcommented, May 21, 2019

PSA: a lot of stuff at work and outside work for the remainder of the week; prob won’t have time to do any work on it before the weekend

0reactions
junkdogcommented, Jun 30, 2019

Closing as junkdog/graftt is its own project

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add custom data to resources using extensions - Microsoft Learn
You can extend Microsoft Graph with your own application data. Add custom properties for storing custom data in Microsoft Graph resources ...
Read more >
This Is How You Extend Your Classes Without Modifying Them
Preferably a simple, easy way out. In this article we're looking into the following. Extract an interface to apply the decorator pattern ...
Read more >
Extending the model for Microsoft Graph | PnP Core SDK
Extending the model for Microsoft Graph. The PnP Core SDK model contains model, collection, and complex type classes which are populated via either ......
Read more >
Extend a Python class functionality from a different class
I want to extend all of the classes with some common functionalities. If I extend each class individually, we introduce a lot of...
Read more >
4.2 Extends, Casting, Higher Order Functions · Hug61B
What if we wanted to define a hierarchical relationship between classes? Suppose we want to build a RotatingSLList that has the same functionality...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found