1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #ifndef RCS_JNI_ENV_WRAPPER_H_
22 #define RCS_JNI_ENV_WRAPPER_H_
28 #include "JavaLocalRef.h"
30 class JavaException: public std::exception
37 JNIEnvWrapper() noexcept : m_env{ } {}
38 JNIEnvWrapper(JNIEnv* env) noexcept : m_env{ env } {}
40 JNIEnvWrapper& operator=(JNIEnv* env) noexcept
46 jboolean IsSameObject(jobject lhs, jobject rhs)
48 return m_env->IsSameObject(lhs, rhs);
51 jclass GetObjectClass(jobject obj)
53 auto ret = m_env->GetObjectClass(obj);
54 if (m_env->ExceptionCheck()) throw JavaException();
58 jclass FindClass(const char* name)
60 auto ret = m_env->FindClass(name);
61 if (m_env->ExceptionCheck()) throw JavaException();
65 jclass FindClassAsGlobalRef(const char* name)
67 JavaLocalClass cls{ m_env, FindClass(name) };
69 return NewGlobalRef(cls);
72 jobject NewGlobalRef(jobject obj)
74 auto ret = m_env->NewGlobalRef(obj);
75 if (m_env->ExceptionCheck()) throw JavaException();
82 auto ret = static_cast< T >(m_env->NewGlobalRef(obj));
83 if (m_env->ExceptionCheck()) throw JavaException();
88 T NewGlobalRef(const JavaLocalRef< T >& obj)
90 auto ret = static_cast< T >(m_env->NewGlobalRef(obj));
91 if (m_env->ExceptionCheck()) throw JavaException();
95 void DeleteGlobalRef(jobject obj)
97 m_env->DeleteGlobalRef(obj);
100 jobject NewObject(jclass cls, jmethodID ctor, ...)
103 va_start(args, ctor);
104 auto ret = m_env->NewObjectV(cls, ctor,args);
107 if (m_env->ExceptionCheck()) throw JavaException();
111 jstring NewStringUTF(const char* str)
113 auto ret = m_env->NewStringUTF(str);
114 if (m_env->ExceptionCheck()) throw JavaException();
118 const char* GetStringUTFChars(jstring str, jboolean* isCopy)
120 auto ret = m_env->GetStringUTFChars(str, isCopy);
121 if (m_env->ExceptionCheck()) throw JavaException();
125 void ReleaseStringUTFChars(jstring str, const char* chars)
127 m_env->ReleaseStringUTFChars(str, chars);
128 if (m_env->ExceptionCheck()) throw JavaException();
131 jmethodID GetConstructorID(jclass cls, const char* sig)
133 auto ret = m_env->GetMethodID(cls, "<init>", sig);
134 if (m_env->ExceptionCheck()) throw JavaException();
138 jmethodID GetMethodID(jclass cls, const char* name, const char* sig)
140 auto ret = m_env->GetMethodID(cls, name, sig);
141 if (m_env->ExceptionCheck()) throw JavaException();
145 jmethodID GetStaticMethodID(jclass cls, const char* name, const char* sig)
147 auto ret = m_env->GetStaticMethodID(cls, name, sig);
148 if (m_env->ExceptionCheck()) throw JavaException();
153 jfieldID GetFieldID(jclass cls, const char* name, const char* sig)
155 auto ret = m_env->GetFieldID(cls, name, sig);
156 if (m_env->ExceptionCheck()) throw JavaException();
160 jfieldID GetStaticFieldID(jclass cls, const char* name, const char* sig)
162 auto ret = m_env->GetStaticFieldID(cls, name, sig);
163 if (m_env->ExceptionCheck()) throw JavaException();
167 jobject GetStaticObjectField(jclass cls, jfieldID fieldId)
169 auto ret = m_env->GetStaticObjectField(cls, fieldId);
170 if (m_env->ExceptionCheck()) throw JavaException();
174 jobject GetStaticObjectField(jclass cls, const char* name, const char* sig)
176 return GetStaticObjectField(cls, GetStaticFieldID(cls, name, sig));
179 jint GetIntField(jobject obj, jfieldID fieldId)
181 auto ret = m_env->GetIntField(obj, fieldId);
182 if (m_env->ExceptionCheck()) throw JavaException();
186 jlong GetLongField(jobject obj, jfieldID fieldId)
188 auto ret = m_env->GetLongField(obj, fieldId);
189 if (m_env->ExceptionCheck()) throw JavaException();
193 void SetLongField(jobject obj, jfieldID fieldId, jlong val)
195 m_env->SetLongField(obj, fieldId, val);
196 if (m_env->ExceptionCheck()) throw JavaException();
199 jobject GetObjectField(jobject obj, jfieldID fieldId)
201 auto ret = m_env->GetObjectField(obj, fieldId);
202 if (m_env->ExceptionCheck()) throw JavaException();
206 jint CallStaticIntMethod(jclass cls, jmethodID methodId, ...)
209 va_start(args, methodId);
210 auto ret = m_env->CallStaticIntMethodV(cls, methodId, args);
212 if (m_env->ExceptionCheck()) throw JavaException();
216 jobject CallStaticObjectMethod(jclass cls, jmethodID methodId, ...)
219 va_start(args, methodId);
220 auto ret = m_env->CallStaticObjectMethodV(cls, methodId, args);
222 if (m_env->ExceptionCheck()) throw JavaException();
226 void CallVoidMethod(jobject obj, jmethodID methodId, ...)
229 va_start(args, methodId);
230 m_env->CallVoidMethodV(obj, methodId, args);
232 if (m_env->ExceptionCheck()) throw JavaException();
235 jboolean CallBooleanMethod(jobject obj, jmethodID methodId, ...)
238 va_start(args, methodId);
239 auto ret = m_env->CallBooleanMethodV(obj, methodId, args);
241 if (m_env->ExceptionCheck()) throw JavaException();
245 jint CallIntMethod(jobject obj, jmethodID methodId, ...)
248 va_start(args, methodId);
249 auto ret = m_env->CallIntMethod(obj, methodId, args);
251 if (m_env->ExceptionCheck()) throw JavaException();
255 jdouble CallDoubleMethod(jobject obj, jmethodID methodId, ...)
258 va_start(args ,methodId);
259 auto ret = m_env->CallDoubleMethod(obj, methodId, args);
261 if (m_env->ExceptionCheck()) throw JavaException();
266 jobject CallObjectMethod(jobject obj, jmethodID methodId, ...)
269 va_start(args, methodId);
270 auto ret = m_env->CallObjectMethodV(obj, methodId, args);
272 if (m_env->ExceptionCheck()) throw JavaException();
276 jbooleanArray NewBooleanArray(jsize len)
278 auto ret = m_env->NewBooleanArray(len);
279 if (m_env->ExceptionCheck()) throw JavaException();
283 jintArray NewIntArray(jsize len)
285 auto ret = m_env->NewIntArray(len);
286 if (m_env->ExceptionCheck()) throw JavaException();
290 jdoubleArray NewDoubleArray(jsize len)
292 auto ret = m_env->NewDoubleArray(len);
293 if (m_env->ExceptionCheck()) throw JavaException();
297 jobjectArray NewObjectArray(jsize len, jclass cls, jobject init)
299 auto ret = m_env->NewObjectArray(len, cls, init);
300 if (m_env->ExceptionCheck()) throw JavaException();
304 jsize GetArrayLength(jarray array)
306 auto ret = m_env->GetArrayLength(array);
307 if (m_env->ExceptionCheck()) throw JavaException();
311 jobject GetObjectArrayElement(jobjectArray array, jsize index)
313 auto ret = m_env->GetObjectArrayElement(array, index);
314 if (m_env->ExceptionCheck()) throw JavaException();
318 void SetObjectArrayElement(jobjectArray array, jsize index, jobject val)
320 m_env->SetObjectArrayElement(array, index, val);
321 if (m_env->ExceptionCheck()) throw JavaException();
324 void SetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, const jboolean* buf)
326 m_env->SetBooleanArrayRegion(array, start, len, buf);
327 if (m_env->ExceptionCheck()) throw JavaException();
330 void SetIntArrayRegion(jintArray array, jsize start, jsize len, const jint* buf)
332 m_env->SetIntArrayRegion(array, start, len, buf);
333 if (m_env->ExceptionCheck()) throw JavaException();
336 void SetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, const jdouble* buf)
338 m_env->SetDoubleArrayRegion(array, start, len, buf);
339 if (m_env->ExceptionCheck()) throw JavaException();
342 void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy)
344 auto ret = m_env->GetPrimitiveArrayCritical(array, isCopy);
345 if (m_env->ExceptionCheck()) throw JavaException();
349 void ReleasePrimitiveArrayCritical(jarray array, void* carray, int mode)
351 m_env->ReleasePrimitiveArrayCritical(array, carray, mode);
352 if (m_env->ExceptionCheck()) throw JavaException();
355 void ThrowNew(jclass cls, const char* msg) {
356 m_env->ThrowNew(cls, msg);
357 throw JavaException();
360 void ExceptionDescribe() const noexcept
362 m_env->ExceptionDescribe();
365 void ExceptionClear() noexcept
367 m_env->ExceptionClear();
370 jboolean ExceptionCheck() const noexcept
372 return m_env->ExceptionCheck();
375 operator bool() const noexcept
377 return m_env != nullptr;
391 #endif // RCS_JNI_ENV_WRAPPER_H_