From 3621404e7147fccca132cefb5bffc8897c845330 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Mon, 1 Dec 2025 18:03:57 +0100 Subject: [PATCH] Convert Java exceptions to C++ exceptions --- client/android/jnipp.cpp | 36 ++++++++++++++++++++++++++++++++ client/android/jnipp.h | 44 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 client/android/jnipp.cpp diff --git a/client/android/jnipp.cpp b/client/android/jnipp.cpp new file mode 100644 index 00000000..abc9f49a --- /dev/null +++ b/client/android/jnipp.cpp @@ -0,0 +1,36 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * Copyright (C) 2025 Patrick Nicolas + * + * 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 . + */ + +#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("toString"); + + throw std::runtime_error("Java exception " + str); + } +} +} // namespace jni::details diff --git a/client/android/jnipp.h b/client/android/jnipp.h index d9fd8994..4c0ed55d 100644 --- a/client/android/jnipp.h +++ b/client/android/jnipp.h @@ -25,6 +25,7 @@ #include #include +#include namespace jni { @@ -171,6 +172,9 @@ struct type_map template using type_map_t = type_map::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)...); return R(std::apply([&](auto &... t) { - return (env.*R1::call_static_method)(*this, method_id, t...); + if constexpr (std::is_void_v) + { + (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)...); return R(std::apply([&](auto &... t) { - return (env.*R1::call_method)(*this, method_id, t...); + if constexpr (std::is_void_v) + { + (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)...); return R(std::apply([&](auto &... t) { - return (env.*R1::call_method)(*this, method_id, t...); + if constexpr (std::is_void_v) + { + (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 new_object(Args &&... args) assert(method_id); auto handles = details::handle(std::forward(args)...); return object(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)); }