Skip to content

Commit 985d2f7

Browse files
Merge pull request #58 from dart-native/feature/android_string
Feature/android string
2 parents 1773fe7 + e5d6733 commit 985d2f7

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

dart_native/android/src/main/jni/dart_native.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
#include "dn_method_call.h"
1010
#include "dn_signature_helper.h"
1111
#include "dn_callback.h"
12+
#include "jni_object_ref.h"
1213

1314
extern "C"
1415
{
1516

1617
static JavaVM *gJvm = nullptr;
17-
static jobject gClassLoader;
18+
static JavaGlobalRef<jobject> *gClassLoader = nullptr;
1819
static jmethodID gFindClassMethod;
1920
static pthread_key_t detachKey = 0;
2021

2122
/// for invoke result compare
22-
static jclass gStrCls;
23+
static JavaGlobalRef<jclass> *gStrCls = nullptr;
2324

24-
/// key is jobject, value is pai which contain jclass and reference count
25+
/// key is jobject, value is pair which contain jclass and reference count
2526
static std::map<jobject, std::pair<jclass, int> > objectGlobalReference;
2627

2728
/// protect objectGlobalReference
@@ -92,13 +93,13 @@ extern "C"
9293
auto getClassLoaderMethod = env->GetMethodID(pluginClass, "getClassLoader",
9394
"()Ljava/lang/ClassLoader;");
9495
auto classLoader = env->CallObjectMethod(plugin, getClassLoaderMethod);
95-
gClassLoader = env->NewGlobalRef(classLoader);
96+
gClassLoader = new JavaGlobalRef<jobject>(env->NewGlobalRef(classLoader), env);
9697
gFindClassMethod = env->GetMethodID(classLoaderClass, "findClass",
9798
"(Ljava/lang/String;)Ljava/lang/Class;");
9899

99100
/// cache string class
100101
jclass strCls = env->FindClass("java/lang/String");
101-
gStrCls = static_cast<jclass>(env->NewGlobalRef(strCls));
102+
gStrCls = new JavaGlobalRef<jclass>(static_cast<jclass>(env->NewGlobalRef(strCls)), env);
102103

103104
env->DeleteLocalRef(classLoader);
104105
env->DeleteLocalRef(plugin);
@@ -119,7 +120,7 @@ extern "C"
119120
{
120121
env->ExceptionClear();
121122
DNDebug("findClass exception");
122-
return static_cast<jclass>(env->CallObjectMethod(gClassLoader,
123+
return static_cast<jclass>(env->CallObjectMethod(gClassLoader->Object(),
123124
gFindClassMethod,
124125
env->NewStringUTF(name)));
125126
}
@@ -227,7 +228,7 @@ extern "C"
227228
jobject obj = env->CallObjectMethodA(object, method, argValues);
228229
if (obj != nullptr)
229230
{
230-
if (env->IsInstanceOf(obj, gStrCls))
231+
if (env->IsInstanceOf(obj, gStrCls->Object()))
231232
{
232233
/// mark the last pointer as string
233234
/// dart will check this pointer

dart_native/android/src/main/jni/dn_method_call.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,3 @@ void *callNativeStringMethod(JNIEnv *env, jobject object, jmethodID methodId, jv
7373
auto javaString = (jstring)env->CallObjectMethodA(object, methodId, arguments);
7474
return convertToDartUtf16(env, javaString);
7575
}
76-
77-
void *callNativeObjectMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments)
78-
{
79-
}

dart_native/android/src/main/jni/dn_method_call.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extern "C"
2727
void *callNativeVoidMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments);
2828

2929
void *callNativeStringMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments);
30-
void *callNativeObjectMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments);
3130

3231
const std::map<char, std::function<CallNativeMethod> > methodCallerMap = {
3332
{'C', callNativeCharMethod},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Created by Hui on 6/9/21.
3+
//
4+
5+
#ifndef DART_NATIVE_JNI_OBJECT_REF_H
6+
#define DART_NATIVE_JNI_OBJECT_REF_H
7+
8+
#include <jni.h>
9+
10+
/**
11+
* Global reference will auto delete in destructor
12+
*/
13+
template <typename T>
14+
class JavaGlobalRef {
15+
public:
16+
explicit JavaGlobalRef(T t, JNIEnv *env): env(env) {
17+
this->obj = env->NewGlobalRef(t);
18+
}
19+
20+
JavaGlobalRef() = delete;
21+
22+
T Object() const { return static_cast<T>(obj); }
23+
24+
~JavaGlobalRef() { this->DeleteGlobalRef(); }
25+
26+
protected:
27+
jobject obj = nullptr;
28+
29+
private:
30+
JNIEnv *env;
31+
32+
void DeleteGlobalRef() {
33+
if (this->obj) {
34+
env->DeleteGlobalRef(this->obj);
35+
this->obj = nullptr;
36+
}
37+
}
38+
};
39+
40+
#endif //DART_NATIVE_JNI_OBJECT_REF_H

0 commit comments

Comments
 (0)