Accessor
Is there a way to implement non-Transformer-Class access to a class's non-public fields? I have come up with a solution:
in OtherClass:
((FooTransformer) (Object) fooInstance).bar();
in FooTransformer:
@CShadow(makePublic = true) public abstract void bar();
instead of the unsupported fooInstance.bar(); 。 Is this possible?
ClassTransform currently does not provide a way to make accessors in the Mixin way. I'm not a fan of this style because it goes against it's very own rule of never loading a mixin class. At the moment you can do this manually by creating an interface and implementing it in a Transformer. I'll see if I can come up with a better way of doing this
Can access be implemented using reflection not function? This will support hot-injection.
That would just be regular reflection. No need to add that to ClassTransform
I tried using an interface "Accessor" that provides a method "example"
public interface Accessor {
void example();
}
, and implementing it in a transformer "ATransformer"
@CShadow(makePublic=true)
public native example();
, but this only makes it possible to access a method, not a field.
((Accessor) instance).example();
And how can I use an access function similar to this in case of hot injection if it doesn't support hot injection?
Using an accessor interface is the same way I am doing this myself. It actually works with fields if you implement the method yourself and just return/set the accessed field.
Hot injection using this way is sadly not possible. You have to resort to using reflection for that.
If you need high performance access consider using MethodHandles with invokeExact because the Java Runtime will optimize that to a direct method call.