Skip to content

Commit

Permalink
Fix JavaClassWrapper so it actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnopek committed Aug 27, 2024
1 parent 142d332 commit 44712f1
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 60 deletions.
24 changes: 22 additions & 2 deletions doc/classes/JavaClass.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="JavaClass" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Represents an object from the Java Native Interface.
Represents a class from the Java Native Interface.
</brief_description>
<description>
Represents an object from the Java Native Interface. It is returned from [method JavaClassWrapper.wrap].
Represents a class from the Java Native Interface. It is returned from [method JavaClassWrapper.wrap].
[b]Note:[/b] This class only works on Android. For any other build, this class does nothing.
[b]Note:[/b] This class is not to be confused with [JavaScriptObject].
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_java_class_name" qualifiers="const">
<return type="String" />
<description>
Returns the Java class name.
</description>
</method>
<method name="get_java_method_list" qualifiers="const">
<return type="Dictionary[]" />
<description>
Returns the object's Java methods and their signatures as an [Array] of dictionaries, in the same format as [method Object.get_method_list].
</description>
</method>
<method name="get_java_parent_class" qualifiers="const">
<return type="JavaClass" />
<description>
Returns a [JavaClass] representing the Java parent class of this class.
</description>
</method>
</methods>
</class>
9 changes: 9 additions & 0 deletions doc/classes/JavaClassWrapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<description>
The JavaClassWrapper singleton provides a way for the Godot application to send and receive data through the [url=https://developer.android.com/training/articles/perf-jni]Java Native Interface[/url] (JNI).
[b]Note:[/b] This singleton is only available in Android builds.
[codeblock]
var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime")
var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter")

var datetime = LocalDateTime.now()
var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")

print(datetime.format(formatter))
[/codeblock]
</description>
<tutorials>
</tutorials>
Expand Down
20 changes: 20 additions & 0 deletions doc/classes/JavaObject.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="JavaObject" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Represents an object from the Java Native Interface. It can be return from Java methods called on [JavaClass]. See [JavaClassWrapper] for an example.
[b]Note:[/b] This class only works on Android. For any other build, this class does nothing.
[b]Note:[/b] This class is not to be confused with [JavaScriptObject].
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_java_class" qualifiers="const">
<return type="JavaClass" />
<description>
Returns the [JavaClass] that this object is an instance of.
</description>
</method>
</methods>
</class>
30 changes: 30 additions & 0 deletions platform/android/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void register_android_api() {
#endif

GDREGISTER_CLASS(JavaClass);
GDREGISTER_CLASS(JavaObject);
GDREGISTER_CLASS(JavaClassWrapper);
Engine::get_singleton()->add_singleton(Engine::Singleton("JavaClassWrapper", JavaClassWrapper::get_singleton()));
}
Expand All @@ -59,6 +60,16 @@ void unregister_android_api() {
#endif
}

void JavaClass::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_java_class_name"), &JavaClass::get_java_class_name);
ClassDB::bind_method(D_METHOD("get_java_method_list"), &JavaClass::get_java_method_list);
ClassDB::bind_method(D_METHOD("get_java_parent_class"), &JavaClass::get_java_parent_class);
}

void JavaObject::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_java_class"), &JavaObject::get_java_class);
}

void JavaClassWrapper::_bind_methods() {
ClassDB::bind_method(D_METHOD("wrap", "name"), &JavaClassWrapper::wrap);
}
Expand All @@ -69,13 +80,32 @@ Variant JavaClass::callp(const StringName &, const Variant **, int, Callable::Ca
return Variant();
}

String JavaClass::get_java_class_name() const {
return "";
}

TypedArray<Dictionary> JavaClass::get_java_method_list() const {
return TypedArray<Dictionary>();
}

Ref<JavaClass> JavaClass::get_java_parent_class() const {
return Ref<JavaClass>();
}

JavaClass::JavaClass() {
}

JavaClass::~JavaClass() {
}

Variant JavaObject::callp(const StringName &, const Variant **, int, Callable::CallError &) {
return Variant();
}

Ref<JavaClass> JavaObject::get_java_class() const {
return Ref<JavaClass>();
}

JavaClassWrapper *JavaClassWrapper::singleton = nullptr;

Ref<JavaClass> JavaClassWrapper::wrap(const String &) {
Expand Down
49 changes: 41 additions & 8 deletions platform/android/api/java_class_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define JAVA_CLASS_WRAPPER_H

#include "core/object/ref_counted.h"
#include "core/variant/typed_array.h"

#ifdef ANDROID_ENABLED
#include <android/log.h>
Expand Down Expand Up @@ -67,6 +68,7 @@ class JavaClass : public RefCounted {

struct MethodInfo {
bool _static = false;
bool _constructor = false;
Vector<uint32_t> param_types;
Vector<StringName> param_sigs;
uint32_t return_type = 0;
Expand Down Expand Up @@ -174,14 +176,29 @@ class JavaClass : public RefCounted {
bool _call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error, Variant &ret);

friend class JavaClassWrapper;
friend class JavaObject;
String java_class_name;
String java_constructor_name;
HashMap<StringName, List<MethodInfo>> methods;
jclass _class;
#endif

protected:
static void _bind_methods();

public:
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;

String get_java_class_name() const;
TypedArray<Dictionary> get_java_method_list() const;
Ref<JavaClass> get_java_parent_class() const;

#ifdef ANDROID_ENABLED
virtual String to_string() override;
#endif

JavaClass();
~JavaClass();
};

class JavaObject : public RefCounted {
Expand All @@ -191,14 +208,24 @@ class JavaObject : public RefCounted {
Ref<JavaClass> base_class;
friend class JavaClass;

jobject instance;
jobject instance = nullptr;
#endif

protected:
static void _bind_methods();

public:
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;

Ref<JavaClass> get_java_class() const;

#ifdef ANDROID_ENABLED
JavaObject(const Ref<JavaClass> &p_base, jobject *p_instance);
virtual String to_string() override;

jobject get_instance() { return instance; }

JavaObject();
JavaObject(const Ref<JavaClass> &p_base, jobject p_instance);
~JavaObject();
#endif
};
Expand All @@ -209,13 +236,17 @@ class JavaClassWrapper : public Object {
#ifdef ANDROID_ENABLED
RBMap<String, Ref<JavaClass>> class_cache;
friend class JavaClass;
jmethodID getDeclaredMethods;
jmethodID getFields;
jmethodID getParameterTypes;
jmethodID getReturnType;
jmethodID getModifiers;
jmethodID getName;
jmethodID Class_getDeclaredConstructors;
jmethodID Class_getDeclaredMethods;
jmethodID Class_getFields;
jmethodID Class_getName;
jmethodID Class_getSuperclass;
jmethodID Constructor_getParameterTypes;
jmethodID Constructor_getModifiers;
jmethodID Method_getParameterTypes;
jmethodID Method_getReturnType;
jmethodID Method_getModifiers;
jmethodID Method_getName;
jmethodID Field_getName;
jmethodID Field_getModifiers;
jmethodID Field_get;
Expand All @@ -242,6 +273,8 @@ class JavaClassWrapper : public Object {
Ref<JavaClass> wrap(const String &p_class);

#ifdef ANDROID_ENABLED
Ref<JavaClass> wrap_jclass(jclass p_class);

JavaClassWrapper(jobject p_activity = nullptr);
#else
JavaClassWrapper();
Expand Down
5 changes: 5 additions & 0 deletions platform/android/api/jni_singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class JNISingleton : public Object {
env->DeleteLocalRef(obj);

} break;
case Variant::OBJECT: {
jobject obj = env->CallObjectMethodA(instance, E->get().method, v);
ret = _jobject_to_variant(env, obj);
env->DeleteLocalRef(obj);
} break;
default: {
env->PopLocalFrame(nullptr);
ERR_FAIL_V(Variant());
Expand Down
Loading

0 comments on commit 44712f1

Please sign in to comment.