[FEATURE] extend any class with `@Graft.*` API
See original GitHub issueExtend 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 toComponentMapperTransplant
->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:
Better to resolve it when applying@Graft.Replace
: Like@Graft.Fuse
but removes the original method.@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:
- Created 4 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Closing as junkdog/graftt is its own project