replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / main / jni / util / JNIEnvWrapper.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #ifndef RCS_JNI_ENV_WRAPPER_H_
22 #define RCS_JNI_ENV_WRAPPER_H_
23
24 #include <jni.h>
25
26 #include <exception>
27
28 #include "JavaLocalRef.h"
29
30 class JavaException: public std::exception
31 {
32 };
33
34 class JNIEnvWrapper
35 {
36 public:
37     JNIEnvWrapper() noexcept : m_env{ } {}
38     JNIEnvWrapper(JNIEnv* env) noexcept : m_env{ env } {}
39
40     JNIEnvWrapper& operator=(JNIEnv* env) noexcept
41     {
42         m_env = env;
43         return *this;
44     }
45
46     jboolean IsSameObject(jobject lhs, jobject rhs)
47     {
48         return m_env->IsSameObject(lhs, rhs);
49     }
50
51     jclass GetObjectClass(jobject obj)
52     {
53         auto ret = m_env->GetObjectClass(obj);
54         if (m_env->ExceptionCheck()) throw JavaException();
55         return ret;
56     }
57
58     jclass FindClass(const char* name)
59     {
60         auto ret = m_env->FindClass(name);
61         if (m_env->ExceptionCheck()) throw JavaException();
62         return ret;
63     }
64
65     jclass FindClassAsGlobalRef(const char* name)
66     {
67         JavaLocalClass cls{ m_env, FindClass(name) };
68
69         return NewGlobalRef(cls);
70     }
71
72     jobject NewGlobalRef(jobject obj)
73     {
74         auto ret = m_env->NewGlobalRef(obj);
75         if (m_env->ExceptionCheck()) throw JavaException();
76         return ret;
77     }
78
79     template<typename T>
80     T NewGlobalRef(T obj)
81     {
82         auto ret = static_cast< T >(m_env->NewGlobalRef(obj));
83         if (m_env->ExceptionCheck()) throw JavaException();
84         return ret;
85     }
86
87     template<typename T>
88     T NewGlobalRef(const JavaLocalRef< T >& obj)
89     {
90         auto ret = static_cast< T >(m_env->NewGlobalRef(obj));
91         if (m_env->ExceptionCheck()) throw JavaException();
92         return ret;
93     }
94
95     void DeleteGlobalRef(jobject obj)
96     {
97         m_env->DeleteGlobalRef(obj);
98     }
99
100     jobject NewObject(jclass cls, jmethodID ctor, ...)
101     {
102         va_list args;
103         va_start(args, ctor);
104         auto ret = m_env->NewObjectV(cls, ctor,args);
105         va_end(args);
106
107         if (m_env->ExceptionCheck()) throw JavaException();
108         return ret;
109     }
110
111     jstring NewStringUTF(const char* str)
112     {
113         auto ret = m_env->NewStringUTF(str);
114         if (m_env->ExceptionCheck()) throw JavaException();
115         return ret;
116     }
117
118     const char* GetStringUTFChars(jstring str, jboolean* isCopy)
119     {
120         auto ret = m_env->GetStringUTFChars(str, isCopy);
121         if (m_env->ExceptionCheck()) throw JavaException();
122         return ret;
123     }
124
125     void ReleaseStringUTFChars(jstring str, const char* chars)
126     {
127         m_env->ReleaseStringUTFChars(str, chars);
128         if (m_env->ExceptionCheck()) throw JavaException();
129     }
130
131     jmethodID GetConstructorID(jclass cls, const char* sig)
132     {
133         auto ret = m_env->GetMethodID(cls, "<init>", sig);
134         if (m_env->ExceptionCheck()) throw JavaException();
135         return ret;
136     }
137
138     jmethodID GetMethodID(jclass cls, const char* name, const char* sig)
139     {
140         auto ret = m_env->GetMethodID(cls, name, sig);
141         if (m_env->ExceptionCheck()) throw JavaException();
142         return ret;
143     }
144
145     jmethodID GetStaticMethodID(jclass cls, const char* name, const char* sig)
146     {
147         auto ret = m_env->GetStaticMethodID(cls, name, sig);
148         if (m_env->ExceptionCheck()) throw JavaException();
149         return ret;
150     }
151
152
153     jfieldID GetFieldID(jclass cls, const char* name, const char* sig)
154     {
155         auto ret = m_env->GetFieldID(cls, name, sig);
156         if (m_env->ExceptionCheck()) throw JavaException();
157         return ret;
158     }
159
160     jfieldID GetStaticFieldID(jclass cls, const char* name, const char* sig)
161     {
162         auto ret = m_env->GetStaticFieldID(cls, name, sig);
163         if (m_env->ExceptionCheck()) throw JavaException();
164         return ret;
165     }
166
167     jobject GetStaticObjectField(jclass cls, jfieldID fieldId)
168     {
169         auto ret = m_env->GetStaticObjectField(cls, fieldId);
170         if (m_env->ExceptionCheck()) throw JavaException();
171         return ret;
172     }
173
174     jobject GetStaticObjectField(jclass cls, const char* name, const char* sig)
175     {
176         return GetStaticObjectField(cls, GetStaticFieldID(cls, name, sig));
177     }
178
179     jint GetIntField(jobject obj, jfieldID fieldId)
180     {
181         auto ret = m_env->GetIntField(obj, fieldId);
182         if (m_env->ExceptionCheck()) throw JavaException();
183         return ret;
184     }
185
186     jlong GetLongField(jobject obj, jfieldID fieldId)
187     {
188         auto ret = m_env->GetLongField(obj, fieldId);
189         if (m_env->ExceptionCheck()) throw JavaException();
190         return ret;
191     }
192
193     void SetLongField(jobject obj, jfieldID fieldId, jlong val)
194     {
195         m_env->SetLongField(obj, fieldId, val);
196         if (m_env->ExceptionCheck()) throw JavaException();
197     }
198
199     jobject GetObjectField(jobject obj, jfieldID fieldId)
200     {
201         auto ret = m_env->GetObjectField(obj, fieldId);
202         if (m_env->ExceptionCheck()) throw JavaException();
203         return ret;
204     }
205
206     jint CallStaticIntMethod(jclass cls, jmethodID methodId, ...)
207     {
208         va_list args;
209         va_start(args, methodId);
210         auto ret = m_env->CallStaticIntMethodV(cls, methodId, args);
211         va_end(args);
212         if (m_env->ExceptionCheck()) throw JavaException();
213         return ret;
214     }
215
216     jobject CallStaticObjectMethod(jclass cls, jmethodID methodId, ...)
217     {
218         va_list args;
219         va_start(args, methodId);
220         auto ret = m_env->CallStaticObjectMethodV(cls, methodId, args);
221         va_end(args);
222         if (m_env->ExceptionCheck()) throw JavaException();
223         return ret;
224     }
225
226     void CallVoidMethod(jobject obj, jmethodID methodId, ...)
227     {
228          va_list args;
229          va_start(args, methodId);
230          m_env->CallVoidMethodV(obj, methodId, args);
231          va_end(args);
232          if (m_env->ExceptionCheck()) throw JavaException();
233     }
234
235     jboolean CallBooleanMethod(jobject obj, jmethodID methodId, ...)
236     {
237         va_list args;
238         va_start(args, methodId);
239         auto ret = m_env->CallBooleanMethodV(obj, methodId, args);
240         va_end(args);
241         if (m_env->ExceptionCheck()) throw JavaException();
242         return ret;
243     }
244
245     jint CallIntMethod(jobject obj, jmethodID methodId, ...)
246     {
247          va_list args;
248          va_start(args, methodId);
249          auto ret = m_env->CallIntMethod(obj, methodId, args);
250          va_end(args);
251          if (m_env->ExceptionCheck()) throw JavaException();
252          return ret;
253     }
254
255      jdouble CallDoubleMethod(jobject obj, jmethodID methodId, ...)
256      {
257           va_list args;
258           va_start(args ,methodId);
259           auto ret = m_env->CallDoubleMethod(obj, methodId, args);
260           va_end(args);
261           if (m_env->ExceptionCheck()) throw JavaException();
262           return ret;
263      }
264
265
266     jobject CallObjectMethod(jobject obj, jmethodID methodId, ...)
267     {
268         va_list args;
269         va_start(args, methodId);
270         auto ret = m_env->CallObjectMethodV(obj, methodId, args);
271         va_end(args);
272         if (m_env->ExceptionCheck()) throw JavaException();
273         return ret;
274     }
275
276     jbooleanArray NewBooleanArray(jsize len)
277     {
278         auto ret = m_env->NewBooleanArray(len);
279         if (m_env->ExceptionCheck()) throw JavaException();
280         return ret;
281     }
282
283     jintArray NewIntArray(jsize len)
284     {
285         auto ret = m_env->NewIntArray(len);
286         if (m_env->ExceptionCheck()) throw JavaException();
287         return ret;
288     }
289
290     jdoubleArray NewDoubleArray(jsize len)
291     {
292         auto ret = m_env->NewDoubleArray(len);
293         if (m_env->ExceptionCheck()) throw JavaException();
294         return ret;
295     }
296
297     jbyteArray NewByteArray(jsize len)
298     {
299         auto ret = m_env->NewByteArray(len);
300         if (m_env->ExceptionCheck()) throw JavaException();
301         return ret;
302     }
303
304   jobjectArray NewObjectArray(jsize len, jclass cls, jobject init)
305     {
306         auto ret = m_env->NewObjectArray(len, cls, init);
307         if (m_env->ExceptionCheck()) throw JavaException();
308         return ret;
309     }
310
311     jsize GetArrayLength(jarray array)
312     {
313         auto ret = m_env->GetArrayLength(array);
314         if (m_env->ExceptionCheck()) throw JavaException();
315         return ret;
316     }
317
318     jobject GetObjectArrayElement(jobjectArray array, jsize index)
319     {
320         auto ret = m_env->GetObjectArrayElement(array, index);
321         if (m_env->ExceptionCheck()) throw JavaException();
322         return ret;
323     }
324
325     jbyte *GetByteArrayElements(jbyteArray array, jboolean *value)
326     {
327         auto ret = m_env->GetByteArrayElements(array, value);
328         if (m_env->ExceptionCheck()) throw JavaException();
329         return ret;
330     }
331
332     void SetObjectArrayElement(jobjectArray array, jsize index, jobject val)
333     {
334         m_env->SetObjectArrayElement(array, index, val);
335         if (m_env->ExceptionCheck()) throw JavaException();
336     }
337
338     void SetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, const jboolean* buf)
339     {
340         m_env->SetBooleanArrayRegion(array, start, len, buf);
341         if (m_env->ExceptionCheck()) throw JavaException();
342     }
343
344     void SetIntArrayRegion(jintArray array, jsize start, jsize len, const jint* buf)
345     {
346         m_env->SetIntArrayRegion(array, start, len, buf);
347         if (m_env->ExceptionCheck()) throw JavaException();
348     }
349
350     void SetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, const jdouble* buf)
351     {
352         m_env->SetDoubleArrayRegion(array, start, len, buf);
353         if (m_env->ExceptionCheck()) throw JavaException();
354     }
355
356     void SetByteArrayRegion(jbyteArray array, jsize start, jsize len, const jbyte *buf)
357     {
358         m_env->SetByteArrayRegion(array, start, len, buf);
359         if (m_env->ExceptionCheck()) throw JavaException();
360     }
361
362     void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy)
363     {
364         auto ret = m_env->GetPrimitiveArrayCritical(array, isCopy);
365         if (m_env->ExceptionCheck()) throw JavaException();
366         return ret;
367     }
368
369     void ReleasePrimitiveArrayCritical(jarray array, void* carray, int mode)
370     {
371         m_env->ReleasePrimitiveArrayCritical(array, carray, mode);
372         if (m_env->ExceptionCheck()) throw JavaException();
373     }
374
375     void ReleaseByteArrayElements(jbyteArray array, jbyte* byteArray, int mode)
376     {
377         m_env->ReleaseByteArrayElements(array, byteArray, mode);
378         if (m_env->ExceptionCheck()) throw JavaException();
379     }
380
381     void ThrowNew(jclass cls, const char* msg)
382     {
383         m_env->ThrowNew(cls, msg);
384         throw JavaException();
385     }
386
387     void ExceptionDescribe() const noexcept
388     {
389         m_env->ExceptionDescribe();
390     }
391
392     void ExceptionClear() noexcept
393     {
394         m_env->ExceptionClear();
395     }
396
397     jboolean ExceptionCheck() const noexcept
398     {
399         return m_env->ExceptionCheck();
400     }
401
402     operator bool() const noexcept
403     {
404         return m_env != nullptr;
405     }
406
407     JNIEnv* get() const
408     {
409         return m_env;
410     }
411
412 private:
413     JNIEnv* m_env;
414 };
415
416
417
418 #endif // RCS_JNI_ENV_WRAPPER_H_