Skip to content

Commit 1cb5de2

Browse files
committed
unix/modjni: jvalue2py: Handle class-containing jvalues.
1 parent 861fad5 commit 1cb5de2

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

unix/modjni.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
static JavaVM *jvm;
4242
static JNIEnv *env;
43+
static jclass Class_class;
4344
static jclass String_class;
4445
static jmethodID Class_getField_mid;
4546
static jmethodID Class_getMethods_mid;
@@ -51,6 +52,7 @@ STATIC const mp_obj_type_t jobject_type;
5152
STATIC const mp_obj_type_t jmethod_type;
5253

5354
STATIC mp_obj_t new_jobject(jobject jo);
55+
STATIC mp_obj_t new_jclass(jclass jc);
5456
STATIC 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);
5557

5658
typedef struct _mp_obj_jclass_t {
@@ -134,6 +136,12 @@ STATIC const mp_obj_type_t jclass_type = {
134136
.locals_dict = (mp_obj_t)&jclass_locals_dict,
135137
};
136138

139+
STATIC mp_obj_t new_jclass(jclass jc) {
140+
mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
141+
o->base.type = &jclass_type;
142+
o->cls = jc;
143+
return o;
144+
}
137145

138146
// jobject
139147

@@ -243,6 +251,8 @@ ret_string:;
243251
// Non-primitive, object type
244252
if (JJ(IsInstanceOf, arg, String_class)) {
245253
goto ret_string;
254+
} else if (JJ(IsInstanceOf, arg, Class_class)) {
255+
return new_jclass(arg);
246256
} else {
247257
return new_jobject(arg);
248258
}
@@ -379,15 +389,15 @@ STATIC void create_jvm() {
379389
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to create JVM"));
380390
}
381391

382-
jclass class_class = JJ(FindClass, "java/lang/Class");
392+
Class_class = JJ(FindClass, "java/lang/Class");
383393
jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
384394
String_class = JJ(FindClass, "java/lang/String");
385395

386-
Class_getField_mid = (*env)->GetMethodID(env, class_class, "getField",
396+
Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
387397
"(Ljava/lang/String;)Ljava/lang/reflect/Field;");
388-
Class_getMethods_mid = (*env)->GetMethodID(env, class_class, "getMethods",
398+
Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
389399
"()[Ljava/lang/reflect/Method;");
390-
Class_getConstructors_mid = (*env)->GetMethodID(env, class_class, "getConstructors",
400+
Class_getConstructors_mid = (*env)->GetMethodID(env, Class_class, "getConstructors",
391401
"()[Ljava/lang/reflect/Constructor;");
392402
Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
393403
"()Ljava/lang/String;");

0 commit comments

Comments
 (0)