本文整理汇总了C++中JPType::convertToJavaObject方法的典型用法代码示例。如果您正苦于以下问题:C++ JPType::convertToJavaObject方法的具体用法?C++ JPType::convertToJavaObject怎么用?C++ JPType::convertToJavaObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JPType
的用法示例。
在下文中一共展示了JPType::convertToJavaObject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: convertToJava
jvalue JPClass::convertToJava(HostRef* obj)
{
jvalue res;
JPCleaner cleaner;
res.l = NULL;
// assume it is convertible;
if (JPEnv::getHost()->isNone(obj))
{
res.l = NULL;
}
string simpleName = m_Name.getSimpleName();
if (JPEnv::getHost()->isInt(obj) && (simpleName == "java.lang.Byte" || simpleName == "java.lang.Short" ||
simpleName == "java.lang.Integer"))
{
return buildObjectWrapper(obj);
}
if ((JPEnv::getHost()->isInt(obj) || JPEnv::getHost()->isLong(obj)) && simpleName == "java.lang.Long" && JPEnv::getHost()->isLong(obj))
{
return buildObjectWrapper(obj);
}
if (JPEnv::getHost()->isFloat(obj) && (simpleName == "java.lang.Float" || simpleName == "java.lang.Double"))
{
if (JPEnv::getHost()->isFloat(obj))
{
return buildObjectWrapper(obj);
}
}
if (JPEnv::getHost()->isString(obj))
{
JPTypeName name = JPTypeName::fromSimple("java.lang.String");
JPType* type = JPTypeManager::getType(name);
return type->convertToJava(obj);
}
if (JPEnv::getHost()->isObject(obj))
{
JPObject* ref = JPEnv::getHost()->asObject(obj);
res.l = ref->getObject();
}
if (JPEnv::getHost()->isProxy(obj))
{
JPProxy* proxy = JPEnv::getHost()->asProxy(obj);
res.l = proxy->getProxy();
}
if (JPEnv::getHost()->isWrapper(obj))
{
res = JPEnv::getHost()->getWrapperValue(obj);
}
if (JPEnv::getHost()->isInt(obj))
{
JPTypeName tname = JPTypeName::fromType(JPTypeName::_int);
JPType* t = JPTypeManager::getType(tname);
res.l = t->convertToJavaObject(obj);
}
if (JPEnv::getHost()->isLong(obj))
{
JPTypeName tname = JPTypeName::fromType(JPTypeName::_long);
JPType* t = JPTypeManager::getType(tname);
res.l = t->convertToJavaObject(obj);
}
if (JPEnv::getHost()->isFloat(obj))
{
JPTypeName tname = JPTypeName::fromType(JPTypeName::_double);
JPType* t = JPTypeManager::getType(tname);
res.l = t->convertToJavaObject(obj);
}
if (JPEnv::getHost()->isBoolean(obj))
{
JPTypeName tname = JPTypeName::fromType(JPTypeName::_boolean);
JPType* t = JPTypeManager::getType(tname);
res.l = t->convertToJavaObject(obj);
}
if (JPEnv::getHost()->isArray(obj) && simpleName == "java.lang.Object")
{
JPArray* a = JPEnv::getHost()->asArray(obj);
res = a->getValue();
}
return res;
}
示例2:
JNIEXPORT jobject JNICALL Java_jpype_JPypeInvocationHandler_hostInvoke(
JNIEnv *env, jclass clazz, jstring name,
jlong hostObj, jobjectArray args,
jobjectArray types, jclass returnType)
{
TRACE_IN("Java_jpype_JPypeInvocationHandler_hostInvoke");
void* callbackState = JPEnv::getHost()->prepareCallbackBegin();
JPCleaner cleaner;
try {
string cname = JPJni::asciiFromJava(name);
HostRef* hostObjRef = (HostRef*)hostObj;
HostRef* callable = JPEnv::getHost()->getCallableFrom(hostObjRef, cname);
cleaner.add(callable);
if (callable == NULL || callable->isNull() || JPEnv::getHost()->isNone(callable))
{
JPEnv::getJava()->ThrowNew(JPJni::s_NoSuchMethodErrorClass, cname.c_str());
JPEnv::getHost()->prepareCallbackFinish(callbackState);
return NULL;
}
// convert the arguments into a python list
jsize argLen = JPEnv::getJava()->GetArrayLength(types);
vector<HostRef*> hostArgs;
std::vector<JPTypeName> argTypes;
for (jsize j = 0; j < argLen; j++)
{
jclass c = (jclass)JPEnv::getJava()->GetObjectArrayElement(types, j);
cleaner.addLocal(c);
JPTypeName tn = JPJni::getName(c);
argTypes.push_back(tn);
}
for (int i = 0; i < argLen; i++)
{
jobject obj = JPEnv::getJava()->GetObjectArrayElement(args, i);
cleaner.addLocal(obj);
JPTypeName t = argTypes[i];
jvalue v;
v.l = obj;
HostRef* o = JPTypeManager::getType(t)->asHostObjectFromObject(v);
cleaner.add(o);
hostArgs.push_back(o);
}
HostRef* returnValue = JPEnv::getHost()->callObject(callable, hostArgs);
cleaner.add(returnValue);
JPTypeName returnT = JPJni::getName(returnType);
if (returnValue == NULL || returnValue->isNull() || JPEnv::getHost()->isNone(returnValue))
{
if (returnT.getType() != JPTypeName::_void && returnT.getType() < JPTypeName::_object)
{
JPEnv::getJava()->ThrowNew(JPJni::s_RuntimeExceptionClass, "Return value is None when it cannot be");
JPEnv::getHost()->prepareCallbackFinish(callbackState);
return NULL;
}
}
if (returnT.getType() == JPTypeName::_void)
{
JPEnv::getHost()->prepareCallbackFinish(callbackState);
return NULL;
}
JPType* rt = JPTypeManager::getType(returnT);
if (rt->canConvertToJava(returnValue) == _none)
{
JPEnv::getJava()->ThrowNew(JPJni::s_RuntimeExceptionClass, "Return value is not compatible with required type.");
JPEnv::getHost()->prepareCallbackFinish(callbackState);
return NULL;
}
jobject returnObj = rt->convertToJavaObject(returnValue);
JPEnv::getHost()->prepareCallbackFinish(callbackState);
return returnObj;
}
catch(HostException* ex)
{
JPEnv::getHost()->clearError();
if (JPEnv::getHost()->isJavaException(ex))
{
JPCleaner cleaner;
HostRef* javaExcRef = JPEnv::getHost()->getJavaException(ex);
JPObject* javaExc = JPEnv::getHost()->asObject(javaExcRef);
cleaner.add(javaExcRef);
jobject obj = javaExc->getObject();
//.........这里部分代码省略.........