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 #include "JniRcsResourceAttributes.h"
23 #include "JNIEnvWrapper.h"
24 #include "JavaClasses.h"
25 #include "JavaLocalRef.h"
26 #include "JniRcsValue.h"
30 using namespace OIC::Service;
32 #define LOG_TAG "JNI-RCSResourceAttributes"
36 jclass g_cls_RCSResourceAttributes;
38 jmethodID g_ctor_RCSResourceAttributes;
40 jfieldID g_field_mCache;
43 void initRCSResourceAttributes(JNIEnvWrapper* env)
45 g_cls_RCSResourceAttributes = env->FindClassAsGlobalRef(CLS_NAME_RESOURCEATTRIBUTES);
46 g_ctor_RCSResourceAttributes = env->GetConstructorID(g_cls_RCSResourceAttributes, "()V");
48 g_field_mCache = env->GetFieldID(g_cls_RCSResourceAttributes, "mCache", AS_SIG(CLS_NAME_MAP));
51 void clearRCSResourceAttributes(JNIEnvWrapper* env)
53 env->DeleteGlobalRef(g_cls_RCSResourceAttributes);
56 jobject newAttributesObject(JNIEnv* env, const RCSResourceAttributes& attrs)
58 jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
59 VERIFY_NO_EXC_RET_DEF(env);
61 jobject mapObj = env->GetObjectField(obj, g_field_mCache);
63 //EXPECT(mapObj, "Map is null.");
64 for (const auto& p : attrs) {
65 JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
68 JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
71 invoke_Map_put(env, mapObj, keyObj, valueObj);
75 // setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
80 jobject newAttributesObject(JNIEnvWrapper* env, const RCSResourceAttributes& attrs)
82 jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
84 jobject mapObj = env->GetObjectField(obj, g_field_mCache);
86 //EXPECT(mapObj, "Map is null.");
87 for (const auto& p : attrs) {
88 JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
91 JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
94 invoke_Map_put(env, mapObj, keyObj, valueObj);
98 // setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
103 RCSResourceAttributes toNativeAttributes(JNIEnv* env, jobject attrsObj)
105 EXPECT_RET(attrsObj, "attrsObj is null!", { });
107 JNIEnvWrapper envWrapper{ env };
111 return toNativeAttributes(&envWrapper, attrsObj);
113 catch (const JavaException&)
119 RCSResourceAttributes toNativeAttributes(JNIEnvWrapper* env, jobject attrsObj)
121 EXPECT_RET(attrsObj, "attrsObj is null!", { });
123 RCSResourceAttributes attrs;
125 /*if (hasNativeHandle(env, attrsObj))
127 attrs = getNativeHandleAs< RCSResourceAttributes >(env, attrsObj);
129 LOGD("writeNativeAttributesFromMap");
131 writeNativeAttributesFromMap(env,
132 JavaLocalObject{ env, env->GetObjectField(attrsObj, g_field_mCache) }, attrs);
137 void writeNativeAttributesFromMap(JNIEnv* env, jobject mapObj, RCSResourceAttributes& targetAttrs)
139 JNIEnvWrapper envWrapper{ env };
143 return writeNativeAttributesFromMap(&envWrapper, mapObj, targetAttrs);
145 catch (const JavaException&)
150 void writeNativeAttributesFromMap(JNIEnvWrapper* env, jobject mapObj,
151 RCSResourceAttributes& targetAttrs)
153 LOGD("in write native attributes from map");
154 LOGD("invoke map entry set");
155 JavaLocalObject setObj{ env, invoke_Map_entrySet(env, mapObj) };
156 LOGD("invoke set iterator");
157 JavaLocalObject iterObj{ env, invoke_Set_iterator(env, setObj) };
158 LOGD("invoke has next");
159 while (invoke_Iterator_hasNext(env, iterObj))
161 LOGD("invoke entry obj next");
162 JavaLocalObject entryObj{ env, invoke_Iterator_next(env, iterObj) };
163 LOGD("invoke key obj next");
164 JavaLocalObject keyObj{ env, invoke_MapEntry_getKey(env, entryObj) };
165 LOGD("invoke value obj next");
166 JavaLocalObject valueObj{ env, invoke_MapEntry_getValue(env, entryObj) };
167 LOGD("invoke toStdString");
168 auto key = toStdString(env, static_cast< jstring >(keyObj.get()));
169 LOGD("invoke toNativeAttrsvalue");
170 targetAttrs[std::move(key)] = toNativeAttrsValue(env, valueObj);
172 LOGD("in write native attributes from map finished");