jvalue JPClassType::convertToJava(HostRef* obj) { JPCleaner cleaner; jvalue v; if (JPEnv::getHost()->isNone(obj)) { v.l = NULL; return v; } else if (JPEnv::getHost()->isWrapper(obj)) { v = JPEnv::getHost()->getWrapperValue(obj); } else { JPClass* w = JPEnv::getHost()->asClass(obj); jclass lr = w->getClass(); v.l = lr; } return v; }
HostRef* PythonHostEnvironment::newObject(JPObject* obj) { TRACE_IN("PythonHostEnvironment::newObject"); TRACE2("classname", obj->getClass()->getName().getSimpleName()); JPClass* jc = obj->getClass(); JPTypeName name = jc->getName(); PyObject* pyClass = getJavaShadowClass(jc); PyObject* args = JPySequence::newTuple(2); PyObject* arg2 = JPySequence::newTuple(1); JPySequence::setItem(arg2, 0, args); Py_DECREF(args); PyObject* joHolder = JPyCObject::fromVoidAndDesc((void*)obj, "JPObject", &deleteJPObjectDestructor); JPySequence::setItem(args, 0, m_SpecialConstructorKey); JPySequence::setItem(args, 1, joHolder); Py_DECREF(joHolder); PyObject* res = JPyObject::call(pyClass, arg2, NULL); Py_DECREF(arg2); return new HostRef(res, false); TRACE_OUT; }
PyObject* JPypeModule::synchronized(PyObject* obj, PyObject* args) { JPCleaner cleaner; TRACE_IN("synchronized"); try { PyObject* o; JPyArg::parseTuple(args, "O!", &PyCObject_Type, &o); string desc = (char*)JPyCObject::getDesc(o); jobject obj; if (desc == "JPObject") { JPObject* jpo = (JPObject*)JPyCObject::asVoidPtr(o); obj = jpo->getObject(); cleaner.addLocal(obj); } else if (desc == "JPClass") { JPClass* jpo = (JPClass*)JPyCObject::asVoidPtr(o); obj = jpo->getClass(); cleaner.addLocal(obj); } else if (desc == "JPArray") { JPArray* jpo = (JPArray*)JPyCObject::asVoidPtr(o); obj = jpo->getObject(); cleaner.addLocal(obj); } else if (desc == "JPArrayClass") { JPArrayClass* jpo = (JPArrayClass*)JPyCObject::asVoidPtr(o); obj = jpo->getClass(); cleaner.addLocal(obj); } else if (hostEnv->isWrapper(o) && hostEnv->getWrapperTypeName(o).isObjectType()) { obj = hostEnv->getWrapperValue(o).l; cleaner.addLocal(obj); } // TODO proxy else { RAISE(JPypeException, "method only accepts object values."); } PyJPMonitor* c = PyJPMonitor::alloc(new JPMonitor(obj)); return (PyObject*)c; } PY_STANDARD_CATCH; PyErr_Clear(); Py_INCREF(Py_None); return Py_None; TRACE_OUT; }
jvalue JPStringType::convertToJava(HostRef* obj) { TRACE_IN("JPStringType::convertToJava"); JPCleaner cleaner; jvalue v; if (JPEnv::getHost()->isNone(obj)) { v.l = NULL; return v; } if (JPEnv::getHost()->isWrapper(obj)) { return JPEnv::getHost()->getWrapperValue(obj); } if (JPEnv::getHost()->isObject(obj)) { JPObject* o = JPEnv::getHost()->asObject(obj); JPClass* oc = o->getClass(); if (oc->getName().getSimpleName() == "java.lang.String") { v.l = o->getObject(); return v; } } JCharString wstr = JPEnv::getHost()->stringAsJCharString(obj); jchar* jstr = new jchar[wstr.length()+1]; jstr[wstr.length()] = 0; for (size_t i = 0; i < wstr.length(); i++) { jstr[i] = (jchar)wstr[i]; } jstring res = JPEnv::getJava()->NewString(jstr, (jint)wstr.length()); delete[] jstr; v.l = res; return v; TRACE_OUT; }
jobject JPPrimitiveType::convertToJavaObject(HostRef* obj) { JPCleaner cleaner; JPTypeName tname = getObjectType(); JPClass* c = JPTypeManager::findClass(tname); jclass jc = c->getClass(); cleaner.addLocal(jc); vector<HostRef*> args(1); args[0] = obj; JPObject* o = c->newInstance(args); jobject res = o->getObject(); delete o; return res; }
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; }
string JPClass::describe() { stringstream out; JPCleaner cleaner; out << "public "; if (isAbstract()) { out << "abstract "; } if (isFinal()) { out << "final "; } out << "class " << m_Name.getSimpleName(); if (m_SuperClass != NULL) { out << " extends " << m_SuperClass->getName().getSimpleName(); } if (m_SuperInterfaces.size() > 0) { out << " implements"; bool first = true; for (vector<JPClass*>::iterator itf = m_SuperInterfaces.begin(); itf != m_SuperInterfaces.end(); itf++) { if (!first) { out << ","; } else { first = false; } JPClass* pc = *itf; out << " " << pc->getName().getSimpleName(); } } out << endl << "{" << endl; // Fields out << " // Accessible Static Fields" << endl; for (map<string, JPField*>::iterator curField = m_StaticFields.begin(); curField != m_StaticFields.end(); curField++) { JPField* f = curField->second; out << " public static "; if (f->isFinal()) { out << "final "; } out << f->getType().getSimpleName() << " " << f->getName() << ";" << endl; } out << endl; out << " // Accessible Instance Fields" << endl; for (map<string, JPField*>::iterator curInstField = m_InstanceFields.begin(); curInstField != m_InstanceFields.end(); curInstField++) { JPField* f = curInstField->second; out << " public "; if (f->isFinal()) { out << "final "; } out << f->getType().getSimpleName() << " " << f->getName() << ";" << endl; } out << endl; // Constructors out << " // Accessible Constructors" << endl; out << m_Constructors->describe(" ") << endl; out << " // Accessible Methods" << endl; for (map<string, JPMethod*>::iterator curMethod = m_Methods.begin(); curMethod != m_Methods.end(); curMethod++) { JPMethod* f = curMethod->second; out << f->describe(" "); out << endl; } out << "}"; return out.str(); }