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