reflection - Android JNI: Getting the parent class -
reflection - Android JNI: Getting the parent class -
i trying pass whichever activity active c++ code written ndk.
if illustration pass reference mainactivity object, (or perhaps settingsactivity), how can phone call method such getname() in object class?
if declare own java method "givemename()" homecoming getclass.getname(), givemename() works fine of course of study limited class in declared it. how can phone call object::getname() within c++ code - without modifying java code pass object reference.
edit:
i thought had working - saw class name beingness returned @ point not. still null returned when trying getname():
// pds: class - should sort of activity subclass.. jclass clsactivity = penv->getobjectclass( pobj2 ); if( jniexception( penv ) ) return; logdebugf( "pds> nanewactivity::clsactivity: %08lx\n", (long) clsactivity ); if( ! clsactivity ) return; jmethodid mid_getclass = penv->getmethodid( clsactivity, "getclass", "()ljava/lang/class;"); logdebugf( "pds> nanewactivity::getclass methodid: %08lx\n", (long) mid_getclass ); if( jniexception( penv ) ) return; // pds: phone call getclass() method on activity object ( object::getclass() ) jclass c = (jclass) penv->callobjectmethod( pobj2, mid_getclass ); logdebugf( "pds> nanewactivity::getclass() returns: %08lx\n", (long) c ); if( jniexception( penv ) ) return; // pds: if class object, phone call class::getname() jmethodid mid_getname = penv->getmethodid( c, "getname", "()ljava/lang/string;"); logdebugf( "pds> nanewactivity::getname methodid: %08lx\n", (long) mid_getname ); *** getting null beingness returned mid_getname if( jniexception( penv ) ) return; // pds: phone call method on activity object ( object::getname() ) jobject n = penv->callobjectmethod( pobj2, mid_getname ); if( jniexception( penv ) ) return; jstring sname = (jstring) n; char *pszdata = (char*) penv->getstringutfchars( sname, jni_false ); logdebugf( "pds> nanewactivity, class name[%s]\n", pszdata ); penv->releasestringutfchars( sname, pszdata );
fixed code: figured out seva's help below:
// pds: class - should sort of activity subclass.. jclass clsactivity = penv->getobjectclass( pobj2 ); if( jniexception( penv ) ) return; logdebugf( "pds> nanewactivity::clsactivity: %08lx\n", (long) clsactivity ); if( ! clsactivity ) return; jmethodid mid_getclass = penv->getmethodid( clsactivity, "getclass", "()ljava/lang/class;"); logdebugf( "pds> nanewactivity::getclass() methodid: %08lx\n", (long) mid_getclass ); if( jniexception( penv ) ) return; // pds: phone call getclass() method on activity object.. ( object::getclass() ) jclass c = (jclass) penv->callobjectmethod( pobj2, mid_getclass ); logdebugf( "pds> nanewactivity::getclass() returns: %08lx\n", (long) c ); if( jniexception( penv ) ) return; jclass clsclass = penv->getobjectclass( c ); logdebugf( "pds> nanewactivity::clsclass: %08lx\n", (long) clsclass ); if( jniexception( penv ) ) return; // pds: if class object, phone call class::getname() jmethodid mid_getname = penv->getmethodid( clsclass, "getname", "()ljava/lang/string;"); logdebugf( "pds> nanewactivity::getname() methodid: %08lx\n", (long) mid_getname ); if( jniexception( penv ) ) return; // pds: can't phone call method on activity object - need phone call on class (object) ( class::getname() ) jobject n = penv->callobjectmethod( c, mid_getname ); if( jniexception( penv ) ) return; jstring sname = (jstring) n; char *pszdata = (char*) penv->getstringutfchars( sname, jni_false ); logdebugf( "pds> nanewactivity, class name[%s]\n", pszdata ); penv->releasestringutfchars( sname, pszdata );
you utilize java.lang.object
class method id, utilize object pointer of derived class instance phone call method. jni permits that. first, you'd have find class id of java.lang.object
name.
android reflection android-ndk jni native-code
Comments
Post a Comment