diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-04-27 17:23:41 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-05-03 18:09:23 +0200 |
commit | d225c6afe765777bcd4f854e755f2e9c18febd7e (patch) | |
tree | c8d2d7682059982235428d89b7d074a77f1bff9e /src/corelib | |
parent | 34f72ca52e7312172f75c1f79da0d226d2d4583b (diff) |
QJniObject: add callStatic[Object]Method overloads for jmethodID
This patch extends the QJniObject::callStatic[Object]Method functions
with the overload which accepts a jmethodID parameter. This can be
convenient when the method id is already cached and you do not want
to query the method by its name and signature.
Task-number: QTBUG-92952
Change-Id: Ib0852a5a27da2a244ac63112784751ef9e32cfa5
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/kernel/qjniobject.cpp | 48 | ||||
-rw-r--r-- | src/corelib/kernel/qjniobject.h | 32 |
2 files changed, 80 insertions, 0 deletions
diff --git a/src/corelib/kernel/qjniobject.cpp b/src/corelib/kernel/qjniobject.cpp index dbca5321a1..319a7ba0d1 100644 --- a/src/corelib/kernel/qjniobject.cpp +++ b/src/corelib/kernel/qjniobject.cpp @@ -917,6 +917,25 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz, */ /*! + \fn template <typename T> T QJniObject::callStaticMethod(jclass clazz, jmethodID methodId, ...) + + Calls the static method identified by \a methodId from the class \a clazz + with any subsequent arguments. Useful when \a clazz and \a methodId are + already cached from previous operations. + + \code + QJniEnvironment env; + jclass javaMathClass = env.findClass("java/lang/Math"); + jmethodID methodId = env.findStaticMethod(javaMathClass, "max", "(II)I"); + if (methodId != 0) { + jint a = 2; + jint b = 4; + jint max = QJniObject::callStaticMethod<jint>(javaMathClass, methodId, a, b); + } + \endcode +*/ + +/*! \fn template <typename T> T QJniObject::callStaticMethod(jclass clazz, const char *methodName) Calls the static method \a methodName on \a clazz and returns the value. @@ -1014,6 +1033,35 @@ QJniObject QJniObject::callStaticObjectMethod(jclass clazz, const char *methodNa } /*! + \fn QJniObject QJniObject::callStaticObjectMethod(jclass clazz, jmethodID methodId, ...) + + Calls the static method identified by \a methodId from the class \a clazz + with any subsequent arguments. Useful when \a clazz and \a methodId are + already cached from previous operations. + + \code + QJniEnvironment env; + jclass clazz = env.findClass("java/lang/String"); + jmethodID methodId = env.findStaticMethod(clazz, "valueOf", "(I)Ljava/lang/String;"); + if (methodId != 0) + QJniObject str = QJniObject::callStaticObjectMethod(clazz, methodId, 10); + \endcode +*/ +QJniObject QJniObject::callStaticObjectMethod(jclass clazz, jmethodID methodId, ...) +{ + QJniEnvironment env; + if (clazz && methodId) { + va_list args; + va_start(args, methodId); + QJniObject res = getCleanJniObject(env->CallStaticObjectMethodV(clazz, methodId, args)); + va_end(args); + return res; + } + + return QJniObject(); +} + +/*! \fn QJniObject QJniObject::callObjectMethod(const char *methodName) const Calls the Java objects method \a methodName and returns a new QJniObject for diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index 83d64ad485..187eccc35d 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -219,6 +219,36 @@ public: } } + template <typename T> + static T callStaticMethod(jclass clazz, jmethodID methodId, ...) + { + assertJniPrimitiveType<T>(); + QJniEnvironment env; + T res{}; + if (clazz && methodId) { + va_list args; + va_start(args, methodId); + callStaticMethodForType<T>(env.jniEnv(), res, clazz, methodId, args); + va_end(args); + if (env.checkAndClearExceptions()) + res = {}; + } + return res; + } + + template <> + void callStaticMethod<void>(jclass clazz, jmethodID methodId, ...) + { + QJniEnvironment env; + if (clazz && methodId) { + va_list args; + va_start(args, methodId); + env->CallStaticVoidMethodV(clazz, methodId, args); + va_end(args); + env.checkAndClearExceptions(); + } + } + template <typename T> static T callStaticMethod(jclass clazz, const char *methodName) { assertJniPrimitiveType<T>(); @@ -254,6 +284,8 @@ public: static QJniObject callStaticObjectMethod(jclass clazz, const char *methodName, const char *signature, ...); + static QJniObject callStaticObjectMethod(jclass clazz, jmethodID methodId, ...); + template <typename T> T getField(const char *fieldName) const { assertJniPrimitiveType<T>(); |