Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / jni / util / JavaClasses.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 JAVA_CLASSES_H_
22 #define JAVA_CLASSES_H_
23
24 #include <jni.h>
25
26 #include <string>
27
28 #define PACKAGE_NAME "org/iotivity/service/resourcecontainer"
29
30 #define CLS_NAME_VALUE PACKAGE_NAME "/RcsValue"
31
32 #define CLS_NAME_RESOURCEATTRIBUTES PACKAGE_NAME "/RcsResourceAttributes"
33
34 #define CLS_NAME_OBJECT "java/lang/Object"
35 #define CLS_NAME_STRING "java/lang/String"
36 #define CLS_NAME_INTEGER "java/lang/Integer"
37 #define CLS_NAME_DOUBLE "java/lang/Double"
38 #define CLS_NAME_BOOLEAN "java/lang/Boolean"
39
40 #define CLS_NAME_COLLECTION "java/util/Collection"
41 #define CLS_NAME_ARRAY_LIST "java/util/ArrayList"
42 #define CLS_NAME_SET "java/util/Set"
43 #define CLS_NAME_MAP "java/util/Map"
44 #define CLS_NAME_MAP_ENTRY "java/util/Map$Entry"
45 #define CLS_NAME_ITERATOR "java/util/Iterator"
46
47 #define AS_SIG(CLS_NAME) "L" CLS_NAME ";"
48
49 class JNIEnvWrapper;
50
51 extern jclass g_cls_Integer;
52 extern jclass g_cls_Double;
53 extern jclass g_cls_Boolean;
54 extern jclass g_cls_String;
55
56 extern jclass g_cls_ArrayList;
57 extern jclass g_cls_Set;
58 extern jclass g_cls_Map;
59 extern jclass g_cls_MapEntry;
60 extern jclass g_cls_Iterator;
61
62 extern jmethodID g_method_Boolean_booleanValue;
63 extern jmethodID g_method_Integer_intValue;
64 extern jmethodID g_method_Double_doubleValue;
65
66 extern jmethodID g_method_Collection_add;
67
68 extern jmethodID g_method_Set_iterator;
69
70 extern jmethodID g_method_Map_entrySet;
71 extern jmethodID g_method_Map_put;
72
73 extern jmethodID g_method_MapEntry_getKey;
74 extern jmethodID g_method_MapEntry_getValue;
75
76 extern jmethodID g_method_Iterator_hasNext;
77 extern jmethodID g_method_Iterator_next;
78
79 extern jmethodID g_ctor_Boolean;
80 extern jmethodID g_ctor_Integer;
81 extern jmethodID g_ctor_Double;
82
83 extern jmethodID g_ctor_ArrayList;
84 void initJavaClasses(JNIEnvWrapper *);
85 void clearJavaClasses(JNIEnvWrapper *);
86
87 template< typename ENV >
88 inline jobject newBooleanObject(ENV* env, bool value)
89 {
90     return env->NewObject(g_cls_Boolean, g_ctor_Boolean, value);
91 }
92
93 template< typename ENV >
94 inline jobject newIntegerObject(ENV* env, int value)
95 {
96     return env->NewObject(g_cls_Integer, g_ctor_Integer, value);
97 }
98
99 template< typename ENV >
100 inline jobject newDoubleObject(ENV* env, double value)
101 {
102     return env->NewObject(g_cls_Double, g_ctor_Double, value);
103 }
104
105 template< typename ENV >
106 inline jstring newStringObject(ENV* env, const std::string& value)
107 {
108     return env->NewStringUTF(value.c_str());
109 }
110
111 template< typename ENV >
112 inline jstring newStringObjectCstr(ENV* env, const char* value)
113 {
114     return env->NewStringUTF(value);
115 }
116
117 template< typename ENV >
118 inline std::string toStdString(ENV* env, jstring obj)
119 {
120     if (!obj) return "";
121
122     auto cstr = env->GetStringUTFChars(obj, nullptr);
123
124     if (!cstr) return "";
125
126     std::string result{ cstr };
127
128     env->ReleaseStringUTFChars(obj, cstr);
129
130     return result;
131 }
132
133 template< typename ENV >
134 inline jobject newArrayList(ENV* env)
135 {
136     return env->NewObject(g_cls_ArrayList, g_ctor_ArrayList);
137 }
138
139 template< typename ENV >
140 inline bool invoke_Boolean_booleanValue(ENV* env, jobject obj)
141 {
142     return env->CallBooleanMethod(obj, g_method_Boolean_booleanValue);
143 }
144
145 template< typename ENV >
146 inline int invoke_Integer_intValue(ENV* env, jobject obj)
147 {
148     return env->CallIntMethod(obj, g_method_Integer_intValue);
149 }
150
151 template< typename ENV >
152 inline double invoke_Double_doubleValue(ENV* env, jobject obj)
153 {
154     return env->CallDoubleMethod(obj, g_method_Double_doubleValue);
155 }
156
157 template< typename ENV >
158 inline jboolean invoke_Collection_add(ENV* env, jobject collectionObj, jobject valueObj)
159 {
160     return env->CallBooleanMethod(collectionObj, g_method_Collection_add, valueObj);
161 }
162
163 template< typename ENV >
164 inline jobject invoke_Map_entrySet(ENV* env, jobject mapObj)
165 {
166     return env->CallObjectMethod(mapObj, g_method_Map_entrySet);
167 }
168
169 template< typename ENV >
170 inline jobject invoke_Map_put(ENV* env, jobject mapObj, jobject keyObj, jobject valueObj)
171 {
172     return env->CallObjectMethod(mapObj, g_method_Map_put, keyObj, valueObj);
173 }
174
175 template< typename ENV >
176 inline jobject invoke_MapEntry_getKey(ENV* env, jobject entryObj)
177 {
178     return env->CallObjectMethod(entryObj, g_method_MapEntry_getKey);
179 }
180
181 template< typename ENV >
182 inline jobject invoke_MapEntry_getValue(ENV* env, jobject entryObj)
183 {
184     return env->CallObjectMethod(entryObj, g_method_MapEntry_getValue);
185 }
186
187 template< typename ENV >
188 inline jobject invoke_Set_iterator(ENV* env, jobject setObj)
189 {
190     return env->CallObjectMethod(setObj, g_method_Set_iterator);
191 }
192
193 template< typename ENV >
194 inline bool invoke_Iterator_hasNext(ENV* env, jobject iterObj)
195 {
196     return env->CallBooleanMethod(iterObj, g_method_Iterator_hasNext);
197 }
198
199 template< typename ENV >
200 inline jobject invoke_Iterator_next(ENV* env, jobject iterObj)
201 {
202     return env->CallObjectMethod(iterObj, g_method_Iterator_next);
203 }
204
205 #endif // JAVA_CLASSES_H_