1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #102862 from dsnopek/java-class-wrapper-get-exception

JavaClassWrapper: Allow handling exceptions (rather than just crashing)
This commit is contained in:
Rémi Verschelde
2025-02-14 22:58:31 +01:00
4 changed files with 29 additions and 0 deletions

View File

@@ -15,10 +15,18 @@
print(datetime.format(formatter)) print(datetime.format(formatter))
[/codeblock] [/codeblock]
[b]Warning:[/b] When calling Java methods, be sure to check [method JavaClassWrapper.get_exception] to check if the method threw an exception.
</description> </description>
<tutorials> <tutorials>
</tutorials> </tutorials>
<methods> <methods>
<method name="get_exception">
<return type="JavaObject" />
<description>
Returns the Java exception from the last call into a Java class. If there was no exception, it will return [code]null[/code].
[b]Note:[/b] This method only works on Android. On every other platform, this method will always return [code]null[/code].
</description>
</method>
<method name="wrap"> <method name="wrap">
<return type="JavaClass" /> <return type="JavaClass" />
<param index="0" name="name" type="String" /> <param index="0" name="name" type="String" />

View File

@@ -70,6 +70,7 @@ void JavaObject::_bind_methods() {
void JavaClassWrapper::_bind_methods() { void JavaClassWrapper::_bind_methods() {
ClassDB::bind_method(D_METHOD("wrap", "name"), &JavaClassWrapper::wrap); ClassDB::bind_method(D_METHOD("wrap", "name"), &JavaClassWrapper::wrap);
ClassDB::bind_method(D_METHOD("get_exception"), &JavaClassWrapper::get_exception);
} }
#if !defined(ANDROID_ENABLED) #if !defined(ANDROID_ENABLED)

View File

@@ -271,6 +271,8 @@ class JavaClassWrapper : public Object {
bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig); bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig);
#endif #endif
Ref<JavaObject> exception;
Ref<JavaClass> _wrap(const String &p_class, bool p_allow_private_methods_access); Ref<JavaClass> _wrap(const String &p_class, bool p_allow_private_methods_access);
static JavaClassWrapper *singleton; static JavaClassWrapper *singleton;
@@ -285,6 +287,10 @@ public:
return _wrap(p_class, false); return _wrap(p_class, false);
} }
Ref<JavaObject> get_exception() {
return exception;
}
#ifdef ANDROID_ENABLED #ifdef ANDROID_ENABLED
Ref<JavaClass> wrap_jclass(jclass p_class, bool p_allow_private_methods_access = false); Ref<JavaClass> wrap_jclass(jclass p_class, bool p_allow_private_methods_access = false);
#endif #endif

View File

@@ -517,6 +517,20 @@ bool JavaClass::_call_method(JavaObject *p_instance, const StringName &p_method,
env->DeleteLocalRef(E); env->DeleteLocalRef(E);
} }
jobject exception = env->ExceptionOccurred();
if (exception) {
env->ExceptionClear();
jclass java_class = env->GetObjectClass(exception);
Ref<JavaClass> java_class_wrapped = JavaClassWrapper::singleton->wrap_jclass(java_class);
env->DeleteLocalRef(java_class);
JavaClassWrapper::singleton->exception.instantiate(java_class_wrapped, exception);
env->DeleteLocalRef(exception);
} else {
JavaClassWrapper::singleton->exception.unref();
}
return success; return success;
} }