over_optimistic
over_optimistic

Reputation: 1419

When to use CallNonvirtualObjectMethod and its related methods?

It seems like CallStaticObjectMethod, CallObjectMethodMethod and etc... are all you need. According to the docs

CallNonvirtual<type>Method families of routines and the Call<type>Method families of routines are different. Call<type>Method routines invoke the method based on the class of the object, while CallNonvirtual<type>Method routines invoke the method based on the class, designated by the clazz parameter, from which the method ID is obtained. The method ID must be obtained from the real class of the object or from one of its superclasses.

From that description it also sounds that the specific method CallNonvirtualObjectMethod has no use.

Upvotes: 2

Views: 1444

Answers (1)

As the documentation says, Call<Type>Method calls the method in the object's actual class, while CallNonvirtual<Type>Method calls the method in the class you specify.

Consider this Java code:

public class A {
    public void doSomething() {
        System.out.println("A.doSomething " + this.getClass().getName());
    }
}

public class B extends A {
    public void doSomething() {
        System.out.println("B.doSomething " + this.getClass().getName());
    }
}

public class Test {
    public static native jniTest(B b);
    public static void main(String[] args) {
        B obj = new B();
        jniTest(obj);
    }
}

If jniTest tries to call doSomething on its parameter using CallVoidMethod, it will print "B.doSomething B". If it uses CallNonvirtualVoidMethod, it will print "A.doSomething B".

This is the same mechanism used for super. calls in Java (by the invokespecial bytecode); however, it is not restricted to calling methods in the current class's immediate superclass - you can't do super.super.something() or new B().super.doSomething() in Java, but you can with JNI.

Upvotes: 8

Related Questions