Exemplo n.º 1
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.º 2
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;
}