From 663906963a5884bc08a18cdd8389f170948b5c98 Mon Sep 17 00:00:00 2001 From: LiuYafei Date: Tue, 22 Jun 2021 11:45:39 +0800 Subject: [PATCH 1/2] Make Java common used exceptions public for users. Some Java codes take an exception as a paramter (e.g. `public void callFailed(Call call, IOException ioe)` in okhttp) Make JXXExceptions public to save time. --- cxx/fbjni/detail/CoreClasses.h | 30 ++++++++++++++++++ cxx/fbjni/detail/Exceptions.cpp | 56 ++++++++++----------------------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/cxx/fbjni/detail/CoreClasses.h b/cxx/fbjni/detail/CoreClasses.h index 551f766..7cc599b 100644 --- a/cxx/fbjni/detail/CoreClasses.h +++ b/cxx/fbjni/detail/CoreClasses.h @@ -607,6 +607,36 @@ class JThrowable : public JavaClass { void setStackTrace(alias_ref>); }; +class JRuntimeException : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/lang/RuntimeException;"; + + static local_ref create(const char* str); + + static local_ref create(); +}; + +class JIOException : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/io/IOException;"; + + static local_ref create(const char* str); +}; + +class JOutOfMemoryError : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/lang/OutOfMemoryError;"; + + static local_ref create(const char* str); +}; + +class JArrayIndexOutOfBoundsException : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/lang/ArrayIndexOutOfBoundsException;"; + + static local_ref create(const char* str); +}; + #pragma push_macro("PlainJniRefMap") #undef PlainJniRefMap #define PlainJniRefMap(rtype, jtype) \ diff --git a/cxx/fbjni/detail/Exceptions.cpp b/cxx/fbjni/detail/Exceptions.cpp index c697adb..6fd622b 100644 --- a/cxx/fbjni/detail/Exceptions.cpp +++ b/cxx/fbjni/detail/Exceptions.cpp @@ -39,46 +39,6 @@ namespace facebook { namespace jni { namespace { -class JRuntimeException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/lang/RuntimeException;"; - - static local_ref create(const char* str) { - return newInstance(make_jstring(str)); - } - - static local_ref create() { - return newInstance(); - } -}; - -class JIOException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/io/IOException;"; - - static local_ref create(const char* str) { - return newInstance(make_jstring(str)); - } -}; - -class JOutOfMemoryError : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/lang/OutOfMemoryError;"; - - static local_ref create(const char* str) { - return newInstance(make_jstring(str)); - } -}; - -class JArrayIndexOutOfBoundsException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/lang/ArrayIndexOutOfBoundsException;"; - - static local_ref create(const char* str) { - return newInstance(make_jstring(str)); - } -}; - class JUnknownCppException : public JavaClass { public: static auto constexpr kJavaDescriptor = "Lcom/facebook/jni/UnknownCppException;"; @@ -186,6 +146,22 @@ void JThrowable::setStackTrace(alias_ref stack) { return meth(self(), stack); } +local_ref JRuntimeException::create(const char *str) { + return newInstance(make_jstring(str)); +} +local_ref JRuntimeException::create() { + return newInstance(); +} +local_ref JIOException::create(const char *str) { + return newInstance(make_jstring(str)); +} +local_ref JOutOfMemoryError::create(const char *str) { + return newInstance(make_jstring(str)); +} +local_ref JArrayIndexOutOfBoundsException::create(const char *str) { + return newInstance(make_jstring(str)); +} + auto JStackTraceElement::create( const std::string& declaringClass, const std::string& methodName, const std::string& file, int line) -> local_ref { From 164ed567c7e4c3439f22220536083b9cf54bd012 Mon Sep 17 00:00:00 2001 From: liuyafei Date: Fri, 30 Jul 2021 11:18:31 +0800 Subject: [PATCH 2/2] Add JExcetion and let all other excetions extended from it --- cxx/fbjni/detail/CoreClasses.h | 30 ------------------------- cxx/fbjni/detail/Exceptions.cpp | 8 +++++++ cxx/fbjni/detail/Exceptions.h | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/cxx/fbjni/detail/CoreClasses.h b/cxx/fbjni/detail/CoreClasses.h index 7cc599b..551f766 100644 --- a/cxx/fbjni/detail/CoreClasses.h +++ b/cxx/fbjni/detail/CoreClasses.h @@ -607,36 +607,6 @@ class JThrowable : public JavaClass { void setStackTrace(alias_ref>); }; -class JRuntimeException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/lang/RuntimeException;"; - - static local_ref create(const char* str); - - static local_ref create(); -}; - -class JIOException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/io/IOException;"; - - static local_ref create(const char* str); -}; - -class JOutOfMemoryError : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/lang/OutOfMemoryError;"; - - static local_ref create(const char* str); -}; - -class JArrayIndexOutOfBoundsException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Ljava/lang/ArrayIndexOutOfBoundsException;"; - - static local_ref create(const char* str); -}; - #pragma push_macro("PlainJniRefMap") #undef PlainJniRefMap #define PlainJniRefMap(rtype, jtype) \ diff --git a/cxx/fbjni/detail/Exceptions.cpp b/cxx/fbjni/detail/Exceptions.cpp index 6fd622b..2e6d41b 100644 --- a/cxx/fbjni/detail/Exceptions.cpp +++ b/cxx/fbjni/detail/Exceptions.cpp @@ -146,6 +146,14 @@ void JThrowable::setStackTrace(alias_ref stack) { return meth(self(), stack); } +local_ref JException::create(const char *str) { + return newInstance(make_jstring(str)); +} + +local_ref JException::create() { + return newInstance(); +} + local_ref JRuntimeException::create(const char *str) { return newInstance(make_jstring(str)); } diff --git a/cxx/fbjni/detail/Exceptions.h b/cxx/fbjni/detail/Exceptions.h index 9fbdd47..872918e 100644 --- a/cxx/fbjni/detail/Exceptions.h +++ b/cxx/fbjni/detail/Exceptions.h @@ -46,6 +46,46 @@ namespace jni { class JThrowable; +class JException : public JavaClass { + public: + static constexpr const char* kJavaDescriptor = "Ljava/lang/Exception;"; + + static local_ref create(const char* str); + + static local_ref create(); +}; + +class JRuntimeException : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/lang/RuntimeException;"; + + static local_ref create(const char* str); + + static local_ref create(); +}; + +class JIOException : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/io/IOException;"; + + static local_ref create(const char* str); +}; + +class JOutOfMemoryError : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/lang/OutOfMemoryError;"; + + static local_ref create(const char* str); +}; + +class JArrayIndexOutOfBoundsException + : public JavaClass { + public: + static auto constexpr kJavaDescriptor = "Ljava/lang/ArrayIndexOutOfBoundsException;"; + + static local_ref create(const char* str); +}; + class JCppException : public JavaClass { public: static auto constexpr kJavaDescriptor = "Lcom/facebook/jni/CppException;";