void JClass::registerNativeMethods(const QList<JNativeMethod>& m) { if (m.isEmpty()) return; JNINativeMethod* nm = new JNINativeMethod[m.size()]; for (int i = 0; i < m.size(); i++) nm[i] = m[i].toStruct(); JNIEnv* env = *JVM::instance(); env->RegisterNatives(m_class, nm, m.size()); delete [] nm; JObject ex = env->ExceptionOccurred(); if (!ex.isNull()) { env->ExceptionClear(); ex.call("printStackTrace"); QString message = ex.call("getMessage", JSignature().retString()).toString(); QString className = ex.getClass().getClassName(); throw JException(message, className, ex); } }
void JavaUpload::globalInit() { if (!JVM::JVMAvailable()) return; // locate Java plugins try { JUploadPlugin::registerNatives(); JClass helper("info.dolezel.fatrat.plugins.helpers.NativeHelpers"); JClass annotation("info.dolezel.fatrat.plugins.annotations.UploadPluginInfo"); JClass annConfigDialog("info.dolezel.fatrat.plugins.annotations.ConfigDialog"); QList<QVariant> args; args << "info.dolezel.fatrat.plugins" << annotation.toVariant(); JArray arr = helper.callStatic("findAnnotatedClasses", JSignature().addString().add("java.lang.Class").retA("java.lang.Class"), args).value<JArray>(); qDebug() << "Found" << arr.size() << "annotated classes (UploadPluginInfo)"; int classes = arr.size(); for (int i = 0; i < classes; i++) { try { JClass obj = (jobject) arr.getObject(i); JObject ann = obj.getAnnotation(annotation); QString name = ann.call("name", JSignature().retString()).toString(); QString clsName = obj.getClassName(); JObject instance(obj, JSignature()); qDebug() << "Class name:" << clsName; qDebug() << "Name:" << name; JObject cfgDlg = obj.getAnnotation(annConfigDialog); JavaEngine e = { "EXT - " + name.toStdString(), clsName.toStdString() }; if (!cfgDlg.isNull()) e.configDialog = cfgDlg.call("value", JSignature().retString()).toString(); if (instance.instanceOf("info.dolezel.fatrat.plugins.extra.URLAcceptableFilter")) e.ownAcceptable = instance; if (instance.instanceOf("info.dolezel.fatrat.plugins.listeners.ConfigListener")) g_configListeners << instance; m_engines[clsName] = e; qDebug() << "createInstance of " << clsName; EngineEntry entry; entry.longName = m_engines[clsName].name.c_str(); entry.shortName = m_engines[clsName].shortName.c_str(); entry.lpfnAcceptable2 = JavaUpload::acceptable; entry.lpfnCreate2 = JavaUpload::createInstance; entry.lpfnInit = 0; entry.lpfnExit = 0; entry.lpfnMultiOptions = 0; addTransferClass(entry, Transfer::Upload); } catch (const RuntimeException& e) { qDebug() << e.what(); } } } catch (const RuntimeException& e) { qDebug() << e.what(); } }
QVariant JClass::callStatic(const char* name, const char* sig, QList<QVariant> args) { JScope s; JNIEnv* env = *JVM::instance(); jmethodID mid = env->GetStaticMethodID(m_class, name, sig); if (!mid) throw RuntimeException(QObject::tr("Method %1 %2 not found").arg(name).arg(sig)); JValue vals[args.size()]; jvalue jargs[args.size()]; for(int i=0;i<args.size();i++) { vals[i] = variantToValue(args[i]); jargs[i] = vals[i]; } const char* rvtype = strchr(sig, ')'); if (!rvtype) throw RuntimeException(QObject::tr("Invalid method return type").arg(name).arg(sig)); rvtype++; QVariant retval; switch (*rvtype) { case 'V': env->CallStaticVoidMethodA(m_class, mid, jargs); break; case '[': { jobject obj = env->CallStaticObjectMethodA(m_class, mid, jargs); retval.setValue<JArray>(JArray(obj)); break; } case 'L': { jclass string_class = env->FindClass("java/lang/String"); jobject obj = env->CallStaticObjectMethodA(m_class, mid, jargs); if (obj && env->IsInstanceOf(obj, string_class) && !strcmp(rvtype+1, "java/lang/String;")) retval = JString(jstring(obj)).str(); else { QVariant var; var.setValue<JObject>(JObject(obj)); retval = var; } break; } case 'Z': retval = (bool) env->CallStaticBooleanMethodA(m_class, mid, jargs); break; case 'B': retval = env->CallStaticByteMethodA(m_class, mid, jargs); break; case 'C': retval = env->CallStaticCharMethodA(m_class, mid, jargs); break; case 'S': retval = env->CallStaticShortMethodA(m_class, mid, jargs); break; case 'I': retval = env->CallStaticIntMethodA(m_class, mid, jargs); break; case 'J': retval = (qlonglong) env->CallStaticLongMethodA(m_class, mid, jargs); break; case 'F': retval = env->CallStaticFloatMethodA(m_class, mid, jargs); break; case 'D': retval = env->CallStaticDoubleMethodA(m_class, mid, jargs); break; default: throw RuntimeException(QObject::tr("Unknown Java data type: %1").arg(*rvtype)); } JObject ex = env->ExceptionOccurred(); if (!ex.isNull()) { env->ExceptionClear(); ex.call("printStackTrace"); QString message = ex.call("getMessage", JSignature().retString()).toString(); QString className = ex.getClass().getClassName(); throw JException(message, className, ex); } return retval; }