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
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;

View File

@ -55,8 +55,11 @@ 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
{
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);
@ -75,15 +78,15 @@ public:
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,30 +102,27 @@ 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{};
}
template <typename Ret, typename ...Args>
static Ret callStaticMethod(jclass clazz, jmethodID methodId, Args &&...args)
static auto callStaticMethod(jclass clazz, jmethodID methodId, Args &&...args)
{
if constexpr (QtJniTypes::isObjectType<Ret>()) {
return callStaticObjectMethod(clazz, methodId, std::forward<Args>(args)...);
} else {
QtJniTypes::assertPrimitiveType<Ret>();
QJniEnvironment env;
if (clazz && methodId) {
@ -140,20 +140,19 @@ public:
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,8 +182,11 @@ 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
{
if constexpr (QtJniTypes::isObjectType<T>()) {
return getObjectField<T>(fieldName);
} else {
QtJniTypes::assertPrimitiveType<T>();
QJniEnvironment env;
T res{};
@ -197,15 +199,20 @@ public:
}
return res;
}
}
template <typename T>
static T getStaticField(const char *className, const char *fieldName)
static auto getStaticField(const char *className, const char *fieldName)
{
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 0;
return res;
constexpr auto signature = QtJniTypes::fieldSignature<T>();
jfieldID id = getCachedFieldID(env.jniEnv(), clazz,
@ -213,18 +220,21 @@ public:
fieldName,
signature, true);
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)
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{};
@ -237,6 +247,7 @@ public:
}
return res;
}
}
template <typename T>
QJniObject getObjectField(const char *fieldName) const

View File

@ -201,6 +201,17 @@ void tst_QJniObject::callMethodTest()
jlong ret = longObject.callMethod<jlong>("longValue");
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()
@ -316,6 +327,17 @@ void tst_QJniObject::callStaticObjectMethod()
returnedString = returnValue.toString();
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()
@ -338,6 +360,15 @@ void tst_QJniObject::callStaticObjectMethodById()
QString returnedString = returnValue.toString();
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()