summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAssam Boudjelthia <assam.boudjelthia@qt.io>2021-05-07 12:05:34 +0300
committerAssam Boudjelthia <assam.boudjelthia@qt.io>2021-05-08 15:32:39 +0000
commit8221b6b8c1594e9753bf60b658cc50cac2dedaa9 (patch)
tree71ad99e9f66211236bfb3756c0811f3a9d05e326
parent3d430935cf85c0fd39804da32870b15cf3e3ba42 (diff)
Add objectClass() and className() for QJniObject
This can be handy sometimes to avoid doing a env->GetObjectClass() call to get the jclass object. Change-Id: I015fe5ed73304338826e468e59778bcd3ceadc3b Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/corelib/kernel/qjnienvironment.cpp7
-rw-r--r--src/corelib/kernel/qjniobject.cpp36
-rw-r--r--src/corelib/kernel/qjniobject.h3
3 files changed, 37 insertions, 9 deletions
diff --git a/src/corelib/kernel/qjnienvironment.cpp b/src/corelib/kernel/qjnienvironment.cpp
index 5aee05bce6..8c1f19ce82 100644
--- a/src/corelib/kernel/qjnienvironment.cpp
+++ b/src/corelib/kernel/qjnienvironment.cpp
@@ -286,12 +286,7 @@ bool QJniEnvironment::registerNativeMethods(const char *className, JNINativeMeth
if (!classObject.isValid())
return false;
-
- jclass clazz = d->jniEnv->GetObjectClass(classObject.object());
- const bool result = registerNativeMethods(clazz, methods, size);
- d->jniEnv->DeleteLocalRef(clazz);
-
- return result;
+ return registerNativeMethods(classObject.objectClass(), methods, size);
}
/*!
diff --git a/src/corelib/kernel/qjniobject.cpp b/src/corelib/kernel/qjniobject.cpp
index c443746ff4..dfb0dbd36d 100644
--- a/src/corelib/kernel/qjniobject.cpp
+++ b/src/corelib/kernel/qjniobject.cpp
@@ -779,10 +779,10 @@ QJniObject::~QJniObject()
jstring jstring = string.object<jstring>();
\endcode
- \note The returned object is still kept live by this QJniObject. To keep the
- object live beyond the lifetime of this QJniObject, for example to record it
+ \note The returned object is still kept alive by this QJniObject. To keep the
+ object alive beyond the lifetime of this QJniObject, for example to record it
for later use, the easiest approach is to store it in another QJniObject with
- a suitable lifetime. Alternatively, you can make a new global reference to the
+ a suitable lifetime. Alternatively, you may create a new global reference to the
object and store it, taking care to free it when you are done with it.
\snippet jni/src_qjniobject.cpp QJniObject scope
@@ -792,6 +792,36 @@ jobject QJniObject::object() const
return javaObject();
}
+/*!
+ \fn jclass QJniObject::objectClass() const
+
+ Returns the class object held by the QJniObject as a \c jclass.
+
+ \note The returned object is still kept alive by this QJniObject. To keep the
+ object alive beyond the lifetime of this QJniObject, for example to record it
+ for later use, the easiest approach is to store it in another QJniObject with
+ a suitable lifetime. Alternatively, you may create a new global reference to the
+ object and store it, taking care to free it when you are done with it.
+
+ \since 6.2
+*/
+jclass QJniObject::objectClass() const
+{
+ return d->m_jclass;
+}
+
+/*!
+ \fn QByteArray QJniObject::className() const
+
+ Returns the name of the class object held by the QJniObject as a \c QByteArray.
+
+ \since 6.2
+*/
+QByteArray QJniObject::className() const
+{
+ return d->m_className;
+}
+
QJniObject QJniObject::callObjectMethodV(const char *methodName,
const char *signature,
va_list args) const
diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h
index e7bf4f3007..d9e11c4934 100644
--- a/src/corelib/kernel/qjniobject.h
+++ b/src/corelib/kernel/qjniobject.h
@@ -68,6 +68,9 @@ public:
return static_cast<T>(javaObject());
}
+ jclass objectClass() const;
+ QByteArray className() const;
+
template <typename T>
T callMethod(const char *methodName, const char *signature, ...) const
{