Search code examples
javacjava-native-interfacejavacpp

JavaCPP ValueGetter vs MemberGetter


What is the difference between @MemberGetter and @ValueGetter annotations in JavaCPP?

What are the use cases for them?

I have C header file with two constants

static const int TRANSPOSE = 0;
static const int NO_TRANSPOSE = 1;

What is the prefferred way to get their values at Java side?


UPDATE:

I've read JavaCPP Generator source code, done some experiments and ended up doing this in both ways and both are working. Here's java sample (see our github project for more).

private static native @MemberGetter @Const int TRANSPOSE();
private static native @ValueGetter @Const int NO_TRANSPOSE();

But I still do not know what are the differences. Both annotations produced same C++ code.

JNIEXPORT jint JNICALL Java_com_rtbhouse_model_natives_NeuralNetworkNativeOps_TRANSPOSE(JNIEnv* env, jclass cls) {
    jint rarg = 0;
    const int rvalue = (const int)TRANSPOSE;
    rarg = (jint)rvalue;
    return rarg;
}

JNIEXPORT jint JNICALL Java_com_rtbhouse_model_natives_NeuralNetworkNativeOps_NO_1TRANSPOSE(JNIEnv* env, jclass cls) {
    jint rarg = 0;
    const int rvalue = (const int)NO_TRANSPOSE;
    rarg = (jint)rvalue;
    return rarg;
}

Solution

  • For global variables and macros there isn't much of any difference. In the case of classes, a pair of native get() and put() methods default to a value getter and setter, mapping to the [] operator of the pointer, usually for accessing the elements of an array. Other getter and setter pairs are for accessing fields, like native int n(); native void n(int n) are member getter and setter for the member variable int n, by default. The @ValueGetter, @ValueSetter, @MemberGetter, @MemberSetter, and @Function annotations can be used to change the default behavior and use other names for the methods.