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