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>
This commit is contained in:
Volker Hilsheimer 2022-04-30 14:22:39 +02:00
parent 18a16533b9
commit f6e89e901b
3 changed files with 158 additions and 104 deletions

View File

@ -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 \since 6.4
Calls the object's method \a methodName with \a signature specifying the types of any 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 \code
QJniObject myJavaStrin("org/qtproject/qt/TestClass"); QJniObject myJavaStrin("org/qtproject/qt/TestClass");
jint index = myJavaString.callMethod<jint>("indexOf", "(I)I", 0x0051); jint index = myJavaString.callMethod<jint>("indexOf", "(I)I", 0x0051);
\endcode \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 \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 \code
QJniObject myJavaStrin("org/qtproject/qt/TestClass"); 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 \since 6.4
Calls the static method \a methodName from class \a className with \a signature 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 \code
jint a = 2; 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 \since 6.4
Calls the static method \a methodName on class \a className with arguments \a args, 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 \code
jint value = QJniObject::callStaticMethod<jint>("MyClass", "staticMethod"); 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 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 \code
QJniEnvironment env; 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 \since 6.4
Calls the static method identified by \a methodId from the class \a clazz 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 with any subsequent arguments, and returns the value of type \c Ret (unless
already cached from previous operations. \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 \code
QJniEnvironment env; 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 \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 \code
QJniEnvironment env; QJniEnvironment env;

View File

@ -55,35 +55,38 @@ public:
QByteArray className() const; QByteArray className() const;
template <typename Ret, typename ...Args> 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>(); if constexpr (QtJniTypes::isObjectType<Ret>()) {
QJniEnvironment env; return callObjectMethod(methodName, signature, std::forward<Args>(args)...);
jmethodID id = getCachedMethodID(env.jniEnv(), methodName, signature); } else {
if (id) { QtJniTypes::assertPrimitiveType<Ret>();
if constexpr (std::is_same<Ret, void>::value) { QJniEnvironment env;
callVoidMethodV(env.jniEnv(), id, std::forward<Args>(args)...); jmethodID id = getCachedMethodID(env.jniEnv(), methodName, signature);
env.checkAndClearExceptions(); if (id) {
} else { if constexpr (std::is_same<Ret, void>::value) {
Ret res{}; callVoidMethodV(env.jniEnv(), id, std::forward<Args>(args)...);
callMethodForType<Ret>(env.jniEnv(), res, object(), id, std::forward<Args>(args)...); env.checkAndClearExceptions();
if (env.checkAndClearExceptions()) } else {
res = {}; Ret res{};
return 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> 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...>(); constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
if constexpr (std::is_same<Ret, void>::value) { if constexpr (std::is_same<Ret, void>::value) {
callMethod<void>(methodName, signature.data(), std::forward<Args>(args)...); callMethod<void>(methodName, signature.data(), std::forward<Args>(args)...);
} else { } else {
QtJniTypes::assertPrimitiveType<Ret>();
return callMethod<Ret>(methodName, signature.data(), std::forward<Args>(args)...); return callMethod<Ret>(methodName, signature.data(), std::forward<Args>(args)...);
} }
} }
@ -99,61 +102,57 @@ public:
QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const; QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const;
template <typename Ret, typename ...Args> 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; QJniEnvironment env;
jclass clazz = QJniObject::loadClass(className, env.jniEnv()); jclass clazz = QJniObject::loadClass(className, env.jniEnv());
return callStaticMethod<Ret>(clazz, methodName, signature, std::forward<Args>(args)...); return callStaticMethod<Ret>(clazz, methodName, signature, std::forward<Args>(args)...);
} }
template <typename Ret, typename ...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; QJniEnvironment env;
if (clazz) { jmethodID id = getMethodID(env.jniEnv(), clazz, methodName, signature, true);
jmethodID id = getMethodID(env.jniEnv(), clazz, methodName, signature, true); return callStaticMethod<Ret, Args...>(clazz, id, std::forward<Args>(args)...);
return callStaticMethod<Ret, Args...>(clazz, id, std::forward<Args>(args)...);
}
if constexpr (!std::is_same<Ret, void>::value)
return Ret{};
} }
template <typename Ret, typename ...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>(); if constexpr (QtJniTypes::isObjectType<Ret>()) {
QJniEnvironment env; return callStaticObjectMethod(clazz, methodId, std::forward<Args>(args)...);
if (clazz && methodId) { } else {
if constexpr (std::is_same<Ret, void>::value) { QtJniTypes::assertPrimitiveType<Ret>();
callStaticMethodForVoid(env.jniEnv(), clazz, methodId, std::forward<Args>(args)...); QJniEnvironment env;
env.checkAndClearExceptions(); if (clazz && methodId) {
} else { if constexpr (std::is_same<Ret, void>::value) {
Ret res{}; callStaticMethodForVoid(env.jniEnv(), clazz, methodId, std::forward<Args>(args)...);
callStaticMethodForType<Ret>(env.jniEnv(), res, clazz, methodId, std::forward<Args>(args)...); env.checkAndClearExceptions();
if (env.checkAndClearExceptions()) } else {
res = {}; Ret res{};
return 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> 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; QJniEnvironment env;
jclass clazz = QJniObject::loadClass(className, env.jniEnv()); jclass clazz = QJniObject::loadClass(className, env.jniEnv());
return callStaticMethod<Ret, Args...>(clazz, methodName, std::forward<Args>(args)...); return callStaticMethod<Ret, Args...>(clazz, methodName, std::forward<Args>(args)...);
} }
template <typename Ret, typename ...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...>(); constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
return callStaticMethod<Ret>(clazz, methodName, signature.data(), std::forward<Args>(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)...); 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>(); if constexpr (QtJniTypes::isObjectType<T>()) {
QJniEnvironment env; return getObjectField<T>(fieldName);
T res{}; } else {
constexpr auto signature = QtJniTypes::fieldSignature<T>(); QtJniTypes::assertPrimitiveType<T>();
jfieldID id = getCachedFieldID(env.jniEnv(), fieldName, signature); QJniEnvironment env;
if (id) { T res{};
getFieldForType<T>(env.jniEnv(), res, object(), id); constexpr auto signature = QtJniTypes::fieldSignature<T>();
if (env.checkAndClearExceptions()) jfieldID id = getCachedFieldID(env.jniEnv(), fieldName, signature);
res = {}; if (id) {
getFieldForType<T>(env.jniEnv(), res, object(), id);
if (env.checkAndClearExceptions())
res = {};
}
return res;
} }
return res;
} }
template <typename T> template <typename T>
static T getStaticField(const char *className, const char *fieldName) static auto getStaticField(const char *className, const char *fieldName)
{ {
QtJniTypes::assertPrimitiveType<T>(); if constexpr (QtJniTypes::isObjectType<T>()) {
QJniEnvironment env; return getStaticObjectField<T>(className, fieldName);
jclass clazz = QJniObject::loadClass(className, env.jniEnv()); } else {
if (!clazz) QtJniTypes::assertPrimitiveType<T>();
return 0; QJniEnvironment env;
jclass clazz = QJniObject::loadClass(className, env.jniEnv());
T res{};
if (!clazz)
return res;
constexpr auto signature = QtJniTypes::fieldSignature<T>(); constexpr auto signature = QtJniTypes::fieldSignature<T>();
jfieldID id = getCachedFieldID(env.jniEnv(), clazz, jfieldID id = getCachedFieldID(env.jniEnv(), clazz,
QJniObject::toBinaryEncClassName(className), QJniObject::toBinaryEncClassName(className),
fieldName, fieldName,
signature, true); signature, true);
if (!id) if (!id)
return 0; return res;
T 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)
{
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); getStaticFieldForType<T>(env.jniEnv(), res, clazz, id);
if (env.checkAndClearExceptions()) if (env.checkAndClearExceptions())
res = {}; res = {};
return res;
}
}
template <typename T>
static auto getStaticField(jclass clazz, const char *fieldName)
{
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> template <typename T>

View File

@ -201,6 +201,17 @@ void tst_QJniObject::callMethodTest()
jlong ret = longObject.callMethod<jlong>("longValue"); jlong ret = longObject.callMethod<jlong>("longValue");
QCOMPARE(ret, jLong); QCOMPARE(ret, jLong);
} }
// as of Qt 6.4, callMethod works with an object type as well!
{
const QString qString = QLatin1String("Hello, Java");
QJniObject jString = QJniObject::fromString(qString);
const QString qStringRet = jString.callMethod<jstring>("toUpperCase").toString();
QCOMPARE(qString.toUpper(), qStringRet);
QJniObject subString = jString.callMethod<jstring>("substring", 0, 4);
QCOMPARE(subString.toString(), qString.mid(0, 4));
}
} }
void tst_QJniObject::callObjectMethodTest() void tst_QJniObject::callObjectMethodTest()
@ -316,6 +327,17 @@ void tst_QJniObject::callStaticObjectMethod()
returnedString = returnValue.toString(); returnedString = returnValue.toString();
QCOMPARE(returnedString, QString::fromLatin1("test format")); QCOMPARE(returnedString, QString::fromLatin1("test format"));
// from 6.4 on we can use callStaticMethod
returnValue = QJniObject::callStaticMethod<jstring>(cls,
"format",
formatString.object<jstring>(),
jobjectArray(0));
QVERIFY(returnValue.isValid());
returnedString = returnValue.toString();
QCOMPARE(returnedString, QString::fromLatin1("test format"));
} }
void tst_QJniObject::callStaticObjectMethodById() void tst_QJniObject::callStaticObjectMethodById()
@ -338,6 +360,15 @@ void tst_QJniObject::callStaticObjectMethodById()
QString returnedString = returnValue.toString(); QString returnedString = returnValue.toString();
QCOMPARE(returnedString, QString::fromLatin1("test format")); QCOMPARE(returnedString, QString::fromLatin1("test format"));
// from Qt 6.4 on we can use callStaticMethod as well
returnValue = QJniObject::callStaticMethod<jstring>(
cls, id, formatString.object<jstring>(), jobjectArray(0));
QVERIFY(returnValue.isValid());
returnedString = returnValue.toString();
QCOMPARE(returnedString, QString::fromLatin1("test format"));
} }
void tst_QJniObject::callStaticBooleanMethod() void tst_QJniObject::callStaticBooleanMethod()