5#include "flutter/fml/platform/android/jni_util.h"
12#include "flutter/fml/logging.h"
13#include "flutter/fml/string_conversion.h"
20#define ASSERT_NO_EXCEPTION() FML_CHECK(env->ExceptionCheck() == JNI_FALSE);
36 <<
"Trying to attach to current thread without calling InitJavaVM first.";
38 JNIEnv*
env =
nullptr;
39 if (
g_jvm->GetEnv(
reinterpret_cast<void**
>(&
env), JNI_VERSION_1_4) ==
44 JavaVMAttachArgs
args;
45 args.version = JNI_VERSION_1_4;
49 int err = prctl(PR_GET_NAME, thread_name);
53 args.name = thread_name;
55 [[maybe_unused]] jint ret =
g_jvm->AttachCurrentThread(&
env, &
args);
66 g_jvm->DetachCurrentThread();
71 if (
env ==
nullptr || str ==
nullptr) {
74 const jchar* chars =
env->GetStringChars(str, NULL);
75 if (chars ==
nullptr) {
78 std::u16string u16_string(
reinterpret_cast<const char16_t*
>(chars),
79 env->GetStringLength(str));
81 env->ReleaseStringChars(str, chars);
87 const std::string& u8_string) {
90 env,
env->NewString(
reinterpret_cast<const jchar*
>(u16_string.data()),
91 u16_string.length()));
97 std::vector<std::string>
out;
98 if (
env ==
nullptr || array ==
nullptr) {
102 jsize
length =
env->GetArrayLength(array);
111 env,
static_cast<jstring
>(
env->GetObjectArrayElement(array,
i)));
119 std::vector<std::string>
out;
120 if (
env ==
nullptr || list ==
nullptr) {
128 env->GetMethodID(list_clazz.
obj(),
"get",
"(I)Ljava/lang/Object;");
129 jmethodID list_size =
env->GetMethodID(list_clazz.
obj(),
"size",
"()I");
131 jint
size =
env->CallIntMethod(list, list_size);
138 for (jint
i = 0;
i <
size; ++
i) {
140 env,
static_cast<jstring
>(
env->CallObjectMethod(list, list_get,
i)));
149 const std::vector<std::string>& vector) {
152 env->FindClass(
"java/lang/String"));
154 jobjectArray java_array =
155 env->NewObjectArray(vector.size(), string_clazz.
obj(), NULL);
157 for (
size_t i = 0;
i < vector.size(); ++
i) {
159 env->SetObjectArrayElement(java_array,
i, item.
obj());
166 const std::vector<std::vector<uint8_t>>& vector) {
169 env,
env->FindClass(
"java/nio/ByteBuffer"));
171 jobjectArray java_array =
172 env->NewObjectArray(vector.size(), byte_buffer_clazz.
obj(), NULL);
174 for (
size_t i = 0;
i < vector.size(); ++
i) {
175 uint8_t*
data =
const_cast<uint8_t*
>(vector[
i].data());
177 env,
env->NewDirectByteBuffer(
reinterpret_cast<void*
>(
data),
179 env->SetObjectArrayElement(java_array,
i, item.
obj());
185 return env->ExceptionCheck() != JNI_FALSE;
193 env->ExceptionDescribe();
195 env->ExceptionClear();
204 jthrowable exception =
env->ExceptionOccurred();
205 env->ExceptionClear();
207 env->DeleteLocalRef(exception);
213 env,
env->FindClass(
"java/lang/Throwable"));
214 jmethodID throwable_printstacktrace =
env->GetMethodID(
215 throwable_clazz.
obj(),
"printStackTrace",
"(Ljava/io/PrintStream;)V");
219 env,
env->FindClass(
"java/io/ByteArrayOutputStream"));
220 jmethodID bytearray_output_stream_constructor =
221 env->GetMethodID(bytearray_output_stream_clazz.
obj(),
"<init>",
"()V");
222 jmethodID bytearray_output_stream_tostring =
env->GetMethodID(
223 bytearray_output_stream_clazz.
obj(),
"toString",
"()Ljava/lang/String;");
225 env,
env->NewObject(bytearray_output_stream_clazz.
obj(),
226 bytearray_output_stream_constructor));
230 env,
env->FindClass(
"java/io/PrintStream"));
231 jmethodID printstream_constructor =
env->GetMethodID(
232 printstream_clazz.
obj(),
"<init>",
"(Ljava/io/OutputStream;)V");
234 env,
env->NewObject(printstream_clazz.
obj(), printstream_constructor,
235 bytearray_output_stream.
obj()));
238 env->CallVoidMethod(java_throwable, throwable_printstacktrace,
244 static_cast<jstring
>(
env->CallObjectMethod(
245 bytearray_output_stream.
obj(), bytearray_output_stream_tostring)));
247 return "Java OOM'd in exception handling, check logcat";
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_LOG(severity)
#define FML_DCHECK(condition)
#define ASSERT_NO_EXCEPTION()
JNIEnv * AttachCurrentThread()
ScopedJavaLocalRef< jobjectArray > VectorToBufferArray(JNIEnv *env, const std::vector< std::vector< uint8_t > > &vector)
std::string JavaStringToString(JNIEnv *env, jstring str)
bool ClearException(JNIEnv *env, bool silent)
static thread_local std::unique_ptr< JNIDetach > tls_jni_detach
bool CheckException(JNIEnv *env)
std::string GetJavaExceptionInfo(JNIEnv *env, jthrowable java_throwable)
ScopedJavaLocalRef< jobjectArray > VectorToStringArray(JNIEnv *env, const std::vector< std::string > &vector)
std::vector< std::string > StringListToVector(JNIEnv *env, jobject list)
bool HasException(JNIEnv *env)
void InitJavaVM(JavaVM *vm)
std::vector< std::string > StringArrayToVector(JNIEnv *env, jobjectArray array)
ScopedJavaLocalRef< jstring > StringToJavaString(JNIEnv *env, const std::string &u8_string)
std::string Utf16ToUtf8(const std::u16string_view string)
constexpr std::size_t size(T(&array)[N])
std::u16string Utf8ToUtf16(const std::string_view string)
std::shared_ptr< const fml::Mapping > data