unix/modjni: jobject.__str__/__repr__: Return Java .toString() value.

This commit is contained in:
Paul Sokolovsky 2015-10-03 08:43:41 -07:00
parent 34f26ea862
commit f22be4ebd9

View File

@ -48,7 +48,7 @@ static jmethodID Class_getField_mid;
static jmethodID Class_getMethods_mid; static jmethodID Class_getMethods_mid;
static jmethodID Class_getConstructors_mid; static jmethodID Class_getConstructors_mid;
static jmethodID Method_getName_mid; static jmethodID Method_getName_mid;
static jmethodID Method_toString_mid; static jmethodID Object_toString_mid;
static jclass List_class; static jclass List_class;
static jmethodID List_get_mid; static jmethodID List_get_mid;
@ -180,10 +180,17 @@ STATIC mp_obj_t new_jclass(jclass jc) {
// jobject // jobject
STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_jobject_t *self = self_in; mp_obj_jobject_t *self = self_in;
// Variable value printed as cast to int if (kind == PRINT_REPR) {
mp_printf(print, "<jobject @%p>", self->obj); mp_printf(print, "<jobject @%p \"", self->obj);
}
jobject str_o = JJ(CallObjectMethod, self->obj, Object_toString_mid);
const char *str = JJ(GetStringUTFChars, str_o, NULL);
mp_printf(print, str);
JJ(ReleaseStringUTFChars, str_o, str);
if (kind == PRINT_REPR) {
mp_printf(print, "\">");
}
} }
STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
@ -358,7 +365,7 @@ STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool
jsize num_methods = JJ(GetArrayLength, methods); jsize num_methods = JJ(GetArrayLength, methods);
for (int i = 0; i < num_methods; i++) { for (int i = 0; i < num_methods; i++) {
jobject meth = JJ(GetObjectArrayElement, methods, i); jobject meth = JJ(GetObjectArrayElement, methods, i);
jobject name_o = JJ(CallObjectMethod, meth, Method_toString_mid); jobject name_o = JJ(CallObjectMethod, meth, Object_toString_mid);
const char *decl = JJ(GetStringUTFChars, name_o, NULL); const char *decl = JJ(GetStringUTFChars, name_o, NULL);
const char *arg_types = strchr(decl, '(') + 1; const char *arg_types = strchr(decl, '(') + 1;
//const char *arg_types_end = strchr(arg_types, ')'); //const char *arg_types_end = strchr(arg_types, ')');
@ -500,6 +507,10 @@ STATIC void create_jvm() {
jclass method_class = JJ(FindClass, "java/lang/reflect/Method"); jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
String_class = JJ(FindClass, "java/lang/String"); String_class = JJ(FindClass, "java/lang/String");
jclass Object_class = JJ(FindClass, "java/lang/Object");
Object_toString_mid = JJ(GetMethodID, Object_class, "toString",
"()Ljava/lang/String;");
Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField", Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
"(Ljava/lang/String;)Ljava/lang/reflect/Field;"); "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods", Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
@ -508,8 +519,6 @@ STATIC void create_jvm() {
"()[Ljava/lang/reflect/Constructor;"); "()[Ljava/lang/reflect/Constructor;");
Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName", Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
"()Ljava/lang/String;"); "()Ljava/lang/String;");
Method_toString_mid = (*env)->GetMethodID(env, method_class, "toString",
"()Ljava/lang/String;");
List_class = JJ(FindClass, "java/util/List"); List_class = JJ(FindClass, "java/util/List");
List_get_mid = JJ(GetMethodID, List_class, "get", List_get_mid = JJ(GetMethodID, List_class, "get",