Skip to content

Commit 3a16864

Browse files
committed
Merge pull request #107075 from m4gr3d/implement_javaclasswrapper_has_method
[Android] `JavaClassWrapper` bug fixes
2 parents e9cd9a9 + 35fda7f commit 3a16864

File tree

12 files changed

+208
-147
lines changed

12 files changed

+208
-147
lines changed

doc/classes/JNISingleton.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@
99
<tutorials>
1010
<link title="Creating Android plugins">$DOCS_URL/tutorials/platform/android/android_plugin.html#doc-android-plugin</link>
1111
</tutorials>
12+
<methods>
13+
<method name="has_java_method" qualifiers="const">
14+
<return type="bool" />
15+
<param index="0" name="method" type="StringName" />
16+
<description>
17+
Returns [code]true[/code] if the given [param method] name exists in the JNISingleton's Java methods.
18+
</description>
19+
</method>
20+
</methods>
1221
</class>

doc/classes/JavaClass.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,12 @@
2929
Returns a [JavaClass] representing the Java parent class of this class.
3030
</description>
3131
</method>
32+
<method name="has_java_method" qualifiers="const">
33+
<return type="bool" />
34+
<param index="0" name="method" type="StringName" />
35+
<description>
36+
Returns [code]true[/code] if the given [param method] name exists in the object's Java methods.
37+
</description>
38+
</method>
3239
</methods>
3340
</class>

doc/classes/JavaObject.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
Returns the [JavaClass] that this object is an instance of.
1818
</description>
1919
</method>
20+
<method name="has_java_method" qualifiers="const">
21+
<return type="bool" />
22+
<param index="0" name="method" type="StringName" />
23+
<description>
24+
Returns [code]true[/code] if the given [param method] name exists in the object's Java methods.
25+
</description>
26+
</method>
2027
</methods>
2128
</class>

platform/android/api/api.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ void JavaClass::_bind_methods() {
6262
ClassDB::bind_method(D_METHOD("get_java_class_name"), &JavaClass::get_java_class_name);
6363
ClassDB::bind_method(D_METHOD("get_java_method_list"), &JavaClass::get_java_method_list);
6464
ClassDB::bind_method(D_METHOD("get_java_parent_class"), &JavaClass::get_java_parent_class);
65+
ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JavaClass::has_java_method);
6566
}
6667

6768
void JavaObject::_bind_methods() {
6869
ClassDB::bind_method(D_METHOD("get_java_class"), &JavaObject::get_java_class);
70+
ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JavaObject::has_java_method);
6971
}
7072

7173
void JavaClassWrapper::_bind_methods() {
@@ -94,6 +96,10 @@ Ref<JavaClass> JavaClass::get_java_parent_class() const {
9496
return Ref<JavaClass>();
9597
}
9698

99+
bool JavaClass::has_java_method(const StringName &) const {
100+
return false;
101+
}
102+
97103
JavaClass::JavaClass() {
98104
}
99105

@@ -108,6 +114,10 @@ Ref<JavaClass> JavaObject::get_java_class() const {
108114
return Ref<JavaClass>();
109115
}
110116

117+
bool JavaObject::has_java_method(const StringName &) const {
118+
return false;
119+
}
120+
111121
JavaClassWrapper *JavaClassWrapper::singleton = nullptr;
112122

113123
Ref<JavaClass> JavaClassWrapper::_wrap(const String &, bool) {

platform/android/api/java_class_wrapper.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class JavaClass : public RefCounted {
200200
String get_java_class_name() const;
201201
TypedArray<Dictionary> get_java_method_list() const;
202202
Ref<JavaClass> get_java_parent_class() const;
203+
bool has_java_method(const StringName &p_method) const;
203204

204205
#ifdef ANDROID_ENABLED
205206
virtual String to_string() override;
@@ -226,6 +227,7 @@ class JavaObject : public RefCounted {
226227
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
227228

228229
Ref<JavaClass> get_java_class() const;
230+
bool has_java_method(const StringName &p_method) const;
229231

230232
#ifdef ANDROID_ENABLED
231233
virtual String to_string() override;
@@ -244,7 +246,7 @@ class JavaClassWrapper : public Object {
244246
#ifdef ANDROID_ENABLED
245247
RBMap<String, Ref<JavaClass>> class_cache;
246248
friend class JavaClass;
247-
jmethodID Class_getDeclaredConstructors;
249+
jmethodID Class_getConstructors;
248250
jmethodID Class_getDeclaredMethods;
249251
jmethodID Class_getFields;
250252
jmethodID Class_getName;
@@ -272,7 +274,7 @@ class JavaClassWrapper : public Object {
272274

273275
Ref<JavaObject> exception;
274276

275-
Ref<JavaClass> _wrap(const String &p_class, bool p_allow_private_methods_access);
277+
Ref<JavaClass> _wrap(const String &p_class, bool p_allow_non_public_methods_access);
276278

277279
static JavaClassWrapper *singleton;
278280

platform/android/api/jni_singleton.h

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,60 @@ class JNISingleton : public Object {
4646
RBMap<StringName, MethodData> method_map;
4747
Ref<JavaObject> wrapped_object;
4848

49+
protected:
50+
static void _bind_methods() {
51+
ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JNISingleton::has_java_method);
52+
}
53+
4954
public:
5055
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
51-
if (wrapped_object.is_valid()) {
52-
RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);
53-
54-
// Check the method we're looking for is in the JNISingleton map and that
55-
// the arguments match.
56-
bool call_error = !E || E->get().argtypes.size() != p_argcount;
57-
if (!call_error) {
58-
for (int i = 0; i < p_argcount; i++) {
59-
if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
60-
call_error = true;
61-
break;
56+
// Godot methods take precedence.
57+
Variant ret = Object::callp(p_method, p_args, p_argcount, r_error);
58+
if (r_error.error == Callable::CallError::CALL_OK) {
59+
return ret;
60+
}
61+
62+
// Check the method we're looking for is in the JNISingleton map.
63+
RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);
64+
if (E) {
65+
if (wrapped_object.is_valid()) {
66+
// Check that the arguments match.
67+
int method_arg_count = E->get().argtypes.size();
68+
if (p_argcount < method_arg_count) {
69+
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
70+
} else if (p_argcount > method_arg_count) {
71+
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
72+
} else {
73+
// Check the arguments are valid.
74+
bool arguments_valid = true;
75+
for (int i = 0; i < p_argcount; i++) {
76+
if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
77+
arguments_valid = false;
78+
break;
79+
}
6280
}
63-
}
64-
}
6581

66-
if (!call_error) {
67-
return wrapped_object->callp(p_method, p_args, p_argcount, r_error);
82+
if (arguments_valid) {
83+
return wrapped_object->callp(p_method, p_args, p_argcount, r_error);
84+
} else {
85+
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
86+
}
87+
}
88+
} else {
89+
r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
6890
}
6991
}
70-
71-
return Object::callp(p_method, p_args, p_argcount, r_error);
92+
return Variant();
7293
}
7394

7495
Ref<JavaObject> get_wrapped_object() const {
7596
return wrapped_object;
7697
}
7798

99+
bool has_java_method(const StringName &p_method) const {
100+
return method_map.has(p_method);
101+
}
102+
78103
void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
79104
MethodData md;
80105
md.argtypes = p_args;

platform/android/java/lib/src/org/godotengine/godot/plugin/AndroidRuntimePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class AndroidRuntimePlugin(godot: Godot) : GodotPlugin(godot) {
5151
* Provides access to the host [android.app.Activity] to GDScript
5252
*/
5353
@UsedByGodot
54-
override fun getActivity() = super.getActivity()
54+
public override fun getActivity() = super.getActivity()
5555

5656
/**
5757
* Utility method used to create [Runnable] from Godot [Callable].

0 commit comments

Comments
 (0)