1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

JNIHelpers: Add a mechanism for loading optional JNI classes

This commit is contained in:
reuk 2025-04-08 20:06:33 +01:00
parent 3d8a97c1c1
commit 2e5ecceea8
No known key found for this signature in database
2 changed files with 19 additions and 3 deletions

View file

@ -377,7 +377,6 @@ void JNIClassBase::initialise (JNIEnv* env, jobject context)
if (classRef == nullptr)
classRef = GlobalRefImpl { LocalRef { env->FindClass (classPath) } };
jassert (classRef != nullptr);
initialiseFields (env);
}
}

View file

@ -246,7 +246,8 @@ public:
JNIClassBase (const char* classPath, int minSDK, const uint8* byteCode, size_t byteCodeSize);
virtual ~JNIClassBase();
operator jclass() const noexcept { return classRef; }
jclass getJclass() const { return classRef; }
operator jclass() const noexcept { return getJclass(); }
static void initialiseAllClasses (JNIEnv*, jobject context);
static void releaseAllClasses (JNIEnv*);
@ -293,7 +294,7 @@ template <typename T, size_t N> constexpr auto numBytes (const T (&) [N]) { retu
#define DECLARE_JNI_FIELD(fieldID, stringName, signature) jfieldID fieldID;
#define DECLARE_JNI_CALLBACK(fieldID, stringName, signature)
#define DECLARE_JNI_CLASS_WITH_BYTECODE(CppClassName, javaPath, minSDK, byteCodeData) \
#define DECLARE_OPTIONAL_JNI_CLASS_WITH_BYTECODE(CppClassName, javaPath, minSDK, byteCodeData, allowFailure) \
static_assert (minSDK >= 24, "There's no need to supply a min SDK lower than JUCE's minimum requirement"); \
class CppClassName ## _Class : public JNIClassBase \
{ \
@ -302,6 +303,19 @@ template <typename T, size_t N> constexpr auto numBytes (const T (&) [N]) { retu
\
void initialiseFields (JNIEnv* env) \
{ \
if constexpr (allowFailure) \
{ \
if (getJclass() == nullptr) \
{ \
env->ExceptionClear(); \
return; \
} \
} \
else \
{ \
jassert (getJclass() != nullptr); \
} \
\
Array<JNINativeMethod> callbacks; \
JNI_CLASS_MEMBERS (CREATE_JNI_METHOD, CREATE_JNI_STATICMETHOD, CREATE_JNI_FIELD, CREATE_JNI_STATICFIELD, CREATE_JNI_CALLBACK); \
resolveCallbacks (env, callbacks); \
@ -311,6 +325,9 @@ template <typename T, size_t N> constexpr auto numBytes (const T (&) [N]) { retu
}; \
static inline const CppClassName ## _Class CppClassName;
#define DECLARE_JNI_CLASS_WITH_BYTECODE(CppClassName, javaPath, minSDK, byteCodeData) \
DECLARE_OPTIONAL_JNI_CLASS_WITH_BYTECODE (CppClassName, javaPath, minSDK, byteCodeData, false) \
//==============================================================================
#define DECLARE_JNI_CLASS_WITH_MIN_SDK(CppClassName, javaPath, minSDK) \
DECLARE_JNI_CLASS_WITH_BYTECODE (CppClassName, javaPath, minSDK, nullptr)