add and use makeObject utility method

This commit is contained in:
Eric House 2020-08-15 13:30:47 -07:00
parent ca4e6e29d5
commit 2338b6e017
4 changed files with 30 additions and 38 deletions

View file

@ -510,21 +510,30 @@ setTypeSetFieldIn( JNIEnv* env, const CommsAddrRec* addr, jobject jTarget,
deleteLocalRef( env, jtypset ); 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, "<init>", 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 jobject
makeJAddr( JNIEnv* env, const CommsAddrRec* addr ) makeJAddr( JNIEnv* env, const CommsAddrRec* addr )
{ {
jobject jaddr = NULL; jobject jaddr = NULL;
if ( NULL != addr ) { if ( NULL != addr ) {
jclass clazz jaddr = makeObject( env, PKG_PATH("jni/CommsAddrRec"), "()V" );
= (*env)->FindClass(env, PKG_PATH("jni/CommsAddrRec") );
XP_ASSERT( !!clazz );
jmethodID mid = getMethodID( env, clazz, "<init>", "()V" );
XP_ASSERT( !!mid );
jaddr = (*env)->NewObject( env, clazz, mid );
setJAddrRec( env, jaddr, addr ); setJAddrRec( env, jaddr, addr );
deleteLocalRef( env, clazz );
} }
return jaddr; return jaddr;
} }
@ -580,12 +589,8 @@ jobject
addrTypesToJ( JNIEnv* env, const CommsAddrRec* addr ) addrTypesToJ( JNIEnv* env, const CommsAddrRec* addr )
{ {
XP_ASSERT( !!addr ); XP_ASSERT( !!addr );
jclass cls = jobject result = makeObject( env, PKG_PATH("jni/CommsAddrRec$CommsConnTypeSet"),
(*env)->FindClass( env, PKG_PATH("jni/CommsAddrRec$CommsConnTypeSet") ); "()V" );
XP_ASSERT( !!cls );
jmethodID initId = (*env)->GetMethodID( env, cls, "<init>", "()V" );
XP_ASSERT( !!initId );
jobject result = (*env)->NewObject( env, cls, initId );
XP_ASSERT( !!result ); XP_ASSERT( !!result );
jmethodID mid2 = getMethodID( env, result, "add", jmethodID mid2 = getMethodID( env, result, "add",
@ -600,7 +605,6 @@ addrTypesToJ( JNIEnv* env, const CommsAddrRec* addr )
(*env)->CallBooleanMethod( env, result, mid2, jtyp ); (*env)->CallBooleanMethod( env, result, mid2, jtyp );
deleteLocalRef( env, jtyp ); deleteLocalRef( env, jtyp );
} }
deleteLocalRef( env, cls );
return result; return result;
} }
@ -708,14 +712,9 @@ intToJenumField( JNIEnv* env, jobject jobj, int val, const char* field,
jobject jenum = (*env)->GetObjectField( env, jobj, fid ); jobject jenum = (*env)->GetObjectField( env, jobj, fid );
if ( !jenum ) { /* won't exist in new object */ if ( !jenum ) { /* won't exist in new object */
jclass clazz = (*env)->FindClass( env, fieldSig ); jenum = makeObject( env, fieldSig, "()V" );
XP_ASSERT( !!clazz );
jmethodID mid = getMethodID( env, clazz, "<init>", "()V" );
XP_ASSERT( !!mid );
jenum = (*env)->NewObject( env, clazz, mid );
XP_ASSERT( !!jenum ); XP_ASSERT( !!jenum );
(*env)->SetObjectField( env, jobj, fid, jenum ); (*env)->SetObjectField( env, jobj, fid, jenum );
deleteLocalRef( env, clazz );
} }
jobject jval = intToJEnum( env, val, fieldSig ); jobject jval = intToJEnum( env, val, fieldSig );

View file

@ -92,6 +92,8 @@ jbyteArray streamToBArray( JNIEnv *env, XWStreamCtxt* stream );
jmethodID getMethodID( JNIEnv* env, jobject obj, const char* proc, jmethodID getMethodID( JNIEnv* env, jobject obj, const char* proc,
const char* sig ); const char* sig );
jobject makeObject( JNIEnv* env, const char* className, const char* initSig, ... );
jobject makeJAddr( JNIEnv* env, const CommsAddrRec* addr ); jobject makeJAddr( JNIEnv* env, const CommsAddrRec* addr );
jobject setJAddrRec( JNIEnv* env, jobject jaddr, const CommsAddrRec* addr ); jobject setJAddrRec( JNIEnv* env, jobject jaddr, const CommsAddrRec* addr );
void getJAddrRec( JNIEnv* env, CommsAddrRec* addr, jobject jaddr ); void getJAddrRec( JNIEnv* env, CommsAddrRec* addr, jobject jaddr );

View file

@ -60,14 +60,11 @@ makeJRect( AndDraw* draw, JNIEnv* env, int indx, const XP_Rect* rect )
int bottom = rect->top + rect->height; int bottom = rect->top + rect->height;
if ( !robj ) { if ( !robj ) {
jclass rclass = (*env)->FindClass( env, "android/graphics/Rect"); robj = makeObject(env, "android/graphics/Rect", "(IIII)V",
jmethodID initId = (*env)->GetMethodID( env, rclass, "<init>", rect->left, rect->top, right, bottom );
"(IIII)V" );
robj = (*env)->NewObject( env, rclass, initId, rect->left, rect->top,
right, bottom );
draw->jCache[indx] = (*env)->NewGlobalRef( env, robj ); draw->jCache[indx] = (*env)->NewGlobalRef( env, robj );
deleteLocalRefs( env, robj, rclass, DELETE_NO_REF ); deleteLocalRef( env, robj );
robj = draw->jCache[indx]; robj = draw->jCache[indx];
} else { } else {
setInt( env, robj, "left", rect->left ); 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]; jobject dsiobj = draw->jCache[indx];
if ( !dsiobj ) { if ( !dsiobj ) {
jclass rclass = (*env)->FindClass( env, PKG_PATH("jni/DrawScoreInfo") ); dsiobj = makeObject( env, PKG_PATH("jni/DrawScoreInfo"), "()V" );
jmethodID initId = (*env)->GetMethodID( env, rclass, "<init>", "()V" );
dsiobj = (*env)->NewObject( env, rclass, initId );
draw->jCache[indx] = (*env)->NewGlobalRef( env, dsiobj ); draw->jCache[indx] = (*env)->NewGlobalRef( env, dsiobj );
deleteLocalRefs( env, rclass, dsiobj, DELETE_NO_REF ); deleteLocalRef( env, dsiobj );
dsiobj = draw->jCache[indx]; dsiobj = draw->jCache[indx];
} }

View file

@ -856,17 +856,13 @@ and_dutil_onInviteReceived( XW_DUtilCtxt* duc, XWEnv xwe, const NetLaunchInfo* n
DUTIL_CBK_HEADER( "onInviteReceived", "(L" PKG_PATH("NetLaunchInfo") ";)V" ); DUTIL_CBK_HEADER( "onInviteReceived", "(L" PKG_PATH("NetLaunchInfo") ";)V" );
/* Allocate a new NetLaunchInfo */ /* Allocate a new NetLaunchInfo */
jclass cls = (*env)->FindClass( env, PKG_PATH("NetLaunchInfo") ); jobject jnli = makeObject( env, PKG_PATH("NetLaunchInfo"), "()V" );
XP_ASSERT( !!cls );
jmethodID initId = (*env)->GetMethodID( env, cls, "<init>", "()V" );
XP_ASSERT( !!initId );
jobject jnli = (*env)->NewObject( env, cls, initId );
XP_ASSERT( !!jnli ); XP_ASSERT( !!jnli );
setNLI( env, jnli, nli ); setNLI( env, jnli, nli );
(*env)->CallVoidMethod( env, dutil->jdutil, mid, jnli ); (*env)->CallVoidMethod( env, dutil->jdutil, mid, jnli );
deleteLocalRefs( env, jnli, cls, DELETE_NO_REF ); deleteLocalRef( env, jnli );
DUTIL_CBK_TAIL(); DUTIL_CBK_TAIL();
} }