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

Efficient way to get a class reference from a given instance #302

Open
pimterry opened this issue Nov 8, 2023 · 0 comments
Open

Efficient way to get a class reference from a given instance #302

pimterry opened this issue Nov 8, 2023 · 0 comments

Comments

@pimterry
Copy link

pimterry commented Nov 8, 2023

Given an instance (matched dynamically) I want to directly access the corresponding class (so that I can patch a target method for all instances).

This feels like something that it should be possible to do efficiently, but the best way I can see to do this right now is with Java.use(instance.$className). That seems likely to be slow and potentially error prone (if the instance comes from an unexpected classloader, for example).

There is an instance.class property, but this returns <instance: java.lang.Class>, rather than <class: $CLASS_NAME>. There's also various $-prefixed single character fields, but I assume those aren't intended to be used directly, and none seems to obviously provide this.

Is there a more direct way to do this? Am I wildly off track somehow?

If not, would you be open to adding a method for this? I'd be happy to open a PR. I think in general, it might be useful to add a small section to the docs related to this, defining the JS API that's available (e.g. $className) on Java instances and classes.

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

1 participant