From 0666975d6c276139788001cdd4cb686c7073f50c Mon Sep 17 00:00:00 2001 From: Yoann Lopes Date: Fri, 1 Mar 2013 18:32:49 +0100 Subject: Added JNI convenience classes in QtPlatformSupport. This is used for Android. Change-Id: I049138c140a472b1721390cf4ec2bd88bbe9c471 Reviewed-by: Eskil Abrahamsen Blomfeldt Reviewed-by: Oswald Buddenhagen Reviewed-by: Paul Olav Tvete --- .../jniconvenience/jniconvenience.pri | 9 + src/platformsupport/jniconvenience/qjnihelpers.cpp | 115 + src/platformsupport/jniconvenience/qjnihelpers_p.h | 191 ++ src/platformsupport/jniconvenience/qjniobject.cpp | 3098 ++++++++++++++++++++ src/platformsupport/jniconvenience/qjniobject_p.h | 169 ++ src/platformsupport/platformsupport.pro | 1 + 6 files changed, 3583 insertions(+) create mode 100644 src/platformsupport/jniconvenience/jniconvenience.pri create mode 100644 src/platformsupport/jniconvenience/qjnihelpers.cpp create mode 100644 src/platformsupport/jniconvenience/qjnihelpers_p.h create mode 100644 src/platformsupport/jniconvenience/qjniobject.cpp create mode 100644 src/platformsupport/jniconvenience/qjniobject_p.h diff --git a/src/platformsupport/jniconvenience/jniconvenience.pri b/src/platformsupport/jniconvenience/jniconvenience.pri new file mode 100644 index 0000000000..991518e370 --- /dev/null +++ b/src/platformsupport/jniconvenience/jniconvenience.pri @@ -0,0 +1,9 @@ +android { + QT += gui-private + + HEADERS += $$PWD/qjnihelpers_p.h \ + $$PWD/qjniobject_p.h + + SOURCES += $$PWD/qjnihelpers.cpp \ + $$PWD/qjniobject.cpp +} diff --git a/src/platformsupport/jniconvenience/qjnihelpers.cpp b/src/platformsupport/jniconvenience/qjnihelpers.cpp new file mode 100644 index 0000000000..3f9b568368 --- /dev/null +++ b/src/platformsupport/jniconvenience/qjnihelpers.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qjnihelpers_p.h" + +#include +#include + +QT_BEGIN_NAMESPACE + +QString qt_convertJString(jstring string) +{ + QAttachedJNIEnv env; + int strLength = env->GetStringLength(string); + QString res; + res.resize(strLength); + env->GetStringRegion(string, 0, strLength, (jchar*)res.utf16()); + return res; +} + +QJNILocalRef qt_toJString(const QString &string) +{ + QAttachedJNIEnv env; + return QJNILocalRef(env->NewString(reinterpret_cast(string.constData()), + string.length())); +} + + +static JavaVM *g_javaVM = 0; + +static JavaVM *getJavaVM() +{ + if (!g_javaVM){ + QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); + g_javaVM = static_cast(nativeInterface->nativeResourceForIntegration("JavaVM")); + } + return g_javaVM; +} + +QThreadStorage QAttachedJNIEnv::m_refCount; + +QAttachedJNIEnv::QAttachedJNIEnv() +{ + JavaVM *vm = javaVM(); + if (vm->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) == JNI_EDETACHED) { + if (vm->AttachCurrentThread(&jniEnv, 0) < 0) { + jniEnv = 0; + return; + } + } + + if (!m_refCount.hasLocalData()) + m_refCount.setLocalData(1); + else + m_refCount.setLocalData(m_refCount.localData() + 1); +} + +QAttachedJNIEnv::~QAttachedJNIEnv() +{ + if (!jniEnv) + return; + + int newRef = m_refCount.localData() - 1; + m_refCount.setLocalData(newRef); + + if (newRef == 0) + javaVM()->DetachCurrentThread(); + + jniEnv = 0; +} + +JavaVM *QAttachedJNIEnv::javaVM() +{ + return getJavaVM(); +} + +QT_END_NAMESPACE diff --git a/src/platformsupport/jniconvenience/qjnihelpers_p.h b/src/platformsupport/jniconvenience/qjnihelpers_p.h new file mode 100644 index 0000000000..fb44d156bd --- /dev/null +++ b/src/platformsupport/jniconvenience/qjnihelpers_p.h @@ -0,0 +1,191 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QJNIHELPERS_H +#define QJNIHELPERS_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +template +class QJNILocalRef; + +QString qt_convertJString(jstring string); +QJNILocalRef qt_toJString(const QString &string); + + +struct QAttachedJNIEnv +{ + QAttachedJNIEnv(); + ~QAttachedJNIEnv(); + + static JavaVM *javaVM(); + + JNIEnv *operator->() + { + return jniEnv; + } + + operator JNIEnv*() const + { + return jniEnv; + } + + JNIEnv *jniEnv; + +private: + static QThreadStorage m_refCount; +}; + + +template +class QJNILocalRef +{ +public: + inline QJNILocalRef() : m_obj(0) { } + inline explicit QJNILocalRef(T o) : m_obj(o) { } + inline QJNILocalRef(const QJNILocalRef &other) : m_obj(other.m_obj) + { + if (other.m_obj) + m_obj = static_cast(m_env->NewLocalRef(other.m_obj)); + } + + template + inline QJNILocalRef(const QJNILocalRef &other) : m_obj(other.m_obj) + { + if (other.m_obj) + m_obj = static_cast(m_env->NewLocalRef(other.m_obj)); + } + + inline ~QJNILocalRef() { release(); } + + inline QJNILocalRef &operator=(const QJNILocalRef &other) + { + release(); + m_obj = other.m_obj; // for type checking + if (other.m_obj) + m_obj = static_cast(m_env->NewLocalRef(other.m_obj)); + return *this; + } + + template + inline QJNILocalRef &operator=(const QJNILocalRef &other) + { + release(); + m_obj = other.m_obj; // for type checking + if (other.m_obj) + m_obj = static_cast(m_env->NewLocalRef(other.m_obj)); + return *this; + } + + inline QJNILocalRef &operator=(T o) + { + release(); + m_obj = o; + return *this; + } + + template + inline QJNILocalRef &operator=(X o) + { + release(); + m_obj = o; + return *this; + } + + inline bool operator !() const { return !m_obj; } + inline bool isNull() const { return !m_obj; } + inline T object() const { return m_obj; } + +private: + void release() + { + if (m_obj) { + m_env->DeleteLocalRef(m_obj); + m_obj = 0; + } + } + + QAttachedJNIEnv m_env; + T m_obj; + + template friend class QJNILocalRef; +}; + +template +bool operator==(const QJNILocalRef &ptr1, const QJNILocalRef &ptr2) +{ + return ptr1.m_obj == ptr2.m_obj; +} +template +bool operator!=(const QJNILocalRef &ptr1, const QJNILocalRef &ptr2) +{ + return ptr1.m_obj != ptr2.m_obj; +} + +template +bool operator==(const QJNILocalRef &ptr1, X ptr2) +{ + return ptr1.m_obj == ptr2; +} +template +bool operator==(T ptr1, const QJNILocalRef &ptr2) +{ + return ptr1 == ptr2.m_obj; +} +template +bool operator!=(const QJNILocalRef &ptr1, X ptr2) +{ + return !(ptr1 == ptr2); +} +template +bool operator!=(const T *ptr1, const QJNILocalRef &ptr2) +{ + return !(ptr2 == ptr1); +} + +QT_END_NAMESPACE + +#endif // QJNIHELPERS_H diff --git a/src/platformsupport/jniconvenience/qjniobject.cpp b/src/platformsupport/jniconvenience/qjniobject.cpp new file mode 100644 index 0000000000..515e82b2f0 --- /dev/null +++ b/src/platformsupport/jniconvenience/qjniobject.cpp @@ -0,0 +1,3098 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qjniobject_p.h" + +#include "qjnihelpers_p.h" +#include + +QT_BEGIN_NAMESPACE + +static QHash g_cachedClasses; + +static jclass getCachedClass(JNIEnv *env, const char *className) +{ + jclass clazz = 0; + QString key = QLatin1String(className); + QHash::iterator it = g_cachedClasses.find(key); + if (it == g_cachedClasses.end()) { + jclass c = env->FindClass(className); + if (env->ExceptionCheck()) { + c = 0; + env->ExceptionClear(); + } + if (c) + clazz = static_cast(env->NewGlobalRef(c)); + g_cachedClasses.insert(key, clazz); + } else { + clazz = it.value(); + } + return clazz; +} + +static QHash g_cachedMethodIDs; +static QString g_keyBase(QLatin1String("%1%2%3")); + +static jmethodID getCachedMethodID(JNIEnv *env, + jclass clazz, + const char *name, + const char *sig, + bool isStatic = false) +{ + jmethodID id = 0; + QString key = g_keyBase.arg(size_t(clazz)).arg(QLatin1String(name)).arg(QLatin1String(sig)); + QHash::iterator it = g_cachedMethodIDs.find(key); + if (it == g_cachedMethodIDs.end()) { + if (isStatic) + id = env->GetStaticMethodID(clazz, name, sig); + else + id = env->GetMethodID(clazz, name, sig); + + if (env->ExceptionCheck()) { + id = 0; + env->ExceptionClear(); + } + + g_cachedMethodIDs.insert(key, id); + } else { + id = it.value(); + } + return id; +} + +static QHash g_cachedFieldIDs; + +static jfieldID getCachedFieldID(JNIEnv *env, + jclass clazz, + const char *name, + const char *sig, + bool isStatic = false) +{ + jfieldID id = 0; + QString key = g_keyBase.arg(size_t(clazz)).arg(QLatin1String(name)).arg(QLatin1String(sig)); + QHash::iterator it = g_cachedFieldIDs.find(key); + if (it == g_cachedFieldIDs.end()) { + if (isStatic) + id = env->GetStaticFieldID(clazz, name, sig); + else + id = env->GetFieldID(clazz, name, sig); + + if (env->ExceptionCheck()) { + id = 0; + env->ExceptionClear(); + } + + g_cachedFieldIDs.insert(key, id); + } else { + id = it.value(); + } + return id; +} + +QJNIObject::QJNIObject(const char *className) + : m_jobject(0) + , m_jclass(0) + , m_own_jclass(false) +{ + QAttachedJNIEnv env; + m_jclass = getCachedClass(env, className); + if (m_jclass) { + // get default constructor + jmethodID constructorId = getCachedMethodID(env, m_jclass, "", "()V"); + if (constructorId) { + jobject obj = env->NewObject(m_jclass, constructorId); + if (obj) { + m_jobject = env->NewGlobalRef(obj); + env->DeleteLocalRef(obj); + } + } + } +} + +QJNIObject::QJNIObject(const char *className, const char *sig, ...) + : m_jobject(0) + , m_jclass(0) + , m_own_jclass(false) +{ + QAttachedJNIEnv env; + m_jclass = getCachedClass(env, className); + if (m_jclass) { + jmethodID constructorId = getCachedMethodID(env, m_jclass, "", sig); + if (constructorId) { + va_list args; + va_start(args, sig); + jobject obj = env->NewObjectV(m_jclass, constructorId, args); + va_end(args); + if (obj) { + m_jobject = env->NewGlobalRef(obj); + env->DeleteLocalRef(obj); + } + } + } +} + +QJNIObject::QJNIObject(jclass clazz) + : m_jobject(0) + , m_jclass(0) + , m_own_jclass(true) +{ + QAttachedJNIEnv env; + m_jclass = static_cast(env->NewGlobalRef(clazz)); + if (m_jclass) { + // get default constructor + jmethodID constructorId = getCachedMethodID(env, m_jclass, "", "()V"); + if (constructorId) { + jobject obj = env->NewObject(m_jclass, constructorId); + if (obj) { + m_jobject = env->NewGlobalRef(obj); + env->DeleteLocalRef(obj); + } + } + } +} + +QJNIObject::QJNIObject(jclass clazz, const char *sig, ...) + : m_jobject(0) + , m_jclass(0) + , m_own_jclass(true) +{ + QAttachedJNIEnv env; + if (clazz) { + m_jclass = static_cast(env->NewGlobalRef(clazz)); + if (m_jclass) { + jmethodID constructorId = getCachedMethodID(env, m_jclass, "", sig); + if (constructorId) { + va_list args; + va_start(args, sig); + jobject obj = env->NewObjectV(m_jclass, constructorId, args); + va_end(args); + if (obj) { + m_jobject = env->NewGlobalRef(obj); + env->DeleteLocalRef(obj); + } + } + } + } +} + +QJNIObject::QJNIObject(jobject obj) + : m_jobject(0) + , m_jclass(0) + , m_own_jclass(true) +{ + QAttachedJNIEnv env; + m_jobject = env->NewGlobalRef(obj); + m_jclass = static_cast(env->NewGlobalRef(env->GetObjectClass(m_jobject))); +} + +QJNIObject::~QJNIObject() +{ + QAttachedJNIEnv env; + if (m_jobject) + env->DeleteGlobalRef(m_jobject); + if (m_jclass && m_own_jclass) + env->DeleteGlobalRef(m_jclass); +} + +bool QJNIObject::isClassAvailable(const char *className) +{ + QAttachedJNIEnv env; + + if (!env.jniEnv) + return false; + + jclass clazz = getCachedClass(env, className); + + return (clazz != 0); +} + +template <> +void QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + env->CallVoidMethodV(m_jobject, id, args); + va_end(args); + } +} + +template <> +jboolean QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jboolean res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallBooleanMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jbyte QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jbyte res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallByteMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jchar QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jchar res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallCharMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jshort QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jshort res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallShortMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jint QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jint res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallIntMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jlong QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jlong res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallLongMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jfloat QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jfloat res = 0.f; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallFloatMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +jdouble QJNIObject::callMethod(const char *methodName, const char *sig, ...) +{ + QAttachedJNIEnv env; + jdouble res = 0.; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallDoubleMethodV(m_jobject, id, args); + va_end(args); + } + return res; +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jobject res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallObjectMethodV(m_jobject, id, args); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jstring res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jobjectArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jbooleanArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jbyteArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jcharArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jshortArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jintArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jlongArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jfloatArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jdoubleArray res = 0; + jmethodID id = getCachedMethodID(env, m_jclass, methodName, sig); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallObjectMethodV(m_jobject, id, args)); + va_end(args); + } + return QJNILocalRef(res); +} + +template <> +void QJNIObject::callMethod(const char *methodName) +{ + callMethod(methodName, "()V"); +} + +template <> +jboolean QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()Z"); +} + +template <> +jbyte QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()B"); +} + +template <> +jchar QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()C"); +} + +template <> +jshort QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()S"); +} + +template <> +jint QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()I"); +} + +template <> +jlong QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()J"); +} + +template <> +jfloat QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()F"); +} + +template <> +jdouble QJNIObject::callMethod(const char *methodName) +{ + return callMethod(methodName, "()D"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()Ljava/lang/String;"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[Z"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[B"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[S"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[I"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[J"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[F"); +} + +template <> +QJNILocalRef QJNIObject::callObjectMethod(const char *methodName) +{ + return callObjectMethod(methodName, "()[D"); +} + +template <> +void QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + env->CallStaticVoidMethodV(clazz, id, args); + va_end(args); + } + } +} + +template <> +void QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + env->CallStaticVoidMethodV(clazz, id, args); + va_end(args); + } +} + +template <> +jboolean QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jboolean res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticBooleanMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jboolean QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jboolean res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticBooleanMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +jbyte QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jbyte res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticByteMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jbyte QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jbyte res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticByteMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +jchar QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jchar res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticCharMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jchar QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jchar res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticCharMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + + +template <> +jshort QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jshort res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticShortMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jshort QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jshort res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticShortMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +jint QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jint res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticIntMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jint QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jint res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticIntMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +jlong QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jlong res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticLongMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jlong QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jlong res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticLongMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +jfloat QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jfloat res = 0.f; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticFloatMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jfloat QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jfloat res = 0.f; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticFloatMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +jdouble QJNIObject::callStaticMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jdouble res = 0.; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticDoubleMethodV(clazz, id, args); + va_end(args); + } + } + + return res; +} + +template <> +jdouble QJNIObject::callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jdouble res = 0.; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticDoubleMethodV(clazz, id, args); + va_end(args); + } + + return res; +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jobject res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticObjectMethodV(clazz, id, args); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jobject res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = env->CallStaticObjectMethodV(clazz, id, args); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jstring res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jstring res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jobjectArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jobjectArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jbooleanArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jbooleanArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jbyteArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jbyteArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jcharArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jcharArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jshortArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jshortArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jintArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jintArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jlongArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jlongArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jfloatArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jfloatArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jdoubleArray res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) { + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + } + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, + ...) +{ + QAttachedJNIEnv env; + + jdoubleArray res = 0; + + jmethodID id = getCachedMethodID(env, clazz, methodName, sig, true); + if (id) { + va_list args; + va_start(args, sig); + res = static_cast(env->CallStaticObjectMethodV(clazz, id, args)); + va_end(args); + } + + return QJNILocalRef(res); +} + +template <> +void QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + callStaticMethod(className, methodName, "()V"); +} + +template <> +void QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + callStaticMethod(clazz, methodName, "()V"); +} + +template <> +jboolean QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()Z"); +} + +template <> +jboolean QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()Z"); +} + +template <> +jbyte QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()B"); +} + +template <> +jbyte QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()B"); +} + +template <> +jchar QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()C"); +} + +template <> +jchar QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()C"); +} + +template <> +jshort QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()S"); +} + +template <> +jshort QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()S"); +} + +template <> +jint QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()I"); +} + +template <> +jint QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()I"); +} + +template <> +jlong QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()J"); +} + +template <> +jlong QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()J"); +} + +template <> +jfloat QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()F"); +} + +template <> +jfloat QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()F"); +} + +template <> +jdouble QJNIObject::callStaticMethod(const char *className, const char *methodName) +{ + return callStaticMethod(className, methodName, "()D"); +} + +template <> +jdouble QJNIObject::callStaticMethod(jclass clazz, const char *methodName) +{ + return callStaticMethod(clazz, methodName, "()D"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()Ljava/lang/String;"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()Ljava/lang/String;"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[Z"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[Z"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[B"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[B"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[C"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[C"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[S"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[S"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[I"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[I"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[J"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[J"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[F"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[F"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(const char *className, + const char *methodName) +{ + return callStaticObjectMethod(className, methodName, "()[D"); +} + +template <> +QJNILocalRef QJNIObject::callStaticObjectMethod(jclass clazz, + const char *methodName) +{ + return callStaticObjectMethod(clazz, methodName, "()[D"); +} + +template <> +jboolean QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jboolean res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "Z"); + if (id) + res = env->GetBooleanField(m_jobject, id); + + return res; +} + +template <> +jbyte QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jbyte res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "B"); + if (id) + res = env->GetByteField(m_jobject, id); + + return res; +} + +template <> +jchar QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jchar res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "C"); + if (id) + res = env->GetCharField(m_jobject, id); + + return res; +} + +template <> +jshort QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jshort res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "S"); + if (id) + res = env->GetShortField(m_jobject, id); + + return res; +} + +template <> +jint QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jint res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "I"); + if (id) + res = env->GetIntField(m_jobject, id); + + return res; +} + +template <> +jlong QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jlong res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "J"); + if (id) + res = env->GetLongField(m_jobject, id); + + return res; +} + +template <> +jfloat QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jfloat res = 0.f; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "F"); + if (id) + res = env->GetFloatField(m_jobject, id); + + return res; +} + +template <> +jdouble QJNIObject::getField(const char *fieldName) +{ + QAttachedJNIEnv env; + jdouble res = 0.; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "D"); + if (id) + res = env->GetDoubleField(m_jobject, id); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName, const char *sig) +{ + QAttachedJNIEnv env; + jobject res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, sig); + if (id) + res = env->GetObjectField(m_jobject, id); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jbooleanArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[Z"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jbyteArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[B"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jcharArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[C"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jshortArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[S"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jintArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[I"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jlongArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[J"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jfloatArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[F"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jdoubleArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[D"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName) +{ + QAttachedJNIEnv env; + jstring res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "Ljava/lang/String;"); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getObjectField(const char *fieldName, + const char *sig) +{ + QAttachedJNIEnv env; + jobjectArray res = 0; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, sig); + if (id) + res = static_cast(env->GetObjectField(m_jobject, id)); + + return QJNILocalRef(res); +} + +template <> +void QJNIObject::setField(const char *fieldName, jboolean value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "Z"); + if (id) + env->SetBooleanField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jbyte value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "B"); + if (id) + env->SetByteField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jchar value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "C"); + if (id) + env->SetCharField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jshort value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "S"); + if (id) + env->SetShortField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jint value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "I"); + if (id) + env->SetIntField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jlong value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "J"); + if (id) + env->SetLongField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jfloat value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "F"); + if (id) + env->SetFloatField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jdouble value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "D"); + if (id) + env->SetDoubleField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jbooleanArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[Z"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jbyteArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[B"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jcharArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[C"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jshortArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[S"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jintArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[I"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jlongArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[J"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jfloatArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[F"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jdoubleArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "[D"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, jstring value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, "Ljava/lang/String;"); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, const char *sig, jobject value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, sig); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +void QJNIObject::setField(const char *fieldName, + const char *sig, + jobjectArray value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, m_jclass, fieldName, sig); + if (id) + env->SetObjectField(m_jobject, id, value); + +} + +template <> +jboolean QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jboolean res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "Z", true); + if (id) + res = env->GetStaticBooleanField(clazz, id); + + return res; +} + +template <> +jboolean QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jboolean res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jbyte QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jbyte res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "B", true); + if (id) + res = env->GetStaticByteField(clazz, id); + + return res; +} + +template <> +jbyte QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jbyte res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jchar QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jchar res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "C", true); + if (id) + res = env->GetStaticCharField(clazz, id); + + return res; +} + +template <> +jchar QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jchar res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jshort QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jshort res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "S", true); + if (id) + res = env->GetStaticShortField(clazz, id); + + return res; +} + +template <> +jshort QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jshort res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jint QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jint res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "I", true); + if (id) + res = env->GetStaticIntField(clazz, id); + + return res; +} + +template <> +jint QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jint res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jlong QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jlong res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "J", true); + if (id) + res = env->GetStaticLongField(clazz, id); + + return res; +} + +template <> +jlong QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jlong res = 0; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jfloat QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jfloat res = 0.f; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "F", true); + if (id) + res = env->GetStaticFloatField(clazz, id); + + return res; +} + +template <> +jfloat QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jfloat res = 0.f; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +jdouble QJNIObject::getStaticField(jclass clazz, const char *fieldName) +{ + QAttachedJNIEnv env; + + jdouble res = 0.; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "D", true); + if (id) + res = env->GetStaticDoubleField(clazz, id); + + return res; +} + +template <> +jdouble QJNIObject::getStaticField(const char *className, const char *fieldName) +{ + QAttachedJNIEnv env; + + jdouble res = 0.; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName, + const char *sig) +{ + QAttachedJNIEnv env; + + jobject res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, sig, true); + if (id) + res = env->GetStaticObjectField(clazz, id); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName, + const char *sig) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName, sig); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jstring res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "Ljava/lang/String;", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jbooleanArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[Z", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jbyteArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[B", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jcharArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[C", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jshortArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[S", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jintArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[I", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jlongArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[J", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jfloatArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[F", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName) +{ + QAttachedJNIEnv env; + + jdoubleArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, "[D", true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName); + + return res; +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(jclass clazz, + const char *fieldName, + const char *sig) +{ + QAttachedJNIEnv env; + + jobjectArray res = 0; + + jfieldID id = getCachedFieldID(env, clazz, fieldName, sig, true); + if (id) + res = static_cast(env->GetStaticObjectField(clazz, id)); + + return QJNILocalRef(res); +} + +template <> +QJNILocalRef QJNIObject::getStaticObjectField(const char *className, + const char *fieldName, + const char *sig) +{ + QAttachedJNIEnv env; + + QJNILocalRef res; + + jclass clazz = getCachedClass(env, className); + if (clazz) + res = getStaticObjectField(clazz, fieldName, sig); + + return res; +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jboolean value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "Z", true); + if (id) + env->SetStaticBooleanField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jboolean value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jbyte value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "B", true); + if (id) + env->SetStaticByteField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jbyte value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jchar value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "C", true); + if (id) + env->SetStaticCharField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jchar value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jshort value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "S", true); + if (id) + env->SetStaticShortField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jshort value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jint value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "I", true); + if (id) + env->SetStaticIntField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, const char *fieldName, jint value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jlong value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "J", true); + if (id) + env->SetStaticLongField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jlong value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jfloat value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "F", true); + if (id) + env->SetStaticFloatField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jfloat value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jdouble value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, "D", true); + if (id) + env->SetStaticDoubleField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jdouble value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + const char *sig, + jobject value) +{ + QAttachedJNIEnv env; + jfieldID id = getCachedFieldID(env, clazz, fieldName, sig, true); + if (id) + env->SetStaticObjectField(clazz, id, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + const char *sig, + jobject value) +{ + QAttachedJNIEnv env; + jclass clazz = getCachedClass(env, className); + if (clazz) + setStaticField(clazz, fieldName, sig, value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jstring value) +{ + setStaticField(className, fieldName, "Ljava/lang/String;", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, const char *fieldName, jstring value) +{ + setStaticField(clazz, fieldName, "Ljava/lang/String;", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jbooleanArray value) +{ + setStaticField(className, fieldName, "[Z", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jbooleanArray value) +{ + setStaticField(clazz, fieldName, "[Z", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jbyteArray value) +{ + setStaticField(className, fieldName, "[B", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jbyteArray value) +{ + setStaticField(clazz, fieldName, "[B", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jcharArray value) +{ + setStaticField(className, fieldName, "[C", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jcharArray value) +{ + setStaticField(clazz, fieldName, "[C", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jshortArray value) +{ + setStaticField(className, fieldName, "[S", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jshortArray value) +{ + setStaticField(clazz, fieldName, "[S", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jintArray value) +{ + setStaticField(className, fieldName, "[I", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jintArray value) +{ + setStaticField(clazz, fieldName, "[I", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jlongArray value) +{ + setStaticField(className, fieldName, "[J", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jlongArray value) +{ + setStaticField(clazz, fieldName, "[J", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jfloatArray value) +{ + setStaticField(className, fieldName, "[F", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jfloatArray value) +{ + setStaticField(clazz, fieldName, "[F", value); +} + +template <> +void QJNIObject::setStaticField(const char *className, + const char *fieldName, + jdoubleArray value) +{ + setStaticField(className, fieldName, "[D", value); +} + +template <> +void QJNIObject::setStaticField(jclass clazz, + const char *fieldName, + jdoubleArray value) +{ + setStaticField(clazz, fieldName, "[D", value); +} + + +QT_END_NAMESPACE diff --git a/src/platformsupport/jniconvenience/qjniobject_p.h b/src/platformsupport/jniconvenience/qjniobject_p.h new file mode 100644 index 0000000000..6874765f06 --- /dev/null +++ b/src/platformsupport/jniconvenience/qjniobject_p.h @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QJNIOBJECT_H +#define QJNIOBJECT_H + +#include +#include + +QT_BEGIN_NAMESPACE + +template +class QJNILocalRef; + +/** + * Allows to wrap any Java class and partially hide some of the jni calls. + * + * Usage example: + * + * QJNIObject javaString("java/lang/String"); + * jchar char = javaString.callMethod("charAt", "(I)C", 0); + * + * ---- + * + * jstring string = QJNIObject::callStaticMethod("java/lang/String", + * "valueOf", + * "(I)Ljava/lang/String;", 2); + * + * ---- + * + * // Constructor with argument + * jstring someString; + * QJNIObject someObject("java/some/Class", "(Ljava/lang/String;)V", someString); + * someObject.setField("fieldName", 10); + * someObject.callMethod("doStuff"); + */ +class QJNIObject +{ +public: + QJNIObject(const char *className); + QJNIObject(const char *className, const char *sig, ...); + QJNIObject(jclass clazz); + QJNIObject(jclass clazz, const char *sig, ...); + QJNIObject(jobject obj); + virtual ~QJNIObject(); + + static bool isClassAvailable(const char *className); + + bool isValid() const { return m_jobject != 0; } + jobject object() const { return m_jobject; } + + template + T callMethod(const char *methodName); + template + T callMethod(const char *methodName, const char *sig, ...); + template + QJNILocalRef callObjectMethod(const char *methodName); + template + QJNILocalRef callObjectMethod(const char *methodName, const char *sig, ...); + + template + static T callStaticMethod(const char *className, const char *methodName); + template + static T callStaticMethod(const char *className, const char *methodName, const char *sig, ...); + template + static QJNILocalRef callStaticObjectMethod(const char *className, const char *methodName); + template + static QJNILocalRef callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, ...); + template + static T callStaticMethod(jclass clazz, const char *methodName); + template + static T callStaticMethod(jclass clazz, const char *methodName, const char *sig, ...); + template + static QJNILocalRef callStaticObjectMethod(jclass clazz, const char *methodName); + template + static QJNILocalRef callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, ...); + + template + T getField(const char *fieldName); + template + T getField(const char *fieldName, const char *sig); + template + QJNILocalRef getObjectField(const char *fieldName); + template + QJNILocalRef getObjectField(const char *fieldName, const char *sig); + + template + void setField(const char *fieldName, T value); + template + void setField(const char *fieldName, const char *sig, T value); + + template + static QJNILocalRef getStaticObjectField(const char *className, const char *fieldName); + template + static QJNILocalRef getStaticObjectField(const char *className, + const char *fieldName, + const char *sig); + template + static T getStaticField(const char *className, const char *fieldName); + template + static QJNILocalRef getStaticObjectField(jclass clazz, const char *fieldName); + template + static QJNILocalRef getStaticObjectField(jclass clazz, const char *fieldName, const char *sig); + template + static T getStaticField(jclass clazz, const char *fieldName); + + template + static void setStaticField(const char *className, + const char *fieldName, + const char *sig, + T value); + template + static void setStaticField(const char *className, const char *fieldName, T value); + template + static void setStaticField(jclass clazz, const char *fieldName, const char *sig, T value); + template + static void setStaticField(jclass clazz, const char *fieldName, T value); + +protected: + jobject m_jobject; + jclass m_jclass; + bool m_own_jclass; +}; + +QT_END_NAMESPACE + +#endif // QJNIOBJECT_H diff --git a/src/platformsupport/platformsupport.pro b/src/platformsupport/platformsupport.pro index 8e0f396993..0566e9d3ec 100644 --- a/src/platformsupport/platformsupport.pro +++ b/src/platformsupport/platformsupport.pro @@ -22,3 +22,4 @@ include(devicediscovery/devicediscovery.pri) include(services/services.pri) include(themes/themes.pri) include(linuxaccessibility/linuxaccessibility.pri) +include(jniconvenience/jniconvenience.pri) -- cgit v1.2.3