Revert "Always cache JNI method id"

This reverts commit 0a1a3d2823.
This commit is contained in:
Patrick Nicolas
2024-09-01 17:20:16 +02:00
parent 30b82bb3b2
commit 71f22135ef
2 changed files with 36 additions and 28 deletions
+28 -24
View File
@@ -139,17 +139,6 @@ using type_map_t = type_map<T>::type;
struct klass
{
private:
template <typename R, typename... Args>
jmethodID get_method_id(const char * method, Args &&... args)
{
using R1 = details::type_map_t<R>;
auto & env = jni_thread::env();
std::string signature = "(" + details::build_type(args...) + ")" + R1::type();
return env.GetStaticMethodID(self.get(), method, signature.c_str());
}
public:
std::unique_ptr<std::remove_pointer_t<jclass>, details::deleter> self;
operator jclass()
@@ -184,7 +173,8 @@ public:
{
using R1 = details::type_map_t<R>;
auto & env = jni_thread::env();
static jmethodID method_id = get_method_id<R>(method, args...);
std::string signature = "(" + details::build_type(args...) + ")" + R1::type();
auto method_id = env.GetStaticMethodID(self.get(), method, signature.c_str());
assert(method_id);
auto handles = details::handle(std::forward<Args>(args)...);
return R(std::apply([&](auto &... t) {
@@ -192,6 +182,17 @@ public:
},
handles));
}
template <typename R, typename... Args>
auto method(const char * method, Args &&... args)
{
using R1 = details::type_map_t<R>;
auto & env = jni_thread::env();
std::string signature = "(" + details::build_type(args...) + ")" + R1::type();
auto method_id = env.GetMethodID(self.get(), method, signature.c_str());
assert(method_id);
return method_id;
}
};
struct Bool
@@ -243,17 +244,6 @@ struct Int
template <details::string_literal Type>
struct object
{
private:
template <typename R, typename... Args>
jmethodID get_method_id(const char * method, Args &&... args)
{
using R1 = details::type_map_t<R>;
auto & env = jni_thread::env();
std::string signature = "(" + details::build_type(args...) + ")" + R1::type();
return env.GetMethodID(klass(), method, signature.c_str());
}
public:
static std::string type()
{
return std::string("L") + Type.value + ";";
@@ -275,7 +265,21 @@ public:
{
using R1 = details::type_map_t<R>;
auto & env = jni_thread::env();
static auto method_id = get_method_id<R>(method, std::forward<Args>(args)...);
std::string signature = "(" + details::build_type(args...) + ")" + R1::type();
auto method_id = env.GetMethodID(klass(), method, signature.c_str());
assert(method_id);
auto handles = details::handle(std::forward<Args>(args)...);
return R(std::apply([&](auto &... t) {
return (env.*R1::call_method)(*this, method_id, t...);
},
handles));
}
template <typename R, typename... Args>
auto call(jmethodID method_id, Args &&... args)
{
using R1 = details::type_map_t<R>;
auto & env = jni_thread::env();
assert(method_id);
auto handles = details::handle(std::forward<Args>(args)...);
return R(std::apply([&](auto &... t) {