From 878283d25cbf16f1e625fb42c65d4c4b29a35f7b Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Sun, 18 Apr 2021 14:30:04 -0400 Subject: [PATCH] feat: implement calling static boolean methods Signed-off-by: Chris Waldon --- gojni.h | 1 + jni.c | 4 ++++ jni.go | 6 ++++++ jni_test.go | 35 +++++++++++++++++++++++++++++++++++ test/AClass.class | Bin 1126 -> 1306 bytes test/AClass.java | 9 +++++++++ 6 files changed, 55 insertions(+) diff --git a/gojni.h b/gojni.h index 2d0cb86..7bcd9c6 100644 --- a/gojni.h +++ b/gojni.h @@ -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); diff --git a/jni.c b/jni.c index ef7ee95..8035686 100644 --- a/jni.c +++ b/jni.c @@ -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); } diff --git a/jni.go b/jni.go index c900a30..6ee1ac3 100644 --- a/jni.go +++ b/jni.go @@ -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. diff --git a/jni_test.go b/jni_test.go index 8301719..57dcb4a 100644 --- a/jni_test.go +++ b/jni_test.go @@ -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") diff --git a/test/AClass.class b/test/AClass.class index 608a39e722ca1b7473287c70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w^ z<7G?;R33&-*zZP*2Jr8FmV0OTg}L?a(TCjD%;{@y>8Pk+HiMq4FtOM7-KV2h2kzjx zbKo&je(Z)FEz|btiB{SScFqLNTD!ha7j3_*p&WDcpvb{a5O}WR>(mztmW1AfRHoEU zG;ba`13hmlv6ye zWovEiF~8aM;;I%q!D!HRTYWiZDJtc z%hklpDh!R6Z2Jc+^W`~iCh2O5(0t-%o-@6NK z%4rgpQx`s`z%?Q^ZpL6W?lC2&zof_wGPja`Es>O4le6c}sr_Bvn|tKfupTQfvtj&E mB5%)C&+FEGSp1*Jqmi_Dftm8jReEQxM07xttGBtD!Nz}jr-WSq diff --git a/test/AClass.java b/test/AClass.java index f5a8fd9..a0a45e6 100644 --- a/test/AClass.java +++ b/test/AClass.java @@ -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; }