2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* java_class_wrapper.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2020-01-19 20:02:40 +01:00
|
|
|
#include "api/java_class_wrapper.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2019-02-26 21:54:45 +01:00
|
|
|
#include "string_android.h"
|
2014-09-03 04:13:40 +02:00
|
|
|
#include "thread_jandroid.h"
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
bool JavaClass::_call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error, Variant &ret) {
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<StringName, List<MethodInfo>>::Iterator M = methods.find(p_method);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!M) {
|
2014-09-03 04:13:40 +02:00
|
|
|
return false;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, false);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MethodInfo *method = nullptr;
|
2022-05-13 15:04:37 +02:00
|
|
|
for (MethodInfo &E : M->value) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!p_instance && !E._static) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
|
2014-09-03 04:13:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
int pc = E.param_types.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pc > p_argcount) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error.argument = pc;
|
2014-09-03 04:13:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pc < p_argcount) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error.argument = pc;
|
2014-09-03 04:13:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-16 05:45:57 +02:00
|
|
|
uint32_t *ptypes = E.param_types.ptrw();
|
2017-03-05 16:44:50 +01:00
|
|
|
bool valid = true;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < pc; i++) {
|
|
|
|
Variant::Type arg_expected = Variant::NIL;
|
|
|
|
switch (ptypes[i]) {
|
2014-09-03 04:13:40 +02:00
|
|
|
case ARG_TYPE_VOID: {
|
|
|
|
//bug?
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_BOOLEAN: {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_args[i]->get_type() != Variant::BOOL) {
|
2017-03-05 16:44:50 +01:00
|
|
|
arg_expected = Variant::BOOL;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_BYTE:
|
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_CHAR:
|
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_SHORT:
|
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_INT:
|
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_LONG:
|
2014-09-03 04:13:40 +02:00
|
|
|
case ARG_TYPE_BYTE:
|
|
|
|
case ARG_TYPE_CHAR:
|
|
|
|
case ARG_TYPE_SHORT:
|
|
|
|
case ARG_TYPE_INT:
|
|
|
|
case ARG_TYPE_LONG: {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!p_args[i]->is_num()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
arg_expected = Variant::INT;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_FLOAT:
|
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_DOUBLE:
|
2014-09-03 04:13:40 +02:00
|
|
|
case ARG_TYPE_FLOAT:
|
|
|
|
case ARG_TYPE_DOUBLE: {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!p_args[i]->is_num()) {
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
arg_expected = Variant::FLOAT;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_STRING: {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_args[i]->get_type() != Variant::STRING) {
|
2017-03-05 16:44:50 +01:00
|
|
|
arg_expected = Variant::STRING;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_CLASS: {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_args[i]->get_type() != Variant::OBJECT) {
|
2017-03-05 16:44:50 +01:00
|
|
|
arg_expected = Variant::OBJECT;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2021-06-04 18:03:15 +02:00
|
|
|
Ref<RefCounted> ref = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
if (!ref.is_null()) {
|
2017-08-26 17:10:58 +02:00
|
|
|
if (Object::cast_to<JavaObject>(ref.ptr())) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<JavaObject> jo = ref;
|
2014-09-03 04:13:40 +02:00
|
|
|
//could be faster
|
2021-07-16 05:45:57 +02:00
|
|
|
jclass c = env->FindClass(E.param_sigs[i].operator String().utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!c || !env->IsInstanceOf(jo->instance, c)) {
|
|
|
|
arg_expected = Variant::OBJECT;
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
|
|
|
//ok
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
arg_expected = Variant::OBJECT;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_args[i]->get_type() != Variant::ARRAY) {
|
2017-03-05 16:44:50 +01:00
|
|
|
arg_expected = Variant::ARRAY;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (arg_expected != Variant::NIL) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error.argument = i;
|
|
|
|
r_error.expected = arg_expected;
|
|
|
|
valid = false;
|
2014-09-03 04:13:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!valid) {
|
2014-09-03 04:13:40 +02:00
|
|
|
continue;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
method = &E;
|
2014-09-03 04:13:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!method) {
|
2014-09-03 04:13:40 +02:00
|
|
|
return true; //no version convinces
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
jvalue *argv = nullptr;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
if (method->param_types.size()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv = (jvalue *)alloca(sizeof(jvalue) * method->param_types.size());
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
List<jobject> to_free;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < method->param_types.size(); i++) {
|
|
|
|
switch (method->param_types[i]) {
|
|
|
|
case ARG_TYPE_VOID: {
|
2014-09-03 04:13:40 +02:00
|
|
|
//can't happen
|
2020-04-02 01:20:12 +02:00
|
|
|
argv[i].l = nullptr; //I hope this works
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case ARG_TYPE_BOOLEAN: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].z = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_BYTE: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].b = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_CHAR: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].c = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_SHORT: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].s = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_INT: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].i = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_LONG: {
|
2017-07-25 12:28:31 +02:00
|
|
|
argv[i].j = (int64_t)*p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_FLOAT: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].f = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
case ARG_TYPE_DOUBLE: {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].d = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_BOOLEAN: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Boolean");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(Z)V");
|
|
|
|
jvalue val;
|
|
|
|
val.z = (bool)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_BYTE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Byte");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(B)V");
|
|
|
|
jvalue val;
|
|
|
|
val.b = (int)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_CHAR: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Character");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(C)V");
|
|
|
|
jvalue val;
|
|
|
|
val.c = (int)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_SHORT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Short");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(S)V");
|
|
|
|
jvalue val;
|
|
|
|
val.s = (int)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_INT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Integer");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(I)V");
|
|
|
|
jvalue val;
|
|
|
|
val.i = (int)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_LONG: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Long");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(J)V");
|
|
|
|
jvalue val;
|
|
|
|
val.j = (int64_t)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_FLOAT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Float");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(F)V");
|
|
|
|
jvalue val;
|
|
|
|
val.f = (float)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_TYPE_DOUBLE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass bclass = env->FindClass("java/lang/Double");
|
|
|
|
jmethodID ctor = env->GetMethodID(bclass, "<init>", "(D)V");
|
|
|
|
jvalue val;
|
|
|
|
val.d = (double)(*p_args[i]);
|
|
|
|
jobject obj = env->NewObjectA(bclass, ctor, &val);
|
|
|
|
argv[i].l = obj;
|
|
|
|
to_free.push_back(obj);
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_STRING: {
|
|
|
|
String s = *p_args[i];
|
|
|
|
jstring jStr = env->NewStringUTF(s.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = jStr;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(jStr);
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_CLASS: {
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<JavaObject> jo = *p_args[i];
|
2014-09-03 04:13:40 +02:00
|
|
|
if (jo.is_valid()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = jo->instance;
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2020-04-02 01:20:12 +02:00
|
|
|
argv[i].l = nullptr; //I hope this works
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jbooleanArray a = env->NewBooleanArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jboolean val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetBooleanArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_BYTE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jbyteArray a = env->NewByteArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jbyte val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetByteArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_CHAR: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jcharArray a = env->NewCharArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jchar val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetCharArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_SHORT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jshortArray a = env->NewShortArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jshort val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetShortArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_INT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jintArray a = env->NewIntArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jint val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetIntArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_LONG: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jlongArray a = env->NewLongArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2017-07-25 12:28:31 +02:00
|
|
|
jlong val = (int64_t)arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetLongArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_FLOAT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jfloatArray a = env->NewFloatArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jfloat val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetFloatArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
|
|
|
jdoubleArray a = env->NewDoubleArray(arr.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jdouble val = arr[j];
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetDoubleArrayRegion(a, j, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_STRING: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array arr = *p_args[i];
|
2020-04-02 01:20:12 +02:00
|
|
|
jobjectArray a = env->NewObjectArray(arr.size(), env->FindClass("java/lang/String"), nullptr);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < arr.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
String s = arr[j];
|
|
|
|
jstring jStr = env->NewStringUTF(s.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
env->SetObjectArrayElement(a, j, jStr);
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(jStr);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
argv[i].l = a;
|
2014-09-03 04:13:40 +02:00
|
|
|
to_free.push_back(a);
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_CLASS: {
|
2020-04-02 01:20:12 +02:00
|
|
|
argv[i].l = nullptr;
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool success = true;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (method->return_type) {
|
2014-09-03 04:13:40 +02:00
|
|
|
case ARG_TYPE_VOID: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallStaticVoidMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = Variant();
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_BOOLEAN: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticBooleanMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallBooleanMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_BYTE: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticByteMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallByteMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_CHAR: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticCharMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallCharMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_SHORT: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticShortMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallShortMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_INT: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticIntMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallIntMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_LONG: {
|
|
|
|
if (method->_static) {
|
2017-07-25 12:28:31 +02:00
|
|
|
ret = (int64_t)env->CallStaticLongMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-07-25 12:28:31 +02:00
|
|
|
ret = (int64_t)env->CallLongMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_FLOAT: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticFloatMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallFloatMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_DOUBLE: {
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallStaticDoubleMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = env->CallDoubleMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
jobject obj;
|
|
|
|
if (method->_static) {
|
2017-03-05 16:44:50 +01:00
|
|
|
obj = env->CallStaticObjectMethodA(_class, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
obj = env->CallObjectMethodA(p_instance->instance, method->method, argv);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ret = Variant();
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!_convert_object_to_variant(env, obj, ret, method->return_type)) {
|
|
|
|
ret = Variant();
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
2017-03-05 16:44:50 +01:00
|
|
|
success = false;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
env->DeleteLocalRef(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
for (jobject &E : to_free) {
|
|
|
|
env->DeleteLocalRef(E);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:58:40 +01:00
|
|
|
Variant JavaClass::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
2014-09-03 04:13:40 +02:00
|
|
|
Variant ret;
|
2020-04-02 01:20:12 +02:00
|
|
|
bool found = _call_method(nullptr, p_method, p_args, p_argcount, r_error, ret);
|
2014-09-03 04:13:40 +02:00
|
|
|
if (found) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:58:40 +01:00
|
|
|
return RefCounted::callp(p_method, p_args, p_argcount, r_error);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JavaClass::JavaClass() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
|
2022-03-09 14:58:40 +01:00
|
|
|
Variant JavaObject::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
2014-09-03 04:13:40 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
JavaObject::JavaObject(const Ref<JavaClass> &p_base, jobject *p_instance) {
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
JavaObject::~JavaObject() {
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool JavaClassWrapper::_get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jstring name2 = (jstring)env->CallObjectMethod(obj, Class_getName);
|
2019-02-26 21:54:45 +01:00
|
|
|
String str_type = jstring_to_string(name2, env);
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(name2);
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t t = 0;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
if (str_type.begins_with("[")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
t = JavaClass::ARG_ARRAY_BIT;
|
|
|
|
strsig = "[";
|
|
|
|
str_type = str_type.substr(1, str_type.length() - 1);
|
2014-09-03 04:13:40 +02:00
|
|
|
if (str_type.begins_with("[")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
print_line("Nested arrays not supported for type: " + str_type);
|
2014-09-03 04:13:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (str_type.begins_with("L")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
str_type = str_type.substr(1, str_type.length() - 2); //ok it's a class
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (str_type == "void" || str_type == "V") {
|
|
|
|
t |= JavaClass::ARG_TYPE_VOID;
|
|
|
|
strsig += "V";
|
|
|
|
} else if (str_type == "boolean" || str_type == "Z") {
|
|
|
|
t |= JavaClass::ARG_TYPE_BOOLEAN;
|
|
|
|
strsig += "Z";
|
|
|
|
} else if (str_type == "byte" || str_type == "B") {
|
|
|
|
t |= JavaClass::ARG_TYPE_BYTE;
|
|
|
|
strsig += "B";
|
|
|
|
} else if (str_type == "char" || str_type == "C") {
|
|
|
|
t |= JavaClass::ARG_TYPE_CHAR;
|
|
|
|
strsig += "C";
|
|
|
|
} else if (str_type == "short" || str_type == "S") {
|
|
|
|
t |= JavaClass::ARG_TYPE_SHORT;
|
|
|
|
strsig += "S";
|
|
|
|
} else if (str_type == "int" || str_type == "I") {
|
|
|
|
t |= JavaClass::ARG_TYPE_INT;
|
|
|
|
strsig += "I";
|
|
|
|
} else if (str_type == "long" || str_type == "J") {
|
|
|
|
t |= JavaClass::ARG_TYPE_LONG;
|
|
|
|
strsig += "J";
|
|
|
|
} else if (str_type == "float" || str_type == "F") {
|
|
|
|
t |= JavaClass::ARG_TYPE_FLOAT;
|
|
|
|
strsig += "F";
|
|
|
|
} else if (str_type == "double" || str_type == "D") {
|
|
|
|
t |= JavaClass::ARG_TYPE_DOUBLE;
|
|
|
|
strsig += "D";
|
|
|
|
} else if (str_type == "java.lang.String") {
|
|
|
|
t |= JavaClass::ARG_TYPE_STRING;
|
|
|
|
strsig += "Ljava/lang/String;";
|
|
|
|
} else if (str_type == "java.lang.Boolean") {
|
|
|
|
t |= JavaClass::ARG_TYPE_BOOLEAN | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Boolean;";
|
|
|
|
} else if (str_type == "java.lang.Byte") {
|
|
|
|
t |= JavaClass::ARG_TYPE_BYTE | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Byte;";
|
|
|
|
} else if (str_type == "java.lang.Character") {
|
|
|
|
t |= JavaClass::ARG_TYPE_CHAR | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Character;";
|
|
|
|
} else if (str_type == "java.lang.Short") {
|
|
|
|
t |= JavaClass::ARG_TYPE_SHORT | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Short;";
|
|
|
|
} else if (str_type == "java.lang.Integer") {
|
|
|
|
t |= JavaClass::ARG_TYPE_INT | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Integer;";
|
|
|
|
} else if (str_type == "java.lang.Long") {
|
|
|
|
t |= JavaClass::ARG_TYPE_LONG | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Long;";
|
|
|
|
} else if (str_type == "java.lang.Float") {
|
|
|
|
t |= JavaClass::ARG_TYPE_FLOAT | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Float;";
|
|
|
|
} else if (str_type == "java.lang.Double") {
|
|
|
|
t |= JavaClass::ARG_TYPE_DOUBLE | JavaClass::ARG_NUMBER_CLASS_BIT;
|
|
|
|
strsig += "Ljava/lang/Double;";
|
2014-09-03 04:13:40 +02:00
|
|
|
} else {
|
|
|
|
//a class likely
|
2017-03-05 16:44:50 +01:00
|
|
|
strsig += "L" + str_type.replace(".", "/") + ";";
|
|
|
|
t |= JavaClass::ARG_TYPE_CLASS;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sig = t;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool JavaClass::_convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig) {
|
2014-09-03 04:13:40 +02:00
|
|
|
if (!obj) {
|
2017-03-05 16:44:50 +01:00
|
|
|
var = Variant(); //seems null is just null...
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_sig) {
|
2014-09-03 04:13:40 +02:00
|
|
|
case ARG_TYPE_VOID: {
|
|
|
|
return Variant();
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallBooleanMethod(obj, JavaClassWrapper::singleton->Boolean_booleanValue);
|
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallByteMethod(obj, JavaClassWrapper::singleton->Byte_byteValue);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallCharMethod(obj, JavaClassWrapper::singleton->Character_characterValue);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallShortMethod(obj, JavaClassWrapper::singleton->Short_shortValue);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallIntMethod(obj, JavaClassWrapper::singleton->Integer_integerValue);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT: {
|
2017-07-25 12:28:31 +02:00
|
|
|
var = (int64_t)env->CallLongMethod(obj, JavaClassWrapper::singleton->Long_longValue);
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallFloatMethod(obj, JavaClassWrapper::singleton->Float_floatValue);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = env->CallDoubleMethod(obj, JavaClassWrapper::singleton->Double_doubleValue);
|
|
|
|
return true;
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_STRING: {
|
2019-02-26 21:54:45 +01:00
|
|
|
var = jstring_to_string((jstring)obj, env);
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
|
|
|
case ARG_TYPE_CLASS: {
|
|
|
|
return false;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_VOID: {
|
2014-09-03 04:13:40 +02:00
|
|
|
var = Array(); // ?
|
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jboolean val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetBooleanArrayRegion((jbooleanArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_BYTE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jbyte val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetByteArrayRegion((jbyteArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_CHAR: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jchar val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetCharArrayRegion((jcharArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_SHORT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jshort val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetShortArrayRegion((jshortArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_INT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jint val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetIntArrayRegion((jintArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_LONG: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jlong val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetLongArrayRegion((jlongArray)arr, 0, 1, &val);
|
2017-07-25 12:28:31 +02:00
|
|
|
ret.push_back((int64_t)val);
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_FLOAT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jfloat val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetFloatArrayRegion((jfloatArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jdouble val;
|
2017-03-05 16:44:50 +01:00
|
|
|
env->GetDoubleArrayRegion((jdoubleArray)arr, 0, 1, &val);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
bool val = env->CallBooleanMethod(o, JavaClassWrapper::singleton->Boolean_booleanValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_BYTE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
int val = env->CallByteMethod(o, JavaClassWrapper::singleton->Byte_byteValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_CHAR: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
int val = env->CallCharMethod(o, JavaClassWrapper::singleton->Character_characterValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_SHORT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
int val = env->CallShortMethod(o, JavaClassWrapper::singleton->Short_shortValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_INT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
int val = env->CallIntMethod(o, JavaClassWrapper::singleton->Integer_integerValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_LONG: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
int64_t val = env->CallLongMethod(o, JavaClassWrapper::singleton->Long_longValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_FLOAT: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
float val = env->CallFloatMethod(o, JavaClassWrapper::singleton->Float_floatValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_DOUBLE: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
double val = env->CallDoubleMethod(o, JavaClassWrapper::singleton->Double_doubleValue);
|
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_STRING: {
|
2014-09-03 04:13:40 +02:00
|
|
|
Array ret;
|
|
|
|
jobjectArray arr = (jobjectArray)obj;
|
|
|
|
|
|
|
|
int count = env->GetArrayLength(arr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject o = env->GetObjectArrayElement(arr, i);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!o) {
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(Variant());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2019-02-26 21:54:45 +01:00
|
|
|
String val = jstring_to_string((jstring)o, env);
|
2014-09-03 04:13:40 +02:00
|
|
|
ret.push_back(val);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(o);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
var = ret;
|
2014-09-03 04:13:40 +02:00
|
|
|
return true;
|
|
|
|
} break;
|
2017-03-05 16:44:50 +01:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_CLASS: {
|
2014-09-03 04:13:40 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<JavaClass> JavaClassWrapper::wrap(const String &p_class) {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (class_cache.has(p_class)) {
|
2014-09-03 04:13:40 +02:00
|
|
|
return class_cache[p_class];
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, Ref<JavaClass>());
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
jclass bclass = env->FindClass(p_class.utf8().get_data());
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(bclass, Ref<JavaClass>());
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
jobjectArray methods = (jobjectArray)env->CallObjectMethod(bclass, getDeclaredMethods);
|
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(methods, Ref<JavaClass>());
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<JavaClass> java_class = memnew(JavaClass);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
int count = env->GetArrayLength(methods);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject obj = env->GetObjectArrayElement(methods, i);
|
|
|
|
ERR_CONTINUE(!obj);
|
|
|
|
|
|
|
|
jstring name = (jstring)env->CallObjectMethod(obj, getName);
|
2019-02-26 21:54:45 +01:00
|
|
|
String str_method = jstring_to_string(name, env);
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(name);
|
|
|
|
|
|
|
|
Vector<String> params;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
jint mods = env->CallIntMethod(obj, getModifiers);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!(mods & 0x0001)) {
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(obj);
|
|
|
|
continue; //not public bye
|
|
|
|
}
|
|
|
|
|
|
|
|
jobjectArray param_types = (jobjectArray)env->CallObjectMethod(obj, getParameterTypes);
|
2017-03-05 16:44:50 +01:00
|
|
|
int count2 = env->GetArrayLength(param_types);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
if (!java_class->methods.has(str_method)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
java_class->methods[str_method] = List<JavaClass::MethodInfo>();
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JavaClass::MethodInfo mi;
|
2017-03-05 16:44:50 +01:00
|
|
|
mi._static = (mods & 0x8) != 0;
|
|
|
|
bool valid = true;
|
|
|
|
String signature = "(";
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < count2; j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject obj2 = env->GetObjectArrayElement(param_types, j);
|
|
|
|
String strsig;
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t sig = 0;
|
|
|
|
if (!_get_type_sig(env, obj2, sig, strsig)) {
|
|
|
|
valid = false;
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(obj2);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
signature += strsig;
|
2014-09-03 04:13:40 +02:00
|
|
|
mi.param_types.push_back(sig);
|
|
|
|
mi.param_sigs.push_back(strsig);
|
|
|
|
env->DeleteLocalRef(obj2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid) {
|
2018-02-17 22:34:08 +01:00
|
|
|
print_line("Method can't be bound (unsupported arguments): " + p_class + "::" + str_method);
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(obj);
|
|
|
|
env->DeleteLocalRef(param_types);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
signature += ")";
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
jobject return_type = (jobject)env->CallObjectMethod(obj, getReturnType);
|
|
|
|
|
|
|
|
String strsig;
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t sig = 0;
|
|
|
|
if (!_get_type_sig(env, return_type, sig, strsig)) {
|
2018-02-17 22:34:08 +01:00
|
|
|
print_line("Method can't be bound (unsupported return type): " + p_class + "::" + str_method);
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(obj);
|
|
|
|
env->DeleteLocalRef(param_types);
|
|
|
|
env->DeleteLocalRef(return_type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
signature += strsig;
|
|
|
|
mi.return_type = sig;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool discard = false;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<JavaClass::MethodInfo>::Element *E = java_class->methods[str_method].front(); E; E = E->next()) {
|
|
|
|
float new_likeliness = 0;
|
|
|
|
float existing_likeliness = 0;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (E->get().param_types.size() != mi.param_types.size()) {
|
2014-09-03 04:13:40 +02:00
|
|
|
continue;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2022-05-30 23:13:49 +02:00
|
|
|
bool this_valid = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < E->get().param_types.size(); j++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
Variant::Type _new;
|
|
|
|
float new_l;
|
|
|
|
Variant::Type existing;
|
|
|
|
float existing_l;
|
2017-03-05 16:44:50 +01:00
|
|
|
JavaClass::_convert_to_variant_type(E->get().param_types[j], existing, existing_l);
|
|
|
|
JavaClass::_convert_to_variant_type(mi.param_types[j], _new, new_l);
|
|
|
|
if (_new != existing) {
|
2022-05-30 23:13:49 +02:00
|
|
|
this_valid = false;
|
2014-09-03 04:13:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
new_likeliness += new_l;
|
|
|
|
existing_likeliness = existing_l;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
if (!this_valid) {
|
2014-09-03 04:13:40 +02:00
|
|
|
continue;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (new_likeliness > existing_likeliness) {
|
2014-09-03 04:13:40 +02:00
|
|
|
java_class->methods[str_method].erase(E);
|
|
|
|
break;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
discard = true;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!discard) {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (mi._static) {
|
2014-09-03 04:13:40 +02:00
|
|
|
mi.method = env->GetStaticMethodID(bclass, str_method.utf8().get_data(), signature.utf8().get_data());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2014-09-03 04:13:40 +02:00
|
|
|
mi.method = env->GetMethodID(bclass, str_method.utf8().get_data(), signature.utf8().get_data());
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
ERR_CONTINUE(!mi.method);
|
|
|
|
|
|
|
|
java_class->methods[str_method].push_back(mi);
|
|
|
|
}
|
|
|
|
|
|
|
|
env->DeleteLocalRef(obj);
|
|
|
|
env->DeleteLocalRef(param_types);
|
|
|
|
env->DeleteLocalRef(return_type);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
env->DeleteLocalRef(methods);
|
|
|
|
|
|
|
|
jobjectArray fields = (jobjectArray)env->CallObjectMethod(bclass, getFields);
|
|
|
|
|
|
|
|
count = env->GetArrayLength(fields);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-03 04:13:40 +02:00
|
|
|
jobject obj = env->GetObjectArrayElement(fields, i);
|
|
|
|
ERR_CONTINUE(!obj);
|
|
|
|
|
|
|
|
jstring name = (jstring)env->CallObjectMethod(obj, Field_getName);
|
2019-02-26 21:54:45 +01:00
|
|
|
String str_field = jstring_to_string(name, env);
|
2014-09-03 04:13:40 +02:00
|
|
|
env->DeleteLocalRef(name);
|
2017-03-05 16:44:50 +01:00
|
|
|
int mods = env->CallIntMethod(obj, Field_getModifiers);
|
|
|
|
if ((mods & 0x8) && (mods & 0x10) && (mods & 0x1)) { //static final public!
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
jobject objc = env->CallObjectMethod(obj, Field_get, nullptr);
|
2014-09-03 04:13:40 +02:00
|
|
|
if (objc) {
|
|
|
|
uint32_t sig;
|
|
|
|
String strsig;
|
|
|
|
jclass cl = env->GetObjectClass(objc);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (JavaClassWrapper::_get_type_sig(env, cl, sig, strsig)) {
|
|
|
|
if ((sig & JavaClass::ARG_TYPE_MASK) <= JavaClass::ARG_TYPE_STRING) {
|
2014-09-03 04:13:40 +02:00
|
|
|
Variant value;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (JavaClass::_convert_object_to_variant(env, objc, value, sig)) {
|
|
|
|
java_class->constant_map[str_field] = value;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
env->DeleteLocalRef(cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
env->DeleteLocalRef(objc);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
env->DeleteLocalRef(fields);
|
|
|
|
|
|
|
|
return Ref<JavaClass>();
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
JavaClassWrapper *JavaClassWrapper::singleton = nullptr;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
JavaClassWrapper::JavaClassWrapper(jobject p_activity) {
|
2017-03-05 16:44:50 +01:00
|
|
|
singleton = this;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(env);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2022-05-30 23:13:49 +02:00
|
|
|
jclass activity = env->FindClass("android/app/Activity");
|
|
|
|
jmethodID getClassLoader = env->GetMethodID(activity, "getClassLoader", "()Ljava/lang/ClassLoader;");
|
2014-09-03 04:13:40 +02:00
|
|
|
classLoader = env->CallObjectMethod(p_activity, getClassLoader);
|
2017-03-05 16:44:50 +01:00
|
|
|
classLoader = (jclass)env->NewGlobalRef(classLoader);
|
2014-09-03 04:13:40 +02:00
|
|
|
jclass classLoaderClass = env->FindClass("java/lang/ClassLoader");
|
|
|
|
findClass = env->GetMethodID(classLoaderClass, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
|
|
|
|
|
|
|
|
jclass bclass = env->FindClass("java/lang/Class");
|
2017-03-05 16:44:50 +01:00
|
|
|
getDeclaredMethods = env->GetMethodID(bclass, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;");
|
|
|
|
getFields = env->GetMethodID(bclass, "getFields", "()[Ljava/lang/reflect/Field;");
|
|
|
|
Class_getName = env->GetMethodID(bclass, "getName", "()Ljava/lang/String;");
|
2022-05-30 23:13:49 +02:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
bclass = env->FindClass("java/lang/reflect/Method");
|
2017-03-05 16:44:50 +01:00
|
|
|
getParameterTypes = env->GetMethodID(bclass, "getParameterTypes", "()[Ljava/lang/Class;");
|
|
|
|
getReturnType = env->GetMethodID(bclass, "getReturnType", "()Ljava/lang/Class;");
|
|
|
|
getName = env->GetMethodID(bclass, "getName", "()Ljava/lang/String;");
|
|
|
|
getModifiers = env->GetMethodID(bclass, "getModifiers", "()I");
|
2022-05-30 23:13:49 +02:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
bclass = env->FindClass("java/lang/reflect/Field");
|
2017-03-05 16:44:50 +01:00
|
|
|
Field_getName = env->GetMethodID(bclass, "getName", "()Ljava/lang/String;");
|
|
|
|
Field_getModifiers = env->GetMethodID(bclass, "getModifiers", "()I");
|
|
|
|
Field_get = env->GetMethodID(bclass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
|
2022-05-30 23:13:49 +02:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
bclass = env->FindClass("java/lang/Boolean");
|
|
|
|
Boolean_booleanValue = env->GetMethodID(bclass, "booleanValue", "()Z");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Byte");
|
|
|
|
Byte_byteValue = env->GetMethodID(bclass, "byteValue", "()B");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Character");
|
|
|
|
Character_characterValue = env->GetMethodID(bclass, "charValue", "()C");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Short");
|
|
|
|
Short_shortValue = env->GetMethodID(bclass, "shortValue", "()S");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Integer");
|
|
|
|
Integer_integerValue = env->GetMethodID(bclass, "intValue", "()I");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Long");
|
|
|
|
Long_longValue = env->GetMethodID(bclass, "longValue", "()J");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Float");
|
|
|
|
Float_floatValue = env->GetMethodID(bclass, "floatValue", "()F");
|
|
|
|
|
|
|
|
bclass = env->FindClass("java/lang/Double");
|
|
|
|
Double_doubleValue = env->GetMethodID(bclass, "doubleValue", "()D");
|
|
|
|
}
|