Beispiel #1
0
PyObject* convertToJValue(PyObject* self, PyObject* arg)
{
	try {
		char* tname;
		PyObject* value;

		JPyArg::parseTuple(arg, "sO", &tname, &value);

		JPTypeName name = JPTypeName::fromSimple(tname);
		JPType* type = JPTypeManager::getType(name);

		HostRef ref(value);
		jvalue v = type->convertToJava(&ref);

		jvalue* pv = new jvalue();
		*pv = v;

		PyObject* res;
		if (type->isObjectType())
		{
			res = JPyCObject::fromVoidAndDesc((void*)pv, (void*)"object jvalue", PythonHostEnvironment::deleteObjectJValueDestructor);
		}
		else
		{
			res = JPyCObject::fromVoidAndDesc((void*)pv, (void*)"jvalue", PythonHostEnvironment::deleteJValueDestructor);
		}

		return res;
	}
	PY_STANDARD_CATCH

	return NULL;
}
Beispiel #2
0
JPObject* JPMethodOverload::invokeConstructor(jclass claz, vector<HostRef*>& arg)
{
	TRACE_IN("JPMethodOverload::invokeConstructor");

	size_t len = arg.size();
	JPCleaner cleaner;
	
	JPMallocCleaner<jvalue> v(len);

	for (unsigned int i = 0; i < len; i++)
	{
		HostRef* obj = arg[i];
		// TODO the following can easily be optimized ... or at least cached
		JPType* t = JPTypeManager::getType(m_Arguments[i]);

		v[i] = t->convertToJava(obj);		
		if (t->isObjectType())
		{
			cleaner.addLocal(v[i].l);
		}
	}
	
	jvalue val;
	val.l = JPEnv::getJava()->NewObjectA(claz, m_MethodID, v.borrow());
	cleaner.addLocal(val.l);
	TRACE1("Object created");
	
	JPTypeName name = JPJni::getName(claz);
	return new JPObject(name, val.l);

	TRACE_OUT;
}
Beispiel #3
0
HostRef* JPMethodOverload::invokeInstance(vector<HostRef*>& args)
{
	TRACE_IN("JPMethodOverload::invokeInstance");
	HostRef* res;
	{
		JPCleaner cleaner;
	
		// Arg 0 is "this"
		HostRef* self = args[0];
		JPObject* selfObj = JPEnv::getHost()->asObject(self);
	
		size_t len = args.size();
	
		JPMallocCleaner<jvalue> v(len-1);
		
		for (unsigned int i = 1; i < len; i++)
		{
			HostRef* obj = args[i];
	
			JPType* type = JPTypeManager::getType(m_Arguments[i]);
			v[i-1] = type->convertToJava(obj);
			// below seems not to be necessary, since jvalues are clean by jpmalloccleaner
//			if (type->isObjectType())
//			{
//			//TODO : investigate: this is not valid for either local or global ref
//				// add hostref to garbage collection
//				cleaner.add(v[i-1].l);
//			}
		}
		
		JPType* retType = JPTypeManager::getType(m_ReturnType);
	
		jobject c = selfObj->getObject();
		cleaner.addLocal(c);
	
		jclass clazz = m_Class->getClass();
		cleaner.addGlobal(clazz);
	
		res = retType->invoke(c, clazz, m_MethodID, v.borrow());
		TRACE1("Call finished");
	}
	TRACE1("Call successfull");
	
	return res;

	TRACE_OUT;
}
Beispiel #4
0
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;
}