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 JAVA_CLASSES_H_
22 #define JAVA_CLASSES_H_
28 #define PACKAGE_NAME "org/iotivity/service/resourcecontainer"
30 #define CLS_NAME_VALUE PACKAGE_NAME "/RcsValue"
32 #define CLS_NAME_RESOURCEATTRIBUTES PACKAGE_NAME "/RcsResourceAttributes"
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"
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"
47 #define AS_SIG(CLS_NAME) "L" CLS_NAME ";"
51 extern jclass g_cls_Integer;
52 extern jclass g_cls_Double;
53 extern jclass g_cls_Boolean;
54 extern jclass g_cls_String;
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;
62 extern jmethodID g_method_Boolean_booleanValue;
63 extern jmethodID g_method_Integer_intValue;
64 extern jmethodID g_method_Double_doubleValue;
66 extern jmethodID g_method_Collection_add;
68 extern jmethodID g_method_Set_iterator;
70 extern jmethodID g_method_Map_entrySet;
71 extern jmethodID g_method_Map_put;
73 extern jmethodID g_method_MapEntry_getKey;
74 extern jmethodID g_method_MapEntry_getValue;
76 extern jmethodID g_method_Iterator_hasNext;
77 extern jmethodID g_method_Iterator_next;
79 extern jmethodID g_ctor_Boolean;
80 extern jmethodID g_ctor_Integer;
81 extern jmethodID g_ctor_Double;
83 extern jmethodID g_ctor_ArrayList;
84 void initJavaClasses(JNIEnvWrapper *);
85 void clearJavaClasses(JNIEnvWrapper *);
87 template< typename ENV >
88 inline jobject newBooleanObject(ENV* env, bool value)
90 return env->NewObject(g_cls_Boolean, g_ctor_Boolean, value);
93 template< typename ENV >
94 inline jobject newIntegerObject(ENV* env, int value)
96 return env->NewObject(g_cls_Integer, g_ctor_Integer, value);
99 template< typename ENV >
100 inline jobject newDoubleObject(ENV* env, double value)
102 return env->NewObject(g_cls_Double, g_ctor_Double, value);
105 template< typename ENV >
106 inline jstring newStringObject(ENV* env, const std::string& value)
108 return env->NewStringUTF(value.c_str());
111 template< typename ENV >
112 inline jstring newStringObjectCstr(ENV* env, const char* value)
114 return env->NewStringUTF(value);
117 template< typename ENV >
118 inline std::string toStdString(ENV* env, jstring obj)
122 auto cstr = env->GetStringUTFChars(obj, nullptr);
124 if (!cstr) return "";
126 std::string result{ cstr };
128 env->ReleaseStringUTFChars(obj, cstr);
133 template< typename ENV >
134 inline jobject newArrayList(ENV* env)
136 return env->NewObject(g_cls_ArrayList, g_ctor_ArrayList);
139 template< typename ENV >
140 inline bool invoke_Boolean_booleanValue(ENV* env, jobject obj)
142 return env->CallBooleanMethod(obj, g_method_Boolean_booleanValue);
145 template< typename ENV >
146 inline int invoke_Integer_intValue(ENV* env, jobject obj)
148 return env->CallIntMethod(obj, g_method_Integer_intValue);
151 template< typename ENV >
152 inline double invoke_Double_doubleValue(ENV* env, jobject obj)
154 return env->CallDoubleMethod(obj, g_method_Double_doubleValue);
157 template< typename ENV >
158 inline jboolean invoke_Collection_add(ENV* env, jobject collectionObj, jobject valueObj)
160 return env->CallBooleanMethod(collectionObj, g_method_Collection_add, valueObj);
163 template< typename ENV >
164 inline jobject invoke_Map_entrySet(ENV* env, jobject mapObj)
166 return env->CallObjectMethod(mapObj, g_method_Map_entrySet);
169 template< typename ENV >
170 inline jobject invoke_Map_put(ENV* env, jobject mapObj, jobject keyObj, jobject valueObj)
172 return env->CallObjectMethod(mapObj, g_method_Map_put, keyObj, valueObj);
175 template< typename ENV >
176 inline jobject invoke_MapEntry_getKey(ENV* env, jobject entryObj)
178 return env->CallObjectMethod(entryObj, g_method_MapEntry_getKey);
181 template< typename ENV >
182 inline jobject invoke_MapEntry_getValue(ENV* env, jobject entryObj)
184 return env->CallObjectMethod(entryObj, g_method_MapEntry_getValue);
187 template< typename ENV >
188 inline jobject invoke_Set_iterator(ENV* env, jobject setObj)
190 return env->CallObjectMethod(setObj, g_method_Set_iterator);
193 template< typename ENV >
194 inline bool invoke_Iterator_hasNext(ENV* env, jobject iterObj)
196 return env->CallBooleanMethod(iterObj, g_method_Iterator_hasNext);
199 template< typename ENV >
200 inline jobject invoke_Iterator_next(ENV* env, jobject iterObj)
202 return env->CallObjectMethod(iterObj, g_method_Iterator_next);
205 #endif // JAVA_CLASSES_H_