From ff2a63c99af8f97efb3151f9b6f7f51de9935fd6 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Sun, 18 Apr 2021 14:30:04 -0400 Subject: [PATCH 1/2] feat: implement calling static boolean methods Signed-off-by: Chris Waldon --- gojni.h | 1 + jni.c | 4 ++++ jni.go | 6 ++++++ jni_test.go | 38 ++++++++++++++++++++++++++++++++++++++ test/AClass.class | Bin 3263 -> 3241 bytes test/AClass.java | 6 +++++- 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/gojni.h b/gojni.h index 008dfd6..9a1e065 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 2aae752..1a06b95 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 22ec8f4..6035453 100644 --- a/jni.go +++ b/jni.go @@ -104,6 +104,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 f5c6c00..4d1dcb0 100644 --- a/jni_test.go +++ b/jni_test.go @@ -198,6 +198,44 @@ func TestStaticObjectMethod(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) + } + if err := CallStaticVoidMethod(env, cls, setterMid, TRUE); err != nil { + t.Errorf("Setter invocation failed") + } + return err + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + func TestMethod(t *testing.T) { defer func() { if r := recover(); r == nil { diff --git a/test/AClass.class b/test/AClass.class index 45d3b33a4b21454b4f17105261032ba6d0a63260..60ccca6be55423f91e2ba1535e9f1e10cce7cd34 100644 GIT binary patch literal 3241 zcmZuzS$k7e6kRt-GxgGj&@yy@Ql|7Wl+U6FW#|BfLM>Rr)CFHb_Gj`y=6XMk-ejRN`R-bgXRbSbcXzcg8aMq1; zUd+aMiT;8YSzIhe__wI%94n9JBfP{2+puW&%|B)TvXJkDd@tmRkgGy|5b~ptYeKFI z`N@T!HF$)T3cA}oPNXQVGsRtEy2})!VT!9Xc+(?7^ArRL!(7v9aV-H9*W$gXtXvll z#BXsePK#^tXjc`miG5p`hHYZeCZ=rhB#G$SM9C&-M_R${X~C0H!JZZkds~yQz)F^j&DsY6I6ZaNTm|7 zkQfUzvHIgm2m3Pwa<^mTZfAdJQ0=l86(%;=pBj`115NCSZf>a5b5)2~HhWUZ zJ{6`64CSswDwL0#Bje%5s?@M58Z+V4I2nra<7$saxfs$=7U)*#c&HieOoWEGH!CnA zk6>mnp6qKKnVN!e`n{|7MPlhpJe8zUMIU1n{T>>MvCva)E60clEB-!L1S8a&RywltM8x*HD{}d1-@L z!G>6|E|SA9I&R=s7jEi!6|d>&7MJVA=yg>7KP3H45EI?Cp@_7{-T$)$N^ zHCaq86;b}J0B^m6bKt>aI567qv~!jMtS9nu92~=Njn(i58s13Y6Xp=jDGX7$VO)gU zv6CZWRSNPxZKBd4GtGKRDtOVc=DCGOPnw;pDL`!wD{ebao-+GZTfo_UhpfOIJbKz} zn+fzcSb-4&58u``3DPm%1}n0U$Y<~@X@ZD47F{8$F#WWpxN_N^!}A4X$@z>k7NCsk z3wW^r9AR?iSqoAI@Q67GjdU(OcN(>0)P66@I5Zkw!qI}|nQR#UN99eftsARb2jJo~ zXm}Z~(5|G{NW6IPUd8OK{yCQsxK1g5Y4ca;5RP%j!M)l1))&sA&gadd-WTP-S7B}z^Zg65Xs*J-EbcH> zTBx!}RPGd&yF_KNsNAhomWaw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|_{own1rjLTNUG+AaN1 znk`Y9T~V5iQJTF`n(a}V9a5T2Qks3D)`TodlP^k>GD?#*N|QKBlRHY2K1!28N|Qv? z7U7Z7q>|EPlhP!V(&UuVq?OWSmeM4b(&QI)H7g>mDYw!33s$axJ^r)M{1;$ZPo06) zZ_U8w_k=Sj@p}d`u=_ow894l&NCr-SC0rSl`n`Gv6KIrW;K}<|h_6@qPE@{?%C}1S zR%d-DDc{Li-zg_qfQC5bYD}bb!G@_QM*}L+h#C&J5ly%mH(?eB+=dyLPpbny9z;OyS=TLjXOlnsT+6aCKBL=w60NYNX z?nn`G9+9I+jYrlOBio4_!}jsWsl~`8MDD=O@yLc^WH*t!@UVfLMlJIzsY#dZ!fwHxc;cbz_j0*(xk3Muv%e z3QtqY%TdQJDr6;^j?&I@G)tF3TF>BF1DQ`g{a6ujD`Dwa>nr@WP+G|=46|g zT!v`(*WRG_qbN})e*ycA$kRD6EAdW$ZQYHgwF3?{rx)=ON^_@k_ zC0d1t+>Ha=v2m}NfBa?`xf-dZF;4#(@ONf0mG;7kgC> Date: Mon, 31 May 2021 16:11:30 -0400 Subject: [PATCH 2/2] fix: use proper return type for static booleans Signed-off-by: Chris Waldon --- jni.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jni.c b/jni.c index 1a06b95..e66bd9f 100644 --- a/jni.c +++ b/jni.c @@ -118,7 +118,7 @@ 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) { +jboolean _jni_CallStaticBooleanMethodA(JNIEnv *env, jclass cls, jmethodID method, jvalue *args) { return (*env)->CallStaticBooleanMethodA(env, cls, method, args); } -- 2.39.2