jnipp icon indicating copy to clipboard operation
jnipp copied to clipboard

Crash on calling static method from class

Open LADIlib opened this issue 9 months ago • 0 comments

Example source code in JAVA

public final class TestClass
{
    TestClass(){
        
    }
    private static TestClass testSingleton;
    public static TestClass instance() { return testSingleton;}
}

Example source code in C++

    jni::init(env);

    jni::Class testClass("com/test/TestClass"_e);
    auto testClassInstance = testClass.call<jni::Object>("instance()Lcom/test/TestClass;");

This code in C++ causes crash jni::NameResolutionException

This method tries to call static method by name adding own signature causing JVM trying to find non-existing method (instance()V)

        template <class TReturn>
        TReturn call(const char* name) const {
            method_t method = getStaticMethod(name, ("()" + internal::valueSig((TReturn*) nullptr)).c_str());
            return call<TReturn>(method);
        }

Simple fix - add getStaticMethod prototype selection if name has signature

        template <class TReturn>
        TReturn call(const char* name) const {
            method_t method;
            if (std::strchr(name, '('))
                method = getStaticMethod(name); // signature already in "name"
            else
                method = getStaticMethod(name, ("()" + internal::valueSig((TReturn*) nullptr)).c_str()); // no signature in "name"
            return call<TReturn>(method);
        }

LADIlib avatar Mar 20 '25 21:03 LADIlib