diff --git a/src/main/c/jpy_jtype.c b/src/main/c/jpy_jtype.c index 9484e20b..b7895a7d 100644 --- a/src/main/c/jpy_jtype.c +++ b/src/main/c/jpy_jtype.c @@ -401,53 +401,63 @@ int JType_CreateJavaObject_2(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jcl return 0; } +#define JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(TARGET_TYPE, STATIC_METHOD_ID, ARG, JOBJECT_PTR) \ + Py_BEGIN_ALLOW_THREADS; \ + *JOBJECT_PTR = (*jenv)->CallStaticObjectMethod(jenv, TARGET_TYPE, STATIC_METHOD_ID, ARG); \ + Py_END_ALLOW_THREADS; \ + if (*objectRef == NULL) { \ + PyErr_NoMemory(); \ + return -1; \ + } \ + JPy_ON_JAVA_EXCEPTION_RETURN(-1); \ + return 0; + int JType_CreateJavaBooleanObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jboolean value; if (PyBool_Check(pyArg) || JPy_IS_CLONG(pyArg)) { - value.z = JPy_AS_JBOOLEAN(pyArg); + value = JPy_AS_JBOOLEAN(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Boolean_JClass, JPy_Boolean_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Boolean_JClass, JPy_Boolean_ValueOf_SMID, value, objectRef); } int JType_CreateJavaCharacterObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jchar value; if (JPy_IS_CLONG(pyArg)) { - value.c = JPy_AS_JCHAR(pyArg); + value = JPy_AS_JCHAR(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Character_JClass, JPy_Character_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Character_JClass, JPy_Character_ValueOf_SMID, value, objectRef); } int JType_CreateJavaByteObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jbyte value; if (JPy_IS_CLONG(pyArg)) { - value.b = JPy_AS_JBYTE(pyArg); + value = JPy_AS_JBYTE(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Byte_JClass, JPy_Byte_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Byte_JClass, JPy_Byte_ValueOf_SMID, value, objectRef); } int JType_CreateJavaShortObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jshort value; if (JPy_IS_CLONG(pyArg)) { - value.s = JPy_AS_JSHORT(pyArg); + value = JPy_AS_JSHORT(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Short_JClass, JPy_Short_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Short_JClass, JPy_Short_ValueOf_SMID, value, objectRef); } int JType_CreateJavaNumberFromPythonInt(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; signed char b; short s; long i; @@ -458,69 +468,63 @@ int JType_CreateJavaNumberFromPythonInt(JNIEnv* jenv, JPy_JType* type, PyObject* b = (signed char) j; if (i != j) { - value.j = j; - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Long_JClass, JPy_Long_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Long_JClass, JPy_Long_ValueOf_SMID, j, objectRef); } if (s != i) { - value.i = i; - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Integer_JClass, JPy_Integer_Init_MID, value, - objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Integer_JClass, JPy_Integer_ValueOf_SMID, i, objectRef); } if (b != s) { - value.s = s; - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Short_JClass, JPy_Short_Init_MID, value, - objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Short_JClass, JPy_Short_ValueOf_SMID, s, objectRef); } - value.b = b; - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Byte_JClass, JPy_Byte_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Byte_JClass, JPy_Byte_ValueOf_SMID, b, objectRef); } int JType_CreateJavaIntegerObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jint value; if (JPy_IS_CLONG(pyArg)) { - value.i = JPy_AS_JINT(pyArg); + value = JPy_AS_JINT(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Integer_JClass, JPy_Integer_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Integer_JClass, JPy_Integer_ValueOf_SMID, value, objectRef); } int JType_CreateJavaLongObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jlong value; if (JPy_IS_CLONG(pyArg)) { - value.j = JPy_AS_JLONG(pyArg); + value = JPy_AS_JLONG(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Long_JClass, JPy_Long_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Long_JClass, JPy_Long_ValueOf_SMID, value, objectRef); } int JType_CreateJavaFloatObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jfloat value; if (JPy_IS_CLONG(pyArg)) { - value.f = (jfloat) JPy_AS_JLONG(pyArg); + value = (jfloat) JPy_AS_JLONG(pyArg); } else if (PyFloat_Check(pyArg)) { - value.f = JPy_AS_JFLOAT(pyArg); + value = JPy_AS_JFLOAT(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Float_JClass, JPy_Float_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Float_JClass, JPy_Float_ValueOf_SMID, value, objectRef); } int JType_CreateJavaDoubleObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) { - jvalue value; + jdouble value; if (JPy_IS_CLONG(pyArg)) { - value.d = (jdouble) JPy_AS_JLONG(pyArg); + value = (jdouble) JPy_AS_JLONG(pyArg); } else if (PyFloat_Check(pyArg)) { - value.d = JPy_AS_JDOUBLE(pyArg); + value = JPy_AS_JDOUBLE(pyArg); } else { return JType_PythonToJavaConversionError(type, pyArg); } - return JType_CreateJavaObject(jenv, type, pyArg, JPy_Double_JClass, JPy_Double_Init_MID, value, objectRef); + JType_CALL_STATIC_OBJECT_METHOD_1_AND_RETURN(JPy_Double_JClass, JPy_Double_ValueOf_SMID, value, objectRef); } int JType_CreateJavaPyObject(JNIEnv* jenv, JPy_JType* type, PyObject* pyArg, jobject* objectRef) diff --git a/src/main/c/jpy_module.c b/src/main/c/jpy_module.c index e41df14e..e8716fca 100644 --- a/src/main/c/jpy_module.c +++ b/src/main/c/jpy_module.c @@ -193,30 +193,30 @@ jclass JPy_StopIteration_JClass = NULL; // java.lang.Boolean jclass JPy_Boolean_JClass = NULL; -jmethodID JPy_Boolean_Init_MID = NULL; +jmethodID JPy_Boolean_ValueOf_SMID = NULL; jmethodID JPy_Boolean_BooleanValue_MID = NULL; jclass JPy_Character_JClass = NULL; -jmethodID JPy_Character_Init_MID; +jmethodID JPy_Character_ValueOf_SMID; jmethodID JPy_Character_CharValue_MID = NULL; jclass JPy_Byte_JClass = NULL; -jmethodID JPy_Byte_Init_MID = NULL; +jmethodID JPy_Byte_ValueOf_SMID = NULL; jclass JPy_Short_JClass = NULL; -jmethodID JPy_Short_Init_MID = NULL; +jmethodID JPy_Short_ValueOf_SMID = NULL; jclass JPy_Integer_JClass = NULL; -jmethodID JPy_Integer_Init_MID = NULL; +jmethodID JPy_Integer_ValueOf_SMID = NULL; jclass JPy_Long_JClass = NULL; -jmethodID JPy_Long_Init_MID = NULL; +jmethodID JPy_Long_ValueOf_SMID = NULL; jclass JPy_Float_JClass = NULL; -jmethodID JPy_Float_Init_MID = NULL; +jmethodID JPy_Float_ValueOf_SMID = NULL; jclass JPy_Double_JClass = NULL; -jmethodID JPy_Double_Init_MID = NULL; +jmethodID JPy_Double_ValueOf_SMID = NULL; // java.lang.Number jclass JPy_Number_JClass = NULL; @@ -890,32 +890,32 @@ int JPy_InitGlobalVars(JNIEnv* jenv) DEFINE_CLASS(JPy_UnsupportedOperationException_JClass, "java/lang/UnsupportedOperationException"); DEFINE_CLASS(JPy_Boolean_JClass, "java/lang/Boolean"); - DEFINE_METHOD(JPy_Boolean_Init_MID, JPy_Boolean_JClass, "", "(Z)V"); + DEFINE_STATIC_METHOD(JPy_Boolean_ValueOf_SMID, JPy_Boolean_JClass, "valueOf", "(Z)Ljava/lang/Boolean;"); DEFINE_METHOD(JPy_Boolean_BooleanValue_MID, JPy_Boolean_JClass, "booleanValue", "()Z"); DEFINE_CLASS(JPy_Character_JClass, "java/lang/Character"); - DEFINE_METHOD(JPy_Character_Init_MID, JPy_Character_JClass, "", "(C)V"); + DEFINE_STATIC_METHOD(JPy_Character_ValueOf_SMID, JPy_Character_JClass, "valueOf", "(C)Ljava/lang/Character;"); DEFINE_METHOD(JPy_Character_CharValue_MID, JPy_Character_JClass, "charValue", "()C"); DEFINE_CLASS(JPy_Number_JClass, "java/lang/Number"); DEFINE_CLASS(JPy_Byte_JClass, "java/lang/Byte"); - DEFINE_METHOD(JPy_Byte_Init_MID, JPy_Byte_JClass, "", "(B)V"); + DEFINE_STATIC_METHOD(JPy_Byte_ValueOf_SMID, JPy_Byte_JClass, "valueOf", "(B)Ljava/lang/Byte;"); DEFINE_CLASS(JPy_Short_JClass, "java/lang/Short"); - DEFINE_METHOD(JPy_Short_Init_MID, JPy_Short_JClass, "", "(S)V"); + DEFINE_STATIC_METHOD(JPy_Short_ValueOf_SMID, JPy_Short_JClass, "valueOf", "(S)Ljava/lang/Short;"); DEFINE_CLASS(JPy_Integer_JClass, "java/lang/Integer"); - DEFINE_METHOD(JPy_Integer_Init_MID, JPy_Integer_JClass, "", "(I)V"); + DEFINE_STATIC_METHOD(JPy_Integer_ValueOf_SMID, JPy_Integer_JClass, "valueOf", "(I)Ljava/lang/Integer;"); DEFINE_CLASS(JPy_Long_JClass, "java/lang/Long"); - DEFINE_METHOD(JPy_Long_Init_MID, JPy_Long_JClass, "", "(J)V"); + DEFINE_STATIC_METHOD(JPy_Long_ValueOf_SMID, JPy_Long_JClass, "valueOf", "(J)Ljava/lang/Long;"); DEFINE_CLASS(JPy_Float_JClass, "java/lang/Float"); - DEFINE_METHOD(JPy_Float_Init_MID, JPy_Float_JClass, "", "(F)V"); + DEFINE_STATIC_METHOD(JPy_Float_ValueOf_SMID, JPy_Float_JClass, "valueOf", "(F)Ljava/lang/Float;"); DEFINE_CLASS(JPy_Double_JClass, "java/lang/Double"); - DEFINE_METHOD(JPy_Double_Init_MID, JPy_Double_JClass, "", "(D)V"); + DEFINE_STATIC_METHOD(JPy_Double_ValueOf_SMID, JPy_Double_JClass, "valueOf", "(D)Ljava/lang/Double;"); DEFINE_CLASS(JPy_Number_JClass, "java/lang/Number"); DEFINE_METHOD(JPy_Number_IntValue_MID, JPy_Number_JClass, "intValue", "()I"); @@ -1036,16 +1036,16 @@ void JPy_ClearGlobalVars(JNIEnv* jenv) JPy_Field_GetName_MID = NULL; JPy_Field_GetModifiers_MID = NULL; JPy_Field_GetType_MID = NULL; - JPy_Boolean_Init_MID = NULL; + JPy_Boolean_ValueOf_SMID = NULL; JPy_Boolean_BooleanValue_MID = NULL; - JPy_Character_Init_MID = NULL; + JPy_Character_ValueOf_SMID = NULL; JPy_Character_CharValue_MID = NULL; - JPy_Byte_Init_MID = NULL; - JPy_Short_Init_MID = NULL; - JPy_Integer_Init_MID = NULL; - JPy_Long_Init_MID = NULL; - JPy_Float_Init_MID = NULL; - JPy_Double_Init_MID = NULL; + JPy_Byte_ValueOf_SMID = NULL; + JPy_Short_ValueOf_SMID = NULL; + JPy_Integer_ValueOf_SMID = NULL; + JPy_Long_ValueOf_SMID = NULL; + JPy_Float_ValueOf_SMID = NULL; + JPy_Double_ValueOf_SMID = NULL; JPy_Number_IntValue_MID = NULL; JPy_Number_LongValue_MID = NULL; JPy_Number_DoubleValue_MID = NULL; diff --git a/src/main/c/jpy_module.h b/src/main/c/jpy_module.h index 3e260d1f..36b48f31 100644 --- a/src/main/c/jpy_module.h +++ b/src/main/c/jpy_module.h @@ -215,32 +215,32 @@ extern jclass JPy_KeyError_JClass; extern jclass JPy_StopIteration_JClass; extern jclass JPy_Boolean_JClass; -extern jmethodID JPy_Boolean_Init_MID; +extern jmethodID JPy_Boolean_ValueOf_SMID; extern jmethodID JPy_Boolean_BooleanValue_MID; extern jclass JPy_Character_JClass; -extern jmethodID JPy_Character_Init_MID; +extern jmethodID JPy_Character_ValueOf_SMID; extern jmethodID JPy_Character_CharValue_MID; extern jclass JPy_Number_JClass; extern jclass JPy_Byte_JClass; -extern jmethodID JPy_Byte_Init_MID; +extern jmethodID JPy_Byte_ValueOf_SMID; extern jclass JPy_Short_JClass; -extern jmethodID JPy_Short_Init_MID; +extern jmethodID JPy_Short_ValueOf_SMID; extern jclass JPy_Integer_JClass; -extern jmethodID JPy_Integer_Init_MID; +extern jmethodID JPy_Integer_ValueOf_SMID; extern jclass JPy_Long_JClass; -extern jmethodID JPy_Long_Init_MID; +extern jmethodID JPy_Long_ValueOf_SMID; extern jclass JPy_Float_JClass; -extern jmethodID JPy_Float_Init_MID; +extern jmethodID JPy_Float_ValueOf_SMID; extern jclass JPy_Double_JClass; -extern jmethodID JPy_Double_Init_MID; +extern jmethodID JPy_Double_ValueOf_SMID; extern jclass JPy_Number_JClass; extern jmethodID JPy_Number_IntValue_MID;