This repository was archived by the owner on Nov 16, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcommon.cpp
More file actions
executable file
·121 lines (108 loc) · 3.86 KB
/
Copy pathcommon.cpp
File metadata and controls
executable file
·121 lines (108 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache 2.0 license.
// Please see LICENSE file in the project root for terms.
#include "common.hpp"
#include <glog/logging.h>
bool SetNativeAddress(JNIEnv *env, jobject object, void* address) {
if (object == NULL) {
LOG(ERROR) << "object is NULL";
return false;
}
/* Get a reference to JVM object class */
jclass claz = env->GetObjectClass(object);
if (claz == NULL || env->ExceptionCheck()) {
LOG(ERROR) << "unable to get object's class";
return false;
}
/* Locate init(long) method */
jmethodID methodId = env->GetMethodID(claz, "init", "(J)V");
if (methodId == NULL || env->ExceptionCheck()) {
LOG(ERROR) << "could not locate init() method";
return false;
}
/* associate native object with JVM object */
env->CallVoidMethod(object, methodId, (long)address);
if (env->ExceptionCheck()) {
LOG(ERROR) << "CallVoidMethod failed";
return false;
}
return true;
}
void* GetNativeAddress(JNIEnv *env, jobject object) {
if (object == NULL) {
LOG(ERROR) << "object is NULL";
return 0;
}
/* Get a reference to JVM object class */
jclass claz = env->GetObjectClass(object);
if (claz == NULL || env->ExceptionCheck()) {
LOG(ERROR) << "unable to get object's class";
return 0;
}
/* Getting the field id in the class */
jfieldID fieldId = env->GetFieldID(claz, "address", "J");
if (fieldId == NULL || env->ExceptionCheck()) {
LOG(ERROR) << "could not locate field 'address'";
return 0;
}
return (void*) env->GetLongField(object, fieldId);
}
bool GetStringVector(vector<const char*>& vec, JNIEnv *env, jobjectArray array, int length) {
for (int i = 0; i < length; i++) {
jstring addr = (jstring)env->GetObjectArrayElement(array, i);
if (addr == NULL || env->ExceptionCheck()) {
LOG(INFO) << i << "-th string is NULL";
vec[i] = NULL;
} else {
const char *cStr = env->GetStringUTFChars(addr, NULL);
if (env->ExceptionCheck()) {
LOG(ERROR) << "GetStringUTFChars failed";
return false;
}
vec[i] = cStr;
//Too many local refs could get created due to the loop, so delete them
//CHECKME:cStr is also a local ref called in loop, but it's not clear if deleting it via DeleteLocalRef deletes the memory pointed by it too
env->DeleteLocalRef(addr);
}
}
return true;
}
bool GetFloatBlobVector(vector< Blob<float>* >& vec, JNIEnv *env, jobjectArray array, int length) {
if (array == NULL) {
LOG(ERROR) << "array is NULL";
return false;
}
for (int i = 0; i < length; i++) {
//get i-th FloatBlob object (JVM)
jobject object = env->GetObjectArrayElement(array, i);
if (object == NULL || env->ExceptionCheck()) {
LOG(WARNING) << i << "-th FloatBlob is NULL";
vec[i] = NULL;
} else {
try{
//find the native Blob<float> object
vec[i] = (Blob<float>*) GetNativeAddress(env, object);
} catch (const std::exception& ex) {
ThrowJavaException(ex, env);
return false;
}
}
//Too many local refs could get created due to the loop, so delete them
env->DeleteLocalRef(object);
if (env->ExceptionCheck()) {
LOG(ERROR) << "DeleteLocalRef failed";
return false;
}
}
return true;
}
void ThrowJavaException(const std::exception& ex, JNIEnv *env) {
char exMsg[sizeof(typeid(ex).name())+sizeof(ex.what())+3];
sprintf(exMsg, "%s : %s", typeid(ex).name(), ex.what());
jclass exClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exClass, exMsg);
}
void ThrowCosJavaException(char* message, JNIEnv *env) {
jclass exClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exClass, message);
}