/** * Gets the method id matching the given arguments. */ jmethodID JConstructor::getMethodID(const JClass& jClass, const JArguments& arguments) { // We cache the jmethodID locally, so if we've already found it, // we don't need to go looking for it again. if (mMethodID) return mMethodID; // If we don't already have the jmethodID, we need to determine // the signature of this method. // We construct this signature with a void return type, // because the return type for constructors is void. JSignature signature(JVoid::staticGetJavaJniClass()); typedef list<const JValue*> ValueList; ValueList args = arguments.asList(); ValueList::iterator i = args.begin(); ValueList::iterator end = args.end(); for (; i != end; ++i) { const JValue* value = *i; signature << value->getJavaJniClass(); } string methodSignature = signature.toString(); // Now that we have the signature for the method, we could look // in a global cache for the jmethodID corresponding to this method, // but for now, we'll always find it. JNIEnv* env = attach(); mMethodID = env->GetMethodID(jClass.getClass(), "<init>", methodSignature.c_str()); if (mMethodID == 0) { string msg = string("JConstructor::getMethodID(): ") + "Unable to find constructor for " + jClass.getInternalName() + " with signature " + methodSignature; try { catchAndThrow(); } catch (std::exception& e) { msg.append("\ncaused by:\n"); msg.append(e.what()); } throw JNIException(msg); } return mMethodID; }
Jvm::JField Jvm::findStaticField(const JClass& clazz, const std::string& name) { jfieldID id = env->GetStaticFieldID(findClass(clazz), name.c_str(), clazz.signature().c_str()); return Jvm::JField(clazz, id); }
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(); } }
void JavaExtractor::globalInit() { try { JClass helper("info.dolezel.fatrat.plugins.helpers.NativeHelpers"); JClass annotation("info.dolezel.fatrat.plugins.annotations.ExtractorPluginInfo"); JExtractorPlugin::registerNatives(); JArgs 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 (ExtractorPluginInfo)"; int classes = arr.size(); for (int i = 0; i < classes; i++) { try { JClass obj = (jobject) arr.getObject(i); JObject ann = obj.getAnnotation(annotation); QString regexp = ann.call("regexp", JSignature().retString()).toString(); QString name = ann.call("name", JSignature().retString()).toString(); QString targetClassName = ann.call("transferClassName", JSignature().retString()).toString(); QString clsName = obj.getClassName(); JObject instance(obj, JSignature()); if (targetClassName.isEmpty()) { JClass cls = JClass(ann.call("transferClass", JSignature().ret("java.lang.Class")).value<JObject>()); if (!cls.isNull() && cls.getClassName() != "java.lang.Object") targetClassName = cls.getClassName(); } qDebug() << "Class name:" << clsName; qDebug() << "Name:" << name; qDebug() << "Regexp:" << regexp; JavaEngine e = { "EXT - " + name.toStdString(), clsName.toStdString(), QRegExp(regexp) }; if (instance.instanceOf("info.dolezel.fatrat.plugins.extra.URLAcceptableFilter")) e.ownAcceptable = instance; if (instance.instanceOf("info.dolezel.fatrat.plugins.listeners.ConfigListener")) g_configListeners << instance; e.targetClass = targetClassName; m_engines[clsName] = e; EngineEntry entry; entry.longName = m_engines[clsName].name.c_str(); entry.shortName = m_engines[clsName].shortName.c_str(); entry.lpfnAcceptable2 = JavaExtractor::acceptable; entry.lpfnCreate2 = JavaExtractor::createInstance; entry.lpfnInit = 0; entry.lpfnExit = 0; entry.lpfnMultiOptions = 0; addTransferClass(entry, Transfer::Download); } catch (const RuntimeException& e) { qDebug() << e.what(); } } } catch (const RuntimeException& e) { qDebug() << e.what(); } }
JObject JClass::getAnnotation(JClass cls) { return toClassObject().call("getAnnotation", JSignature().add("java.lang.Class").ret("java.lang.annotation.Annotation"), JArgs() << cls.toVariant()).value<JObject>(); }