1
1
#include " jni_helper.h"
2
2
3
+ #include < cstdlib>
3
4
#include < malloc.h>
4
5
#include < cstring>
5
6
@@ -12,7 +13,7 @@ static jmethodID m_get_bytes;
12
13
void initialize_jni (JavaVM *vm, JNIEnv *env) {
13
14
global_vm = vm;
14
15
15
- c_string = ( jclass) new_global (find_class (" java/lang/String" ));
16
+ c_string = reinterpret_cast < jclass>( new_global (find_class (" java/lang/String" ) ));
16
17
m_new_string = find_method (c_string, " <init>" , " ([B)V" );
17
18
m_get_bytes = find_method (c_string, " getBytes" , " ()[B" );
18
19
}
@@ -22,33 +23,33 @@ JavaVM *global_java_vm() {
22
23
}
23
24
24
25
char *jni_get_string (JNIEnv *env, jstring str) {
25
- auto array = ( jbyteArray) env->CallObjectMethod (str, m_get_bytes);
26
- int length = env->GetArrayLength (array);
27
- char * content = ( char *) malloc (length + 1 );
28
- env->GetByteArrayRegion (array, 0 , length, ( jbyte *) content);
26
+ const auto array = reinterpret_cast < jbyteArray>( env->CallObjectMethod (str, m_get_bytes) );
27
+ const int length = env->GetArrayLength (array);
28
+ const auto content = static_cast < char *>( malloc (length + 1 ) );
29
+ env->GetByteArrayRegion (array, 0 , length, reinterpret_cast < jbyte *>( content) );
29
30
content[length] = 0 ;
30
31
return content;
31
32
}
32
33
33
34
jstring jni_new_string (JNIEnv *env, const char *str) {
34
- auto length = ( int ) strlen (str);
35
- jbyteArray array = env->NewByteArray (length);
36
- env->SetByteArrayRegion (array, 0 , length, ( const jbyte *) str);
37
- return ( jstring) env->NewObject (c_string, m_new_string, array);
35
+ const auto length = static_cast < int >( strlen (str) );
36
+ const auto array = env->NewByteArray (length);
37
+ env->SetByteArrayRegion (array, 0 , length, reinterpret_cast < const jbyte *>( str) );
38
+ return reinterpret_cast < jstring>( env->NewObject (c_string, m_new_string, array) );
38
39
}
39
40
40
41
int jni_catch_exception (JNIEnv *env) {
41
- int result = env->ExceptionCheck ();
42
+ const int result = env->ExceptionCheck ();
42
43
if (result) {
43
44
env->ExceptionDescribe ();
44
45
env->ExceptionClear ();
45
46
}
46
47
return result;
47
48
}
48
49
49
- void jni_attach_thread (struct scoped_jni *jni) {
50
+ void jni_attach_thread (scoped_jni *jni) {
50
51
JavaVM *vm = global_java_vm ();
51
- if (vm->GetEnv (( void **) &jni->env , JNI_VERSION_1_6) == JNI_OK) {
52
+ if (vm->GetEnv (reinterpret_cast < void **>( &jni->env ) , JNI_VERSION_1_6) == JNI_OK) {
52
53
jni->require_release = 0 ;
53
54
return ;
54
55
}
@@ -58,9 +59,9 @@ void jni_attach_thread(struct scoped_jni *jni) {
58
59
jni->require_release = 1 ;
59
60
}
60
61
61
- void jni_detach_thread (struct scoped_jni *jni ) {
62
+ void jni_detach_thread (const scoped_jni *env ) {
62
63
JavaVM *vm = global_java_vm ();
63
- if (jni ->require_release ) {
64
+ if (env ->require_release ) {
64
65
vm->DetachCurrentThread ();
65
66
}
66
67
}
0 commit comments