10000 Can't call some Java methods · Issue #232 · gudzpoz/luajava · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Can't call some Java methods #232

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

Open
Saturn745 opened this issue Apr 7, 2025 · 1 comment
Open

Can't call some Java methods #232

Saturn745 opened this issue Apr 7, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Saturn745
Copy link

Describe the bug
When attempting to call some Java methods from Lua an IllegalAccessException is thrown

To Reproduce
Steps to reproduce the behavior:

  • Create a public interface with some methods
  • Create an implementation of that interface as package-private (no access modifier)
  • Add a method (static or otherwise, doesn't matter) in the same package that returns the interface type, but actually returns the package-private implementation (in the example below, it's a static method inside the interface, but any approach works as long as it returns the implementation)
  • Call the class’s methods normally in Java — everything works as expected
  • Try calling the same method via LuaJava (or plain reflection) — you’ll get an IllegalAccessException
public interface TestClass {
    static TestClass create() {
        return new TestClassImpl();
    }

    void test();
}
final class TestClassImpl implements TestClass {

    TestClassImpl() {
        System.out.println("TestClass constructor");
    }

    public void test() {
        System.out.println("TestClass.test()");
    }
}

Current behavior
LuaJava throws an IllegalAccessException.

Expected behavior
The method should be called normally.


Platform:

  • Arch: x86_64
  • OS: Linux
  • Lua Version: LuaJIT

Additional context
I realize this is more of a Java reflection quirk, but I figured it was worth opening this to get ideas on how to work around it.

One possible solution I came up with: before invoking the method, check if it's inaccessible but public (to avoid calling anything that's meant to stay private). If that’s the case, call setAccessible(true), which seems to let the call go through just fine.

@Saturn745 Saturn745 added the bug Something isn't working label Apr 7, 2025
@Saturn745
Copy link
Author

I have confirmed that checking if a method is public and inaccessible then setting it accessible does seem to resolve this issue. However is there another way?

    public static int methodInvoke(Lua L, Method method, @Nullable Object obj, Object[] objects) {
        Object ret;

        if (Modifier.isPublic(method.getModifiers()) && !method.isAccessible()) {
            method.setAccessible(true);
        }

        try {
            ret = method.invoke(obj, objects);
        } catch (IllegalAccessException e) {
            return L.error(e);
        } catch (InvocationTargetException e) {
            return L.error(e.getCause());
        }
        if (ret == null) {
            return 0;
        } else {
            L.push(ret, Lua.Conversion.SEMI);
            return 1;
        }
    }

Saturn745 added a commit to LuaLink/luajava that referenced this issue Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants
0