ClassTransform icon indicating copy to clipboard operation
ClassTransform copied to clipboard

Accessor

Open MatrixAura opened this issue 1 year ago • 5 comments

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?

MatrixAura avatar Aug 01 '24 05:08 MatrixAura

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

Lenni0451 avatar Aug 01 '24 06:08 Lenni0451

Can access be implemented using reflection not function? This will support hot-injection.

MatrixAura avatar Aug 16 '24 13:08 MatrixAura

That would just be regular reflection. No need to add that to ClassTransform

Lenni0451 avatar Aug 16 '24 13:08 Lenni0451

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?

MatrixAura avatar Sep 08 '24 03:09 MatrixAura

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.

Lenni0451 avatar Sep 09 '24 16:09 Lenni0451