| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include "jpype.h" |
| 17 | #include "pyjp.h" |
| 18 | #include "jp_boxedtype.h" |
| 19 | |
| 20 | JPBoxedType::JPBoxedType(JPJavaFrame& frame, jclass clss, |
| 21 | const string& name, |
| 22 | JPClass* super, |
| 23 | JPClassList& interfaces, |
| 24 | jint modifiers, |
| 25 | JPPrimitiveType* primitiveType) |
| 26 | : JPClass(frame, clss, name, super, interfaces, modifiers), |
| 27 | m_PrimitiveType(primitiveType) |
| 28 | { |
| 29 | if (name != "java.lang.Void") |
| 30 | { |
| 31 | string s = string("(") + primitiveType->getTypeCode() + ")V"; |
| 32 | m_CtorID = frame.GetMethodID(clss, "<init>", s.c_str()); |
| 33 | } |
| 34 | |
| 35 | m_DoubleValueID = nullptr; |
| 36 | m_FloatValueID = nullptr; |
| 37 | m_LongValueID = nullptr; |
| 38 | m_IntValueID = nullptr; |
| 39 | m_BooleanValueID = nullptr; |
| 40 | m_CharValueID = nullptr; |
| 41 | |
| 42 | if (name != "java.lang.Void" && name != "java.lang.Boolean" && name != "java.lang.Character" ) |
| 43 | { |
| 44 | m_DoubleValueID = frame.GetMethodID(clss, "doubleValue", "()D"); |
| 45 | m_FloatValueID = frame.GetMethodID(clss, "floatValue", "()F"); |
| 46 | m_IntValueID = frame.GetMethodID(clss, "intValue", "()I"); |
| 47 | m_LongValueID = frame.GetMethodID(clss, "longValue", "()J"); |
| 48 | } |
| 49 | |
| 50 | if (name == "java.lang.Boolean") |
| 51 | { |
| 52 | m_BooleanValueID = frame.GetMethodID(clss, "booleanValue", "()Z"); |
| 53 | } |
| 54 | |
| 55 | if (name == "java.lang.Character") |
| 56 | { |
| 57 | m_CharValueID = frame.GetMethodID(clss, "charValue", "()C"); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | JPBoxedType::~JPBoxedType() |
| 63 | = default; |
| 64 | |
| 65 | JPMatch::Type JPBoxedType::findJavaConversion(JPMatch &match) |
| 66 | { |
| 67 | JP_TRACE_IN("JPBoxedType::findJavaConversion"); |
| 68 | JPClass::findJavaConversion(match); |
| 69 | if (match.type != JPMatch::_none) |
| 70 | return match.type; |
| 71 | if (m_PrimitiveType->findJavaConversion(match) != JPMatch::_none) |
| 72 | { |
| 73 | JP_TRACE("Primitive", match.type); |
| 74 | match.conversion = boxBooleanConversion; |
| 75 | match.closure = this; |
| 76 | if (match.type == JPMatch::_exact) |
| 77 | return match.type = JPMatch::_implicit; |
| 78 | return match.type = JPMatch::_explicit; |
| 79 | } |
| 80 | return match.type = JPMatch::_none; |
| 81 | JP_TRACE_OUT; |
| 82 | } |
| 83 | |
| 84 | void JPBoxedType::getConversionInfo(JPConversionInfo &info) |
| 85 | { |
| 86 | JP_TRACE_IN("JPBoxedType::getConversionInfo"); |
| 87 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 88 | m_PrimitiveType->getConversionInfo(info); |
| 89 | JPPyObject::call(PyObject_CallMethod(info.expl, "extend", "O", info.implicit)); |
| 90 | JPPyObject::call(PyObject_CallMethod(info.implicit, "clear", "")); |
| 91 | JPPyObject::call(PyObject_CallMethod(info.implicit, "extend", "O", info.exact)); |
| 92 | JPPyObject::call(PyObject_CallMethod(info.exact, "clear", "")); |
| 93 | JPClass::getConversionInfo(info); |
| 94 | JP_TRACE_OUT; |
| 95 | } |
| 96 | |
| 97 | jobject JPBoxedType::box(JPJavaFrame &frame, jvalue v) |
| 98 | { |
| 99 | return frame.NewObjectA(m_Class.get(), m_CtorID, &v); |
| 100 | } |
| 101 | |
| 102 | JPPyObject JPBoxedType::convertToPythonObject(JPJavaFrame& frame, jvalue value, bool cast) |
| 103 | { |
| 104 | JPClass *cls = this; |
| 105 | if (!cast) |
| 106 | { |
| 107 | // This loses type |
| 108 | if (value.l == nullptr) |
| 109 | { |
| 110 | return JPPyObject::getNone(); |
| 111 | } |
| 112 | |
| 113 | cls = frame.findClassForObject(value.l); |
| 114 | if (cls != this) |
| 115 | return cls->convertToPythonObject(frame, value, true); |
| 116 | } |
| 117 | |
| 118 | JPPyObject wrapper = PyJPClass_create(frame, cls); |
| 119 | JPPyObject obj; |
| 120 | JPContext *context = JPContext_global; |
| 121 | if (this->getPrimitive() == context->_char) |
| 122 | { |
| 123 | jchar value2 = 0; |
| 124 | // Not null get the char value |
| 125 | if (value.l != nullptr) |
| 126 | value2 = context->_char->getValueFromObject(frame, JPValue(this, value)).getValue().c; |
| 127 | // Create a char string object |
| 128 | obj = JPPyObject::call(PyJPChar_Create((PyTypeObject*) wrapper.get(), value2)); |
| 129 | } else |
| 130 | obj = PyJPNumber_create(frame, wrapper, JPValue(cls, value)); |
| 131 | PyJPValue_assignJavaSlot(frame, obj.get(), JPValue(cls, value)); |
| 132 | return obj; |
| 133 | } |