summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-04-30 14:22:39 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-05-17 00:34:06 +0200
commitf6e89e901ba718d64b50f79f3b1b327a7f6a0211 (patch)
tree5a4c7752642f28dd1102fbb74cd83b0856b9115b /src
parent18a16533b9c8782c5ff642a37dd58f82c1eb881d (diff)
Fold methods for object return type into generic methods
Since we know at compile time whether the return type is an object type, we can use 'if constexpr' and auto return type in the call(Static)Method and get(Static)Field functions to call the object-type methods. This makes the object-methods conceptually obsolete, but don't declare them as deprecated as long as they are still used in submodules to avoid warning floods and build failures in -Werror configurations. Change-Id: Ic3019ed990a9252eefcb02cdb355f8a6ed6bc2ff Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/kernel/qjniobject.cpp44
-rw-r--r--src/corelib/kernel/qjniobject.h185
2 files changed, 126 insertions, 103 deletions
diff --git a/src/corelib/kernel/qjniobject.cpp b/src/corelib/kernel/qjniobject.cpp
index 045b9316cb..8f21835a1f 100644
--- a/src/corelib/kernel/qjniobject.cpp
+++ b/src/corelib/kernel/qjniobject.cpp
@@ -903,24 +903,26 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz,
}
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callMethod(const char *methodName, const char *signature, Args &&...args) const
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callMethod(const char *methodName, const char *signature, Args &&...args) const
\since 6.4
Calls the object's method \a methodName with \a signature specifying the types of any
- subsequent arguments \a args.
+ subsequent arguments \a args, and returns the value (unless \c Ret is \c void). If \c Ret
+ is a jobject type, then the returned value will be a QJniObject.
\code
QJniObject myJavaStrin("org/qtproject/qt/TestClass");
jint index = myJavaString.callMethod<jint>("indexOf", "(I)I", 0x0051);
\endcode
-
*/
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callMethod(const char *methodName, Args &&...args) const
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callMethod(const char *methodName, Args &&...args) const
\since 6.4
- Calls the method \a methodName with arguments \a args and returns the value.
+ Calls the method \a methodName with arguments \a args and returns the value
+ (unless \c Ret is \c void). If \c Ret is a jobject type, then the returned value
+ will be a QJniObject.
\code
QJniObject myJavaStrin("org/qtproject/qt/TestClass");
@@ -931,11 +933,13 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz,
*/
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args)
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args)
\since 6.4
Calls the static method \a methodName from class \a className with \a signature
- specifying the types of any subsequent arguments \a args.
+ specifying the types of any subsequent arguments \a args. Returns the result of
+ the method (unless \c Ret is \c void). If \c Ret is a jobject type, then the
+ returned value will be a QJniObject.
\code
jint a = 2;
@@ -945,11 +949,12 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz,
*/
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callStaticMethod(const char *className, const char *methodName, Args &&...args)
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callStaticMethod(const char *className, const char *methodName, Args &&...args)
\since 6.4
Calls the static method \a methodName on class \a className with arguments \a args,
- and returns the value of type \c Ret.
+ and returns the value of type \c Ret (unless \c Ret is \c void). If \c Ret
+ is a jobject type, then the returned value will be a QJniObject.
\code
jint value = QJniObject::callStaticMethod<jint>("MyClass", "staticMethod");
@@ -959,10 +964,12 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz,
*/
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
Calls the static method \a methodName from \a clazz with \a signature
- specifying the types of any subsequent arguments.
+ specifying the types of any subsequent arguments. Returns the result of
+ the method (unless \c Ret is \c void). If \c Ret is a jobject type, then the
+ returned value will be a QJniObject.
\code
QJniEnvironment env;
@@ -974,12 +981,15 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz,
*/
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callStaticMethod(jclass clazz, jmethodID methodId, Args &&...args)
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callStaticMethod(jclass clazz, jmethodID methodId, Args &&...args)
\since 6.4
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.
+ with any subsequent arguments, and returns the value of type \c Ret (unless
+ \c Ret is \c void). If \c Ret is a jobject type, then the returned value will
+ be a QJniObject.
+
+ Useful when \a clazz and \a methodId are already cached from previous operations.
\code
QJniEnvironment env;
@@ -994,10 +1004,12 @@ QJniObject QJniObject::callStaticObjectMethodV(jclass clazz,
*/
/*!
- \fn template <typename Ret, typename ...Args> Ret QJniObject::callStaticMethod(jclass clazz, const char *methodName, Args &&...args)
+ \fn template <typename Ret, typename ...Args> auto QJniObject::callStaticMethod(jclass clazz, const char *methodName, Args &&...args)
\since 6.4
- Calls the static method \a methodName on \a clazz and returns the value.
+ Calls the static method \a methodName on \a clazz and returns the value of type \c Ret
+ (unless c Ret is \c void). If \c Ret if a jobject type, then the returned value will
+ be a QJniObject.
\code
QJniEnvironment env;
diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h
index bb3f49c94f..0d5c95b405 100644
--- a/src/corelib/kernel/qjniobject.h
+++ b/src/corelib/kernel/qjniobject.h
@@ -55,35 +55,38 @@ public:
QByteArray className() const;
template <typename Ret, typename ...Args>
- Ret callMethod(const char *methodName, const char *signature, Args &&...args) const
+ auto callMethod(const char *methodName, const char *signature, Args &&...args) const
{
- QtJniTypes::assertPrimitiveType<Ret>();
- QJniEnvironment env;
- jmethodID id = getCachedMethodID(env.jniEnv(), methodName, signature);
- if (id) {
- if constexpr (std::is_same<Ret, void>::value) {
- callVoidMethodV(env.jniEnv(), id, std::forward<Args>(args)...);
- env.checkAndClearExceptions();
- } else {
- Ret res{};
- callMethodForType<Ret>(env.jniEnv(), res, object(), id, std::forward<Args>(args)...);
- if (env.checkAndClearExceptions())
- res = {};
- return res;
+ if constexpr (QtJniTypes::isObjectType<Ret>()) {
+ return callObjectMethod(methodName, signature, std::forward<Args>(args)...);
+ } else {
+ QtJniTypes::assertPrimitiveType<Ret>();
+ QJniEnvironment env;
+ jmethodID id = getCachedMethodID(env.jniEnv(), methodName, signature);
+ if (id) {
+ if constexpr (std::is_same<Ret, void>::value) {
+ callVoidMethodV(env.jniEnv(), id, std::forward<Args>(args)...);
+ env.checkAndClearExceptions();
+ } else {
+ Ret res{};
+ callMethodForType<Ret>(env.jniEnv(), res, object(), id, std::forward<Args>(args)...);
+ if (env.checkAndClearExceptions())
+ res = {};
+ return res;
+ }
}
+ if constexpr (!std::is_same<Ret, void>::value)
+ return Ret{};
}
- if constexpr (!std::is_same<Ret, void>::value)
- return Ret{};
}
template <typename Ret, typename ...Args>
- Ret callMethod(const char *methodName, Args &&...args) const
+ auto callMethod(const char *methodName, Args &&...args) const
{
constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
if constexpr (std::is_same<Ret, void>::value) {
callMethod<void>(methodName, signature.data(), std::forward<Args>(args)...);
} else {
- QtJniTypes::assertPrimitiveType<Ret>();
return callMethod<Ret>(methodName, signature.data(), std::forward<Args>(args)...);
}
}
@@ -99,61 +102,57 @@ public:
QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const;
template <typename Ret, typename ...Args>
- static Ret callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args)
+ static auto callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args)
{
- QtJniTypes::assertPrimitiveType<Ret>();
QJniEnvironment env;
jclass clazz = QJniObject::loadClass(className, env.jniEnv());
return callStaticMethod<Ret>(clazz, methodName, signature, std::forward<Args>(args)...);
}
template <typename Ret, typename ...Args>
- static Ret callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
+ static auto callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
{
- QtJniTypes::assertPrimitiveType<Ret>();
QJniEnvironment env;
- if (clazz) {
- jmethodID id = getMethodID(env.jniEnv(), clazz, methodName, signature, true);
- return callStaticMethod<Ret, Args...>(clazz, id, std::forward<Args>(args)...);
- }
- if constexpr (!std::is_same<Ret, void>::value)
- return Ret{};
+ jmethodID id = getMethodID(env.jniEnv(), clazz, methodName, signature, true);
+ return callStaticMethod<Ret, Args...>(clazz, id, std::forward<Args>(args)...);
}
template <typename Ret, typename ...Args>
- static Ret callStaticMethod(jclass clazz, jmethodID methodId, Args &&...args)
+ static auto callStaticMethod(jclass clazz, jmethodID methodId, Args &&...args)
{
- QtJniTypes::assertPrimitiveType<Ret>();
- QJniEnvironment env;
- if (clazz && methodId) {
- if constexpr (std::is_same<Ret, void>::value) {
- callStaticMethodForVoid(env.jniEnv(), clazz, methodId, std::forward<Args>(args)...);
- env.checkAndClearExceptions();
- } else {
- Ret res{};
- callStaticMethodForType<Ret>(env.jniEnv(), res, clazz, methodId, std::forward<Args>(args)...);
- if (env.checkAndClearExceptions())
- res = {};
- return res;
+ if constexpr (QtJniTypes::isObjectType<Ret>()) {
+ return callStaticObjectMethod(clazz, methodId, std::forward<Args>(args)...);
+ } else {
+ QtJniTypes::assertPrimitiveType<Ret>();
+ QJniEnvironment env;
+ if (clazz && methodId) {
+ if constexpr (std::is_same<Ret, void>::value) {
+ callStaticMethodForVoid(env.jniEnv(), clazz, methodId, std::forward<Args>(args)...);
+ env.checkAndClearExceptions();
+ } else {
+ Ret res{};
+ callStaticMethodForType<Ret>(env.jniEnv(), res, clazz, methodId, std::forward<Args>(args)...);
+ if (env.checkAndClearExceptions())
+ res = {};
+ return res;
+ }
}
+ if constexpr (!std::is_same<Ret, void>::value)
+ return Ret{};
}
- if constexpr (!std::is_same<Ret, void>::value)
- return Ret{};
}
template <typename Ret, typename ...Args>
- static Ret callStaticMethod(const char *className, const char *methodName, Args &&...args)
+ static auto callStaticMethod(const char *className, const char *methodName, Args &&...args)
{
- QtJniTypes::assertPrimitiveType<Ret>();
QJniEnvironment env;
jclass clazz = QJniObject::loadClass(className, env.jniEnv());
return callStaticMethod<Ret, Args...>(clazz, methodName, std::forward<Args>(args)...);
}
template <typename Ret, typename ...Args>
- static Ret callStaticMethod(jclass clazz, const char *methodName, Args &&...args)
+ static auto callStaticMethod(jclass clazz, const char *methodName, Args &&...args)
{
- QtJniTypes::assertPrimitiveType<Ret>();
constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
return callStaticMethod<Ret>(clazz, methodName, signature.data(), std::forward<Args>(args)...);
}
@@ -183,59 +182,71 @@ public:
return callStaticObjectMethod(clazz, methodName, signature.data(), std::forward<Args>(args)...);
}
- template <typename T> T getField(const char *fieldName) const
+ template <typename T> auto getField(const char *fieldName) const
{
- QtJniTypes::assertPrimitiveType<T>();
- QJniEnvironment env;
- T res{};
- constexpr auto signature = QtJniTypes::fieldSignature<T>();
- jfieldID id = getCachedFieldID(env.jniEnv(), fieldName, signature);
- if (id) {
- getFieldForType<T>(env.jniEnv(), res, object(), id);
- if (env.checkAndClearExceptions())
- res = {};
+ if constexpr (QtJniTypes::isObjectType<T>()) {
+ return getObjectField<T>(fieldName);
+ } else {
+ QtJniTypes::assertPrimitiveType<T>();
+ QJniEnvironment env;
+ T res{};
+ constexpr auto signature = QtJniTypes::fieldSignature<T>();
+ jfieldID id = getCachedFieldID(env.jniEnv(), fieldName, signature);
+ if (id) {
+ getFieldForType<T>(env.jniEnv(), res, object(), id);
+ if (env.checkAndClearExceptions())
+ res = {};
+ }
+ return res;
}
- return res;
}
template <typename T>
- static T getStaticField(const char *className, const char *fieldName)
+ static auto getStaticField(const char *className, const char *fieldName)
{
- QtJniTypes::assertPrimitiveType<T>();
- QJniEnvironment env;
- jclass clazz = QJniObject::loadClass(className, env.jniEnv());
- if (!clazz)
- return 0;
+ if constexpr (QtJniTypes::isObjectType<T>()) {
+ return getStaticObjectField<T>(className, fieldName);
+ } else {
+ QtJniTypes::assertPrimitiveType<T>();
+ QJniEnvironment env;
+ jclass clazz = QJniObject::loadClass(className, env.jniEnv());
+ T res{};
+ if (!clazz)
+ return res;
- constexpr auto signature = QtJniTypes::fieldSignature<T>();
- jfieldID id = getCachedFieldID(env.jniEnv(), clazz,
- QJniObject::toBinaryEncClassName(className),
- fieldName,
- signature, true);
- if (!id)
- return 0;
+ constexpr auto signature = QtJniTypes::fieldSignature<T>();
+ jfieldID id = getCachedFieldID(env.jniEnv(), clazz,
+ QJniObject::toBinaryEncClassName(className),
+ fieldName,
+ signature, true);
+ if (!id)
+ return res;
- T res{};
- getStaticFieldForType<T>(env.jniEnv(), res, clazz, id);
- if (env.checkAndClearExceptions())
- res = {};
- return res;
+ getStaticFieldForType<T>(env.jniEnv(), res, clazz, id);
+ if (env.checkAndClearExceptions())
+ res = {};
+ return res;
+ }
}
template <typename T>
- static T getStaticField(jclass clazz, const char *fieldName)
+ static auto getStaticField(jclass clazz, const char *fieldName)
{
- QtJniTypes::assertPrimitiveType<T>();
- QJniEnvironment env;
- T res{};
- constexpr auto signature = QtJniTypes::fieldSignature<T>();
- jfieldID id = getFieldID(env.jniEnv(), clazz, fieldName, signature, true);
- if (id) {
- getStaticFieldForType<T>(env.jniEnv(), res, clazz, id);
- if (env.checkAndClearExceptions())
- res = {};
+ if constexpr (QtJniTypes::isObjectType<T>()) {
+ return getStaticObjectField<T>(clazz, fieldName);
+ } else {
+ QtJniTypes::assertPrimitiveType<T>();
+ QJniEnvironment env;
+ T res{};
+ constexpr auto signature = QtJniTypes::fieldSignature<T>();
+ jfieldID id = getFieldID(env.jniEnv(), clazz, fieldName, signature, true);
+ if (id) {
+ getStaticFieldForType<T>(env.jniEnv(), res, clazz, id);
+ if (env.checkAndClearExceptions())
+ res = {};
+ }
+ return res;
}
- return res;
}
template <typename T>