Convert Java exceptions to C++ exceptions

This commit is contained in:
Guillaume Meunier
2025-12-02 19:57:11 +01:00
parent e51d653bd7
commit 3621404e71
2 changed files with 76 additions and 4 deletions
+36
View File
@@ -0,0 +1,36 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "jnipp.h"
namespace jni::details
{
void handle_java_exception()
{
auto & env = jni_thread::env();
if (auto exc = env.ExceptionOccurred())
{
env.ExceptionClear();
std::string str = object<"java/lang/Object">{exc}.call<jni::string>("toString");
throw std::runtime_error("Java exception " + str);
}
}
} // namespace jni::details
+40 -4
View File
@@ -25,6 +25,7 @@
#include <tuple>
#include <jni.h>
#include <type_traits>
namespace jni
{
@@ -171,6 +172,9 @@ struct type_map<int>
template <typename T>
using type_map_t = type_map<T>::type;
void handle_java_exception();
} // namespace details
struct klass
@@ -215,7 +219,17 @@ struct klass
assert(method_id);
auto handles = details::handle(std::forward<Args>(args)...);
return R(std::apply([&](auto &... t) {
return (env.*R1::call_static_method)(*this, method_id, t...);
if constexpr (std::is_void_v<R>)
{
(env.*R1::call_static_method)(*this, method_id, t...);
details::handle_java_exception();
}
else
{
auto res = (env.*R1::call_static_method)(*this, method_id, t...);
details::handle_java_exception();
return res;
}
},
handles));
}
@@ -283,7 +297,17 @@ struct object
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...);
if constexpr (std::is_void_v<R>)
{
(env.*R1::call_method)(*this, method_id, t...);
details::handle_java_exception();
}
else
{
auto res = (env.*R1::call_method)(*this, method_id, t...);
details::handle_java_exception();
return res;
}
},
handles));
}
@@ -296,7 +320,17 @@ struct object
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...);
if constexpr (std::is_void_v<R>)
{
(env.*R1::call_method)(*this, method_id, t...);
details::handle_java_exception();
}
else
{
auto res = (env.*R1::call_method)(*this, method_id, t...);
details::handle_java_exception();
return res;
}
},
handles));
}
@@ -327,7 +361,9 @@ static object<Type> new_object(Args &&... args)
assert(method_id);
auto handles = details::handle(std::forward<Args>(args)...);
return object<Type>(std::apply([&](auto &... t) {
return env.NewObject(klass, method_id, t...);
auto res = env.NewObject(klass, method_id, t...);
details::handle_java_exception();
return res;
},
handles));
}