Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessor #54

Open
MatrixAura opened this issue Aug 1, 2024 · 5 comments
Open

Accessor #54

MatrixAura opened this issue Aug 1, 2024 · 5 comments

Comments

@MatrixAura
Copy link

MatrixAura commented Aug 1, 2024

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?

@Lenni0451
Copy link
Owner

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

@MatrixAura
Copy link
Author

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

@Lenni0451
Copy link
Owner

Lenni0451 commented Aug 16, 2024

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

@MatrixAura
Copy link
Author

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?

@Lenni0451
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants