1
0
forked from gmp/jni

Compare commits

...

1 Commits

Author SHA1 Message Date
878283d25c
feat: implement calling static boolean methods
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2021-04-18 14:30:04 -04:00
6 changed files with 55 additions and 0 deletions

View File

@ -23,6 +23,7 @@ __attribute__ ((visibility ("hidden"))) void _jni_DeleteGlobalRef(JNIEnv *env, j
__attribute__ ((visibility ("hidden"))) jobject _jni_NewLocalRef(JNIEnv *env, jobject obj);
__attribute__ ((visibility ("hidden"))) void _jni_DeleteLocalRef(JNIEnv *env, jobject obj);
__attribute__ ((visibility ("hidden"))) jint _jni_CallStaticIntMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jboolean _jni_CallStaticBooleanMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jobject _jni_CallStaticObjectMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jobject _jni_NewObjectA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) void _jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args);

4
jni.c
View File

@ -118,6 +118,10 @@ jint _jni_CallStaticIntMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue
return (*env)->CallStaticIntMethodA(env, cls, method, args);
}
jint _jni_CallStaticBooleanMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args) {
return (*env)->CallStaticBooleanMethodA(env, cls, method, args);
}
jobject _jni_CallStaticObjectMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args) {
return (*env)->CallStaticObjectMethodA(env, cls, method, args);
}

6
jni.go
View File

@ -102,6 +102,12 @@ func CallStaticIntMethod(e Env, cls Class, method MethodID, args ...Value) (int,
return int(res), exception(e)
}
// CallStaticBooleanMethod calls a static method on a Java class, returning an int.
func CallStaticBooleanMethod(e Env, cls Class, method MethodID, args ...Value) (bool, error) {
res := C._jni_CallStaticBooleanMethodA(e.env, C.jclass(cls), C.jmethodID(method), varArgs(args))
return res == TRUE, exception(e)
}
// FindClass returns a reference to a Java class with a given name, using the
// JVM's default class loader. Any exceptions caused by the underlying JNI call
// (for example if the class is not found) will result in a panic.

View File

@ -122,6 +122,41 @@ func TestStaticIntMethod(t *testing.T) {
}
}
func TestStaticBooleanMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetStaticMethodID(env, cls, "GetStaticBoolean", "()Z")
if mid == nil {
t.Errorf("MethodID is nil")
}
setterMid := GetStaticMethodID(env, cls, "SetStaticBoolean", "(Z)V")
if setterMid == nil {
t.Errorf("Setter MethodID is nil")
}
res, err := CallStaticBooleanMethod(env, cls, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if !res {
t.Errorf("Method returned %v, not expected value of %v.", res, true)
}
if err := CallStaticVoidMethod(env, cls, setterMid, FALSE); err != nil {
t.Errorf("Setter invocation failed")
}
res, err = CallStaticBooleanMethod(env, cls, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res {
t.Errorf("Method returned %v, not expected value of %v.", res, false)
}
return err
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestIntMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")

Binary file not shown.

View File

@ -2,6 +2,7 @@ package test;
public class AClass {
public static int staticintval = 100;
public static boolean staticboolval = true;
public int intval = 0;
@ -9,10 +10,18 @@ public class AClass {
return staticintval;
}
public static boolean GetStaticBoolean() {
return staticboolval;
}
public static void SetStaticInt(int val) {
staticintval = val;
}
public static void SetStaticBoolean(boolean val) {
staticboolval = val;
}
public void IncInt() {
intval = intval + 1;
}