3636#include <jni.h>
3737
3838#define JJ (call , ...) (*env)->call(env, __VA_ARGS__)
39+ #define JJ1 (call ) (*env)->call(env)
3940
4041static JavaVM * jvm ;
4142static JNIEnv * env ;
@@ -49,6 +50,7 @@ static jmethodID Method_toString_mid;
4950STATIC const mp_obj_type_t jobject_type ;
5051STATIC const mp_obj_type_t jmethod_type ;
5152
53+ STATIC mp_obj_t new_jobject (jobject jo );
5254STATIC mp_obj_t call_method (jobject obj , const char * name , jarray methods , bool is_constr , mp_uint_t n_args , const mp_obj_t * args );
5355
5456typedef struct _mp_obj_jclass_t {
@@ -66,6 +68,7 @@ typedef struct _mp_obj_jmethod_t {
6668 jobject obj ;
6769 jmethodID meth ;
6870 qstr name ;
71+ bool is_static ;
6972} mp_obj_jmethod_t ;
7073
7174// jclass
@@ -85,12 +88,21 @@ STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
8588
8689 jstring field_name = JJ (NewStringUTF , attr );
8790 jobject field = JJ (CallObjectMethod , self -> cls , Class_getField_mid , field_name );
88- jfieldID field_id = JJ (FromReflectedField , field );
89- jobject obj = JJ (GetStaticObjectField , self -> cls , field_id );
91+ if (!JJ1 (ExceptionCheck )) {
92+ jfieldID field_id = JJ (FromReflectedField , field );
93+ jobject obj = JJ (GetStaticObjectField , self -> cls , field_id );
94+ dest [0 ] = new_jobject (obj );
95+ return ;
96+ }
97+ //JJ1(ExceptionDescribe);
98+ JJ1 (ExceptionClear );
9099
91- mp_obj_jobject_t * o = m_new_obj (mp_obj_jobject_t );
92- o -> base .type = & jobject_type ;
93- o -> obj = obj ;
100+ mp_obj_jmethod_t * o = m_new_obj (mp_obj_jmethod_t );
101+ o -> base .type = & jmethod_type ;
102+ o -> name = attr_in ;
103+ o -> meth = NULL ;
104+ o -> obj = self -> cls ;
105+ o -> is_static = true;
94106 dest [0 ] = o ;
95107 }
96108}
@@ -142,6 +154,7 @@ STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
142154 o -> name = attr_in ;
143155 o -> meth = NULL ;
144156 o -> obj = self -> obj ;
157+ o -> is_static = false;
145158 dest [0 ] = o ;
146159 }
147160}
@@ -310,7 +323,10 @@ STATIC mp_obj_t jmethod_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw,
310323 const char * name = qstr_str (self -> name );
311324// jstring meth_name = JJ(NewStringUTF, name);
312325
313- jclass obj_class = JJ (GetObjectClass , self -> obj );
326+ jclass obj_class = self -> obj ;
327+ if (!self -> is_static ) {
328+ obj_class = JJ (GetObjectClass , self -> obj );
329+ }
314330 jarray methods = JJ (CallObjectMethod , obj_class , Class_getMethods_mid );
315331
316332 return call_method (self -> obj , name , methods , false, n_args , args );
0 commit comments