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 );
}
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
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, "<init>", "()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, "<init>", "()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, "<init>", "()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 );

View file

@ -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 );

View file

@ -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, "<init>",
"(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, "<init>", "()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];
}

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" );
/* Allocate a new NetLaunchInfo */
jclass cls = (*env)->FindClass( env, PKG_PATH("NetLaunchInfo") );
XP_ASSERT( !!cls );
jmethodID initId = (*env)->GetMethodID( env, cls, "<init>", "()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();
}