diff --git a/xwords4/android/jni/andutils.c b/xwords4/android/jni/andutils.c index f0f87663a..237b90ae2 100644 --- a/xwords4/android/jni/andutils.c +++ b/xwords4/android/jni/andutils.c @@ -510,21 +510,30 @@ setTypeSetFieldIn( JNIEnv* env, const CommsAddrRec* addr, jobject jTarget, deleteLocalRef( env, jtypset ); } +jobject +makeObject( JNIEnv* env, const char* className, const char* initSig, ... ) +{ + jclass clazz = (*env)->FindClass( env, className ); + XP_ASSERT( !!clazz ); + jmethodID mid = (*env)->GetMethodID( env, clazz, "", initSig ); + XP_ASSERT( !!mid ); + + va_list ap; + va_start( ap, initSig ); + jobject result = (*env)->NewObjectV( env, clazz, mid, ap ); + va_end( ap ); + + deleteLocalRef( env, clazz ); + return result; +} + jobject makeJAddr( JNIEnv* env, const CommsAddrRec* addr ) { jobject jaddr = NULL; if ( NULL != addr ) { - jclass clazz - = (*env)->FindClass(env, PKG_PATH("jni/CommsAddrRec") ); - XP_ASSERT( !!clazz ); - jmethodID mid = getMethodID( env, clazz, "", "()V" ); - XP_ASSERT( !!mid ); - - jaddr = (*env)->NewObject( env, clazz, mid ); + jaddr = makeObject( env, PKG_PATH("jni/CommsAddrRec"), "()V" ); setJAddrRec( env, jaddr, addr ); - - deleteLocalRef( env, clazz ); } return jaddr; } @@ -580,12 +589,8 @@ jobject addrTypesToJ( JNIEnv* env, const CommsAddrRec* addr ) { XP_ASSERT( !!addr ); - jclass cls = - (*env)->FindClass( env, PKG_PATH("jni/CommsAddrRec$CommsConnTypeSet") ); - XP_ASSERT( !!cls ); - jmethodID initId = (*env)->GetMethodID( env, cls, "", "()V" ); - XP_ASSERT( !!initId ); - jobject result = (*env)->NewObject( env, cls, initId ); + jobject result = makeObject( env, PKG_PATH("jni/CommsAddrRec$CommsConnTypeSet"), + "()V" ); XP_ASSERT( !!result ); jmethodID mid2 = getMethodID( env, result, "add", @@ -600,7 +605,6 @@ addrTypesToJ( JNIEnv* env, const CommsAddrRec* addr ) (*env)->CallBooleanMethod( env, result, mid2, jtyp ); deleteLocalRef( env, jtyp ); } - deleteLocalRef( env, cls ); return result; } @@ -708,14 +712,9 @@ intToJenumField( JNIEnv* env, jobject jobj, int val, const char* field, jobject jenum = (*env)->GetObjectField( env, jobj, fid ); if ( !jenum ) { /* won't exist in new object */ - jclass clazz = (*env)->FindClass( env, fieldSig ); - XP_ASSERT( !!clazz ); - jmethodID mid = getMethodID( env, clazz, "", "()V" ); - XP_ASSERT( !!mid ); - jenum = (*env)->NewObject( env, clazz, mid ); + jenum = makeObject( env, fieldSig, "()V" ); XP_ASSERT( !!jenum ); (*env)->SetObjectField( env, jobj, fid, jenum ); - deleteLocalRef( env, clazz ); } jobject jval = intToJEnum( env, val, fieldSig ); diff --git a/xwords4/android/jni/andutils.h b/xwords4/android/jni/andutils.h index 935d6a89a..d31636d17 100644 --- a/xwords4/android/jni/andutils.h +++ b/xwords4/android/jni/andutils.h @@ -92,6 +92,8 @@ jbyteArray streamToBArray( JNIEnv *env, XWStreamCtxt* stream ); jmethodID getMethodID( JNIEnv* env, jobject obj, const char* proc, const char* sig ); +jobject makeObject( JNIEnv* env, const char* className, const char* initSig, ... ); + jobject makeJAddr( JNIEnv* env, const CommsAddrRec* addr ); jobject setJAddrRec( JNIEnv* env, jobject jaddr, const CommsAddrRec* addr ); void getJAddrRec( JNIEnv* env, CommsAddrRec* addr, jobject jaddr ); diff --git a/xwords4/android/jni/drawwrapper.c b/xwords4/android/jni/drawwrapper.c index a461803a6..1c8f25845 100644 --- a/xwords4/android/jni/drawwrapper.c +++ b/xwords4/android/jni/drawwrapper.c @@ -60,14 +60,11 @@ makeJRect( AndDraw* draw, JNIEnv* env, int indx, const XP_Rect* rect ) int bottom = rect->top + rect->height; if ( !robj ) { - jclass rclass = (*env)->FindClass( env, "android/graphics/Rect"); - jmethodID initId = (*env)->GetMethodID( env, rclass, "", - "(IIII)V" ); - robj = (*env)->NewObject( env, rclass, initId, rect->left, rect->top, - right, bottom ); + robj = makeObject(env, "android/graphics/Rect", "(IIII)V", + rect->left, rect->top, right, bottom ); draw->jCache[indx] = (*env)->NewGlobalRef( env, robj ); - deleteLocalRefs( env, robj, rclass, DELETE_NO_REF ); + deleteLocalRef( env, robj ); robj = draw->jCache[indx]; } else { setInt( env, robj, "left", rect->left ); @@ -187,12 +184,10 @@ makeDSI( AndDraw* draw, XWEnv xwe, int indx, const DrawScoreInfo* dsi ) jobject dsiobj = draw->jCache[indx]; if ( !dsiobj ) { - jclass rclass = (*env)->FindClass( env, PKG_PATH("jni/DrawScoreInfo") ); - jmethodID initId = (*env)->GetMethodID( env, rclass, "", "()V" ); - dsiobj = (*env)->NewObject( env, rclass, initId ); + dsiobj = makeObject( env, PKG_PATH("jni/DrawScoreInfo"), "()V" ); draw->jCache[indx] = (*env)->NewGlobalRef( env, dsiobj ); - deleteLocalRefs( env, rclass, dsiobj, DELETE_NO_REF ); + deleteLocalRef( env, dsiobj ); dsiobj = draw->jCache[indx]; } diff --git a/xwords4/android/jni/utilwrapper.c b/xwords4/android/jni/utilwrapper.c index 10e9ab3df..00e4c3341 100644 --- a/xwords4/android/jni/utilwrapper.c +++ b/xwords4/android/jni/utilwrapper.c @@ -856,17 +856,13 @@ and_dutil_onInviteReceived( XW_DUtilCtxt* duc, XWEnv xwe, const NetLaunchInfo* n DUTIL_CBK_HEADER( "onInviteReceived", "(L" PKG_PATH("NetLaunchInfo") ";)V" ); /* Allocate a new NetLaunchInfo */ - jclass cls = (*env)->FindClass( env, PKG_PATH("NetLaunchInfo") ); - XP_ASSERT( !!cls ); - jmethodID initId = (*env)->GetMethodID( env, cls, "", "()V" ); - XP_ASSERT( !!initId ); - jobject jnli = (*env)->NewObject( env, cls, initId ); + jobject jnli = makeObject( env, PKG_PATH("NetLaunchInfo"), "()V" ); XP_ASSERT( !!jnli ); setNLI( env, jnli, nli ); (*env)->CallVoidMethod( env, dutil->jdutil, mid, jnli ); - deleteLocalRefs( env, jnli, cls, DELETE_NO_REF ); + deleteLocalRef( env, jnli ); DUTIL_CBK_TAIL(); }