Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
jniutil.cc
Go to the documentation of this file.
1 
19 
20 #include <memory>
21 
22 #include "ion/port/logging.h"
23 
24 namespace ion {
25 namespace port {
26 namespace android {
27 
28 JavaVM* s_jvm = NULL;
29 
30 JavaVM* GetJVM() {
31  return s_jvm;
32 }
33 
34 void SetJVM(JavaVM* jvm) {
35  s_jvm = jvm;
36 }
37 
41 static const std::unique_ptr<LogEntryWriter> log_writer(
43 
44 jclass FindClassGlobal(JNIEnv* env, const char* class_name) {
45  jclass clazz = env->FindClass(class_name);
46  jthrowable mException = env->ExceptionOccurred();
47  if (mException) {
48  env->ExceptionDescribe();
49  env->ExceptionClear();
50  log_writer->Write(ERROR,
51  std::string("Android JNI: Class ").
52  append(class_name).append(" not found.").c_str());
53  return NULL;
54  }
55  if (!clazz) {
56  log_writer->Write(ERROR,
57  std::string("Android JNI: Class ").
58  append(class_name).append(" not found.").c_str());
59  return NULL;
60  }
61  jclass global_clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
62  env->DeleteLocalRef(clazz);
63  return global_clazz;
64 }
65 
66 jmethodID GetStaticMethod(JNIEnv* env, jclass clazz, const char* class_name,
67  const char* name, const char* signature) {
68  jmethodID method = env->GetStaticMethodID(clazz, name, signature);
69  if (env->ExceptionCheck() || !method) {
70  log_writer->Write(ERROR,
71  std::string("Android JNI: static method ").
72  append(name).append(" not found in class.").
73  append(class_name).c_str());
74  env->ExceptionClear();
75  return NULL;
76  }
77  return method;
78 }
79 
80 jmethodID GetMethod(JNIEnv* env, jclass clazz, const char* class_name,
81  const char* name, const char* signature) {
82  jmethodID method = env->GetMethodID(clazz, name, signature);
83  if (env->ExceptionCheck() || !method) {
84  log_writer->Write(ERROR,
85  std::string("Android JNI: method ").
86  append(name).append(" not found in class ").
87  append(class_name).c_str());
88  env->ExceptionClear();
89  return NULL;
90  }
91  return method;
92 }
93 
95  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
96  return GetStaticMember(env, clazz, class_name, name, "Ljava/lang/String;");
97 }
98 
100  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
101  return GetStaticMember(env, clazz, class_name, name, "I");
102 }
103 
105  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
106  return GetStaticMember(env, clazz, class_name, name, "J");
107 }
108 
110  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
111  return GetStaticMember(env, clazz, class_name, name, "Z");
112 }
113 
114 jfieldID GetIntMember(
115  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
116  return GetMember(env, clazz, class_name, name, "I");
117 }
118 
119 jfieldID GetLongMember(
120  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
121  return GetMember(env, clazz, class_name, name, "J");
122 }
123 
125  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
126  return GetMember(env, clazz, class_name, name, "Z");
127 }
128 
130  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
131  return GetMember(env, clazz, class_name, name, "Ljava/lang/String;");
132 }
133 
135  JNIEnv* env, jclass clazz, const char* class_name, const char* name) {
136  return GetMember(env, clazz, class_name, name, "[I");
137 }
138 
139 jfieldID GetStaticMember(JNIEnv* env, jclass clazz, const char* class_name,
140  const char* name, const char* type) {
141  jfieldID field = env->GetStaticFieldID(clazz, name, type);
142  if (env->ExceptionCheck() || !field) {
143  log_writer->Write(ERROR,
144  std::string("Android JNI: static field ").
145  append(name).append(" not found in class ").
146  append(class_name).c_str());
147  env->ExceptionClear();
148  return NULL;
149  }
150  return field;
151 }
152 
153 jfieldID GetMember(JNIEnv* env, jclass clazz, const char* class_name,
154  const char* name, const char* type) {
155  jfieldID field = env->GetFieldID(clazz, name, type);
156  if (env->ExceptionCheck() || !field) {
157  log_writer->Write(ERROR,
158  std::string("Android JNI: field ").
159  append(name).append(" not found in class ").
160  append(class_name).c_str());
161  env->ExceptionClear();
162  return NULL;
163  }
164  return field;
165 }
166 
167 jobjectArray JavaStringArray(JNIEnv* env, int length) {
168  return env->NewObjectArray(length, env->FindClass("java/lang/String"),
169  env->NewStringUTF(""));
170 }
171 
172 jstring JavaString(JNIEnv* env, const std::string& s) {
173  return env->NewStringUTF(s.c_str());
174 }
175 
176 jbyteArray JavaByteArray(JNIEnv* env, const std::string& bytes) {
177  return JavaByteArray(env, reinterpret_cast<const jbyte*>(bytes.c_str()),
178  static_cast<jsize>(bytes.size()));
179 }
180 
181 jbyteArray JavaByteArray(JNIEnv* env, const jbyte* data, jsize size) {
182  if (size == 0) {
183  return NULL;
184  }
185  jbyteArray j_array = env->NewByteArray(size);
186  env->SetByteArrayRegion(j_array, 0, size, data);
187  return j_array;
188 }
189 
190 void JavaGetByteArray(JNIEnv* env, jbyteArray array,
191  jsize first, jsize size, char* out) {
192  if (array == NULL) {
193  return;
194  }
195 
196  if (!out) {
197  log_writer->Write(ERROR,
198  "Android JNI: JavaGetByteArray called without a valid out.");
199  return;
200  }
201 
202  env->GetByteArrayRegion(array, first, size,
203  reinterpret_cast<jbyte*>(out));
204 }
205 
206 LocalFrame::LocalFrame(JNIEnv* env) : env_(env) {
207  jint result = env_->PushLocalFrame(0);
208  if (result != JNI_OK) {
209  log_writer->Write(ERROR,
210  "Android JNI: Error on PushLocalFrame");
211  }
212 }
213 
215  env_->PopLocalFrame(NULL);
216 }
217 
218 } // namespace android
219 } // namespace port
220 } // namespace ion
void JavaGetByteArray(JNIEnv *env, jbyteArray array, jsize first, jsize size, char *out)
Definition: jniutil.cc:190
JavaVM * GetJVM()
Definition: jniutil.cc:30
jfieldID GetStaticStringMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:94
std::string type
Definition: printer.cc:353
jbyteArray JavaByteArray(JNIEnv *env, const std::string &bytes)
Definition: jniutil.cc:176
void SetJVM(JavaVM *jvm)
Set the pointer to the JavaVM.
Definition: jniutil.cc:34
jfieldID GetStaticLongMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:104
JavaVM * s_jvm
Definition: jniutil.cc:28
jfieldID GetStringMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:129
uint32 length
jfieldID GetIntMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:114
jclass FindClassGlobal(JNIEnv *env, const char *class_name)
Definition: jniutil.cc:44
jmethodID GetStaticMethod(JNIEnv *env, jclass clazz, const char *class_name, const char *name, const char *signature)
Definition: jniutil.cc:66
LocalFrame(JNIEnv *env)
Pushes a local reference frame frame with env->PushLocalFrame(0).
Definition: jniutil.cc:206
jstring JavaString(JNIEnv *env, const std::string &s)
Definition: jniutil.cc:172
jfieldID GetStaticBooleanMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:109
jfieldID GetMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name, const char *type)
Definition: jniutil.cc:153
std::string name
Definition: printer.cc:324
jobjectArray JavaStringArray(JNIEnv *env, int length)
Definition: jniutil.cc:167
jfieldID GetLongMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:119
jmethodID GetMethod(JNIEnv *env, jclass clazz, const char *class_name, const char *name, const char *signature)
Definition: jniutil.cc:80
jfieldID GetStaticMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name, const char *type)
Definition: jniutil.cc:139
jfieldID GetBooleanMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:124
jfieldID GetStaticIntMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:99
ION_API LogEntryWriter * CreateDefaultLogEntryWriter()
Instantiate a new LogEntryWriter of the default type for the current platform...
jfieldID GetIntArrayMember(JNIEnv *env, jclass clazz, const char *class_name, const char *name)
Definition: jniutil.cc:134
~LocalFrame()
Pops a local reference frame with env->PopLocalFrame(NULL);.
Definition: jniutil.cc:214