Exemplo n.º 1
0
EMatchType JPClassType::canConvertToJava(HostRef* obj)
{
	JPCleaner cleaner;

	if (JPEnv::getHost()->isNone(obj))
	{
		return _implicit;
	}

	if (JPEnv::getHost()->isClass(obj))
	{
		return _exact;
	}

	if (JPEnv::getHost()->isWrapper(obj))
	{
		JPTypeName name = JPEnv::getHost()->getWrapperTypeName(obj);

		if (name.getType() == JPTypeName::_class)
		{
			return _exact;
		}
	}

	return _none;
}
Exemplo n.º 2
0
EMatchType JPDoubleType::canConvertToJava(HostRef* obj)
{
	if (JPEnv::getHost()->isNone(obj))
	{
		return _none;
	}

	if (JPEnv::getHost()->isFloat(obj))
	{
		if (JPEnv::getHost()->isObject(obj))
		{
			return _implicit;
		}
		return _exact;
	}

	if (JPEnv::getHost()->isWrapper(obj))
	{
		JPTypeName name = JPEnv::getHost()->getWrapperTypeName(obj);
		if (name.getType() == JPTypeName::_double)
		{
			return _exact;
		}
	}

	// Java allows conversion to any type with a longer range even if lossy
	if (JPEnv::getHost()->isInt(obj) || JPEnv::getHost()->isLong(obj))
	{
		return _implicit;
	}

	return _none;
}
Exemplo n.º 3
0
jvalue PythonHostEnvironment::getWrapperValue(PyObject* obj)
{
	JPTypeName name = getWrapperTypeName(obj);
	PyObject* value = JPyObject::getAttrString(obj, "_value");
	jvalue* v = (jvalue*)JPyCObject::asVoidPtr(value);
	Py_DECREF(value);

	if (name.isObjectType())
	{
		jvalue res;
		res.l = JPEnv::getJava()->NewGlobalRef(v->l);
		return res;
	}
	return *v;
}
Exemplo n.º 4
0
HostRef* JPClass::asHostObject(jvalue obj) 
{
	TRACE_IN("JPClass::asPyObject");
	if (obj.l == NULL)
	{
		return JPEnv::getHost()->getNone();
	}

	JPTypeName name = JPJni::getClassName(obj.l);
	if (name.getType() ==JPTypeName::_array)
	{
		JPType* arrayType = JPTypeManager::getType(name);
		return arrayType->asHostObject(obj);
	}
	
	return JPEnv::getHost()->newObject(new JPObject(name, obj.l));
	TRACE_OUT;
}
Exemplo n.º 5
0
EMatchType JPStringType::canConvertToJava(HostRef* obj)
{
	TRACE_IN("JPStringType::canConvertToJava");
	JPCleaner cleaner;

	if (obj == NULL || JPEnv::getHost()->isNone(obj))
	{
		return _implicit;
	}

	if (JPEnv::getHost()->isString(obj))
	{
		return _exact;
	}
	
	if (JPEnv::getHost()->isWrapper(obj))
	{
		JPTypeName name = JPEnv::getHost()->getWrapperTypeName(obj);

		if (name.getType() == JPTypeName::_string)
		{
			return _exact;
		}
	}

	if (JPEnv::getHost()->isObject(obj))
	{
		JPObject* o = JPEnv::getHost()->asObject(obj);

		JPClass* oc = o->getClass();
		if (oc->getName().getSimpleName() == "java.lang.String")
		{
			return _exact;
		}
	}
	return _none;
	TRACE_OUT;
}
Exemplo n.º 6
0
HostRef* PythonHostEnvironment::newArray(JPArray* m)
{
	JPArrayClass* jc = m->getClass();
	JPTypeName name = jc->getName();

	PyObject* args = JPySequence::newTuple(1);
	PyObject* cname = JPyString::fromString(name.getSimpleName().c_str());
	JPySequence::setItem(args, 0, cname);
	Py_DECREF(cname);

	PyObject* pyClass = JPyObject::call(m_GetArrayClassMethod, args, NULL);
	Py_DECREF(args);
	
	PyObject* joHolder = JPyCObject::fromVoidAndDesc((void*)m, "JPArray", &deleteJPArrayDestructor);
	args = JPySequence::newTuple(2);
	JPySequence::setItem(args, 0, m_SpecialConstructorKey);
	JPySequence::setItem(args, 1, joHolder);
	Py_DECREF(joHolder);

	PyObject* res = JPyObject::call(pyClass, args, NULL);
	Py_DECREF(args);

	return new HostRef(res, false);
}
Exemplo n.º 7
0
EMatchType JPClass::canConvertToJava(HostRef* obj)
{
	if (JPEnv::getHost()->isNone(obj))
	{
		return _implicit;
	}

	JPCleaner cleaner;

	string simpleName = m_Name.getSimpleName();	
	
	if (simpleName == "java.lang.Byte" || simpleName == "java.lang.Short" ||
	    simpleName == "java.lang.Integer")
	{
		if (JPEnv::getHost()->isInt(obj))
		{
			return _explicit;
		}
	}    	
	
	if (simpleName == "java.lang.Long" && JPEnv::getHost()->isLong(obj))
	{
		return _explicit;
	}
	
	if (simpleName == "java.lang.Float" || simpleName == "java.lang.Double")
	{
		if (JPEnv::getHost()->isFloat(obj))
		{
			return _explicit;
		}
	}    	
		
	if (JPEnv::getHost()->isObject(obj))
	{
		JPObject* o = JPEnv::getHost()->asObject(obj);

		JPClass* oc = o->getClass();

		if (oc == this)
		{
			// hey, this is me! :)
			return _exact;
		}

		if (JPEnv::getJava()->IsAssignableFrom(oc->m_Class, m_Class))
		{
			return _implicit;
		}
	}

	if (JPEnv::getHost()->isProxy(obj))
	{
		JPProxy* proxy = JPEnv::getHost()->asProxy(obj);
		// Check if any of the interfaces matches ...
		vector<jclass> itf = proxy->getInterfaces();
		for (unsigned int i = 0; i < itf.size(); i++)
		{
			if (JPEnv::getJava()->IsAssignableFrom(itf[i], m_Class))
			{
				return _implicit;
			}
		}
	}

	if (JPEnv::getHost()->isWrapper(obj))
	{
		JPTypeName o = JPEnv::getHost()->getWrapperTypeName(obj);

		if (o.getSimpleName() == m_Name.getSimpleName())
		{
			return _exact;
		}
	}

	if (m_Name.getSimpleName() == "java.lang.Object")
	{
		// arrays are objects
		if (JPEnv::getHost()->isArray(obj))
		{
			return _implicit;
		}
		
		// Strings are objects too
		if (JPEnv::getHost()->isString(obj))
		{
			return _implicit;
		}
		
		// Class are objects too
		if (JPEnv::getHost()->isClass(obj) || JPEnv::getHost()->isArrayClass(obj))
		{
			return _implicit;
		}

		// Let'a allow primitives (int, long, float and boolean) to convert implicitly too ...
		if (JPEnv::getHost()->isInt(obj))
		{
			return _implicit;
		}
		
		if (JPEnv::getHost()->isLong(obj))
		{
			return _implicit;
		}
		
		if (JPEnv::getHost()->isFloat(obj))
		{
			return _implicit;
		}

		if (JPEnv::getHost()->isBoolean(obj))
		{
			return _implicit;
		}
	}

	return _none;
}
Exemplo n.º 8
0
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();
			cleaner.addLocal(obj);
			JPEnv::getJava()->Throw((jthrowable)obj);
		}
		else
		{
			JPEnv::getJava()->ThrowNew(JPJni::s_RuntimeExceptionClass, "Python exception thrown");
		}
	} 
	catch(JavaException*) 
	{ 
		cerr << "Java exception at " << __FILE__ << ":" << __LINE__ << endl; 
	}
	catch(JPypeException* ex)
	{
		JPEnv::getJava()->ThrowNew(JPJni::s_RuntimeExceptionClass, ex->getMsg());
	}

	JPEnv::getHost()->prepareCallbackFinish(callbackState);

	return NULL;

	TRACE_OUT;
}