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 "simulator_resource_model_jni.h"
22 #include "simulator_error_codes.h"
23 #include "simulator_exceptions_jni.h"
24 #include "simulator_utils_jni.h"
25 #include "jni_string.h"
30 JniTypeInfo(JNIEnv *env) : m_env(env) {}
32 SimulatorResourceModel::TypeInfo toCpp(jobject jAttributeValue)
34 static jmethodID typeInfoMID = m_env->GetMethodID(gSimulatorClassRefs.attributeValueCls,
35 "typeInfo", "()Lorg/oic/simulator/AttributeValue$TypeInfo;");
36 static jfieldID typeFID = m_env->GetFieldID(gSimulatorClassRefs.attributeTypeInfoCls,
37 "mType", "Lorg/oic/simulator/AttributeValue$ValueType;");
38 static jfieldID baseTypeFID = m_env->GetFieldID(gSimulatorClassRefs.attributeTypeInfoCls,
39 "mBaseType", "Lorg/oic/simulator/AttributeValue$ValueType;");
40 static jfieldID depthFID = m_env->GetFieldID(gSimulatorClassRefs.attributeTypeInfoCls,
43 jobject jTypeInfo = m_env->CallObjectMethod(jAttributeValue, typeInfoMID);
44 jobject jType = m_env->GetObjectField(jTypeInfo, typeFID);
45 jobject jBaseType = m_env->GetObjectField(jTypeInfo, baseTypeFID);
46 jint jDepth = m_env->GetIntField(jTypeInfo, depthFID);
48 return SimulatorResourceModel::TypeInfo(getValueType(jType),
49 getValueType(jBaseType), jDepth);
53 SimulatorResourceModel::ValueType getValueType(jobject jValueType)
55 static jmethodID ordinalMID = m_env->GetMethodID(
56 gSimulatorClassRefs.attributeValueTypeCls, "ordinal", "()I");
58 int ordinal = m_env->CallIntMethod(jValueType, ordinalMID);
59 return SimulatorResourceModel::ValueType(ordinal);
65 class ValueConverterJava : public boost::static_visitor<jobject>
68 ValueConverterJava(JNIEnv *env) : m_env(env) {}
70 jobject operator ()(const int &value)
72 static jmethodID integerCtor =
73 m_env->GetMethodID(gSimulatorClassRefs.integerCls, "<init>", "(I)V");
74 return m_env->NewObject(gSimulatorClassRefs.integerCls,
78 jobject operator ()(const double &value)
80 static jmethodID doubleCtor =
81 m_env->GetMethodID(gSimulatorClassRefs.doubleCls, "<init>", "(D)V");
82 return m_env->NewObject(gSimulatorClassRefs.doubleCls,
86 jobject operator ()(const bool &value)
88 static jmethodID booleanCtor =
89 m_env->GetMethodID(gSimulatorClassRefs.booleanCls, "<init>", "(Z)V");
90 return m_env->NewObject(gSimulatorClassRefs.booleanCls,
94 jobject operator ()(const std::string &value)
96 jstring stringValue = m_env->NewStringUTF(value.c_str());
97 return static_cast<jobject>(stringValue);
100 jobject operator ()(const SimulatorResourceModel &value)
102 return simulatorResourceModelToJava(m_env, const_cast<SimulatorResourceModel &>(value));
105 template <typename T>
106 jobject operator ()(const std::vector<T> &values)
108 jobjectArray jArray = m_env->NewObjectArray(values.size(), getClass(values), nullptr);
112 for (size_t index = 0; index < values.size(); index++)
114 jobject element = operator()(values[index]);
115 m_env->SetObjectArrayElement(jArray, index, element);
121 template <typename T>
122 jobject operator ()(const std::vector<std::vector<T>> &values)
124 jobjectArray jArray = m_env->NewObjectArray(values.size(), getClass(values), nullptr);
128 for (size_t index = 0; index < values.size(); index++)
130 jobject element = operator()(values[index]);
131 m_env->SetObjectArrayElement(jArray, index, element);
137 template <typename T>
138 jobject operator ()(const std::vector<std::vector<std::vector<T>>> &values)
140 jobjectArray jArray = m_env->NewObjectArray(values.size(), getClass(values), nullptr);
144 for (size_t index = 0; index < values.size(); index++)
146 jobject element = operator()(values[index]);
147 m_env->SetObjectArrayElement(jArray, index, element);
154 jclass getClass(const std::vector<int> &)
156 return gSimulatorClassRefs.integerCls;
159 jclass getClass(const std::vector<std::vector<int>> &)
161 return gSimulatorClassRefs.integer1DArrayCls;
164 jclass getClass(const std::vector<std::vector<std::vector<int>>> &)
166 return gSimulatorClassRefs.integer2DArrayCls;
169 jclass getClass(const std::vector<double> &)
171 return gSimulatorClassRefs.doubleCls;
174 jclass getClass(const std::vector<std::vector<double>> &)
176 return gSimulatorClassRefs.double1DArrayCls;
179 jclass getClass(const std::vector<std::vector<std::vector<double>>> &)
181 return gSimulatorClassRefs.double2DArrayCls;
184 jclass getClass(const std::vector<bool> &)
186 return gSimulatorClassRefs.booleanCls;
189 jclass getClass(const std::vector<std::vector<bool>> &)
191 return gSimulatorClassRefs.boolean1DArrayCls;
194 jclass getClass(const std::vector<std::vector<std::vector<bool>>> &)
196 return gSimulatorClassRefs.boolean2DArrayCls;
199 jclass getClass(const std::vector<std::string> &)
201 return gSimulatorClassRefs.stringCls;
204 jclass getClass(const std::vector<std::vector<std::string>> &)
206 return gSimulatorClassRefs.string1DArrayCls;
209 jclass getClass(const std::vector<std::vector<std::vector<std::string>>> &)
211 return gSimulatorClassRefs.string2DArrayCls;
214 jclass getClass(const std::vector<SimulatorResourceModel> &)
216 return gSimulatorClassRefs.simulatorResourceModelCls;
219 jclass getClass(const std::vector<std::vector<SimulatorResourceModel>> &)
221 return gSimulatorClassRefs.simulatorResModel1DArrayCls;
224 jclass getClass(const std::vector<std::vector<std::vector<SimulatorResourceModel>>> &)
226 return gSimulatorClassRefs.simulatorResModel2DArrayCls;
232 class ValueConverterCpp
235 ValueConverterCpp(JNIEnv *env, SimulatorResourceModel::TypeInfo &typeInfo,
236 jobject &value) : m_env(env), m_typeInfo(typeInfo), m_value(value) {}
240 switch (m_typeInfo.baseType())
242 case SimulatorResourceModel::ValueType::INTEGER:
243 return handleByDepth<int>();
244 case SimulatorResourceModel::ValueType::DOUBLE:
245 return handleByDepth<double>();
246 case SimulatorResourceModel::ValueType::BOOLEAN:
247 return handleByDepth<bool>();
248 case SimulatorResourceModel::ValueType::STRING:
249 return handleByDepth<std::string>();
250 case SimulatorResourceModel::ValueType::RESOURCE_MODEL:
251 return handleByDepth<SimulatorResourceModel>();
252 case SimulatorResourceModel::ValueType::VECTOR:
253 case SimulatorResourceModel::ValueType::UNKNOWN:
258 SimulatorResourceModel::ValueVariant get()
260 return std::move(m_result);
264 template <typename T>
267 if (0 == m_typeInfo.depth())
270 getValue(m_value, value);
273 else if (1 == m_typeInfo.depth())
275 std::vector<T> value;
276 getValue(m_value, value);
279 else if (2 == m_typeInfo.depth())
281 std::vector<std::vector<T>> value;
282 getValue(m_value, value);
285 else if (3 == m_typeInfo.depth())
287 std::vector<std::vector<std::vector<T>>> value;
288 getValue(m_value, value);
293 void getValue(jobject &jValue, int &value)
295 static jmethodID intValueMID = m_env->GetMethodID(
296 gSimulatorClassRefs.integerCls, "intValue", "()I");
298 jint temp = m_env->CallIntMethod(jValue, intValueMID);
302 void getValue(jobject &jValue, std::vector<int> &value)
304 jobjectArray array = (jobjectArray) jValue;
305 size_t length = m_env->GetArrayLength(array);
306 for (size_t i = 0; i < length; i++)
308 jobject jElement = m_env->GetObjectArrayElement(array, i);
311 getValue(jElement, element);
312 value.push_back(element);
316 void getValue(jobject &jValue, std::vector<std::vector<int>> &value)
318 jobjectArray array = (jobjectArray) jValue;
319 size_t length = m_env->GetArrayLength(array);
320 for (size_t i = 0; i < length; i++)
322 jobject jElement = m_env->GetObjectArrayElement(array, i);
324 std::vector<int> element;
325 getValue(jElement, element);
326 value.push_back(element);
330 void getValue(jobject &jValue, std::vector<std::vector<std::vector<int>>> &value)
332 jobjectArray array = (jobjectArray) jValue;
333 size_t length = m_env->GetArrayLength(array);
334 for (size_t i = 0; i < length; i++)
336 jobject jElement = m_env->GetObjectArrayElement(array, i);
338 std::vector<std::vector<int>> element;
339 getValue(jElement, element);
340 value.push_back(element);
344 void getValue(jobject &jValue, double &value)
346 static jmethodID doubleValueMID = m_env->GetMethodID(
347 gSimulatorClassRefs.doubleCls, "doubleValue", "()D");
349 value = m_env->CallDoubleMethod(jValue, doubleValueMID);
352 void getValue(jobject &jValue, std::vector<double> &value)
354 jobjectArray array = (jobjectArray) jValue;
355 size_t length = m_env->GetArrayLength(array);
356 for (size_t i = 0; i < length; i++)
358 jobject jElement = m_env->GetObjectArrayElement(array, i);
361 getValue(jElement, element);
362 value.push_back(element);
366 void getValue(jobject &jValue, std::vector<std::vector<double>> &value)
368 jobjectArray array = (jobjectArray) jValue;
369 size_t length = m_env->GetArrayLength(array);
370 for (size_t i = 0; i < length; i++)
372 jobject jElement = m_env->GetObjectArrayElement(array, i);
374 std::vector<double> element;
375 getValue(jElement, element);
376 value.push_back(element);
380 void getValue(jobject &jValue, std::vector<std::vector<std::vector<double>>> &value)
382 jobjectArray array = (jobjectArray) jValue;
383 size_t length = m_env->GetArrayLength(array);
384 for (size_t i = 0; i < length; i++)
386 jobject jElement = m_env->GetObjectArrayElement(array, i);
388 std::vector<std::vector<double>> element;
389 getValue(jElement, element);
390 value.push_back(element);
394 void getValue(jobject &jValue, bool &value)
396 static jmethodID boolValueMID = m_env->GetMethodID(
397 gSimulatorClassRefs.booleanCls, "booleanValue", "()Z");
399 value = m_env->CallBooleanMethod(jValue, boolValueMID);
402 void getValue(jobject &jValue, std::vector<bool> &value)
404 jobjectArray array = (jobjectArray) jValue;
405 size_t length = m_env->GetArrayLength(array);
406 for (size_t i = 0; i < length; i++)
408 jobject jElement = m_env->GetObjectArrayElement(array, i);
411 getValue(jElement, element);
412 value.push_back(element);
416 void getValue(jobject &jValue, std::vector<std::vector<bool>> &value)
418 jobjectArray array = (jobjectArray) jValue;
419 size_t length = m_env->GetArrayLength(array);
420 for (size_t i = 0; i < length; i++)
422 jobject jElement = m_env->GetObjectArrayElement(array, i);
424 std::vector<bool> element;
425 getValue(jElement, element);
426 value.push_back(element);
430 void getValue(jobject &jValue, std::vector<std::vector<std::vector<bool>>> &value)
432 jobjectArray array = (jobjectArray) jValue;
433 size_t length = m_env->GetArrayLength(array);
434 for (size_t i = 0; i < length; i++)
436 jobject jElement = m_env->GetObjectArrayElement(array, i);
438 std::vector<std::vector<bool>> element;
439 getValue(jElement, element);
440 value.push_back(element);
444 void getValue(jobject &jValue, std::string &value)
446 jstring stringValue = (jstring) jValue;
447 JniString jniValue(m_env, stringValue);
448 value = jniValue.get();
451 void getValue(jobject jValue, std::vector<std::string> &value)
453 jobjectArray array = (jobjectArray) jValue;
454 size_t length = m_env->GetArrayLength(array);
455 for (size_t i = 0; i < length; i++)
457 jobject jElement = m_env->GetObjectArrayElement(array, i);
460 getValue(jElement, element);
461 value.push_back(element);
465 void getValue(jobject &jValue, std::vector<std::vector<std::string>> &value)
467 jobjectArray array = (jobjectArray) jValue;
468 size_t length = m_env->GetArrayLength(array);
469 for (size_t i = 0; i < length; i++)
471 jobject jElement = m_env->GetObjectArrayElement(array, i);
473 std::vector<std::string> element;
474 getValue(jElement, element);
475 value.push_back(element);
479 void getValue(jobject &jValue, std::vector<std::vector<std::vector<std::string>>> &value)
481 jobjectArray array = (jobjectArray) jValue;
482 size_t length = m_env->GetArrayLength(array);
483 for (size_t i = 0; i < length; i++)
485 jobject jElement = m_env->GetObjectArrayElement(array, i);
487 std::vector<std::vector<std::string>> element;
488 getValue(jElement, element);
489 value.push_back(element);
493 void getValue(jobject &jValue, SimulatorResourceModel &value)
497 void getValue(jobject &jValue, std::vector<SimulatorResourceModel> &value)
501 void getValue(jobject &jValue, std::vector<std::vector<SimulatorResourceModel>> &value)
505 void getValue(jobject &jValue, std::vector<std::vector<std::vector<SimulatorResourceModel>>> &value)
510 SimulatorResourceModel::TypeInfo &m_typeInfo;
512 SimulatorResourceModel::ValueVariant m_result;
515 class JniAttributeValue
518 static jobject toJava(JNIEnv *env, SimulatorResourceModel::Attribute &attribute)
520 auto value = attribute.getValue();
521 return toJava(env, value);
524 static jobject toJava(JNIEnv *env, SimulatorResourceModel::ValueVariant &value)
526 ValueConverterJava converter(env);
527 jobject jValue = boost::apply_visitor(converter, value);
529 static jmethodID attrValueCtor = env->GetMethodID(
530 gSimulatorClassRefs.attributeValueCls, "<init>", "(Ljava/lang/Object;)V");
532 return env->NewObject(gSimulatorClassRefs.attributeValueCls, attrValueCtor, jValue);
535 static SimulatorResourceModel::ValueVariant toCpp(JNIEnv *env, jobject &jAttributeValue)
537 static jmethodID getMID = env->GetMethodID(gSimulatorClassRefs.attributeValueCls,
538 "get", "()Ljava/lang/Object;");
540 SimulatorResourceModel::TypeInfo typeInfo = JniTypeInfo(env).toCpp(jAttributeValue);
541 jobject jValue = env->CallObjectMethod(jAttributeValue, getMID);
543 ValueConverterCpp converter(env, typeInfo, jValue);
545 return converter.get();
549 class JniAttributeProperty
552 static jobject toJava(JNIEnv *env,
553 SimulatorResourceModel::AttributeProperty &property)
555 jobject jAttributeProperty = nullptr;
556 if (SimulatorResourceModel::AttributeProperty::Type::RANGE == property.type())
558 static jmethodID propertyCtor = env->GetMethodID(
559 gSimulatorClassRefs.attributePropertyCls, "<init>", "(DD)V");
561 jAttributeProperty = env->NewObject(gSimulatorClassRefs.attributePropertyCls, propertyCtor,
562 property.min(), property.max());
566 static jmethodID propertyCtor = env->GetMethodID(
567 gSimulatorClassRefs.attributePropertyCls, "<init>", "([Lorg/oic/simulator/AttributeValue;)V");
569 jobjectArray jValueSet = env->NewObjectArray(property.valueSetSize(),
570 gSimulatorClassRefs.attributeValueCls, nullptr);
572 for (auto &value : property.valueSet())
574 jobject jValue = JniAttributeValue::toJava(env, value);
575 env->SetObjectArrayElement(jValueSet, index++, jValue);
578 jAttributeProperty = env->NewObject(gSimulatorClassRefs.attributePropertyCls, propertyCtor,
582 // Add child property
583 if (jAttributeProperty && property.getChildProperty())
585 SimulatorResourceModel::AttributeProperty childProperty = *(property.getChildProperty());
586 jobject jChildProperty = JniAttributeProperty::toJava(env, property);
589 static jfieldID childPropFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
590 "mChildProperty", "Lorg/oic/simulator/AttributeProperty;");
591 env->SetObjectField(jAttributeProperty, childPropFID, jChildProperty);
595 return jAttributeProperty;
598 static SimulatorResourceModel::AttributeProperty toCpp(JNIEnv *env, jobject jAttributeProperty)
600 static jfieldID typeFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
601 "mType", "Lorg/oic/simulator/AttributeProperty$Type;");
602 static jfieldID minFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
604 static jfieldID maxFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
606 static jfieldID valueSetFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
607 "mValueSet", "[Lorg/oic/simulator/AttributeValue;");
608 static jfieldID childPropFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
609 "mChildProperty", "Lorg/oic/simulator/AttributeProperty;");
610 static jmethodID ordinalMID = env->GetMethodID(
611 gSimulatorClassRefs.attributePropertyTypeCls, "ordinal", "()I");
613 SimulatorResourceModel::AttributeProperty attributeProperty;
614 jobject jType = env->GetObjectField(jAttributeProperty, typeFID);
615 jdouble jMin = env->GetDoubleField(jAttributeProperty, minFID);
616 jdouble jMax = env->GetDoubleField(jAttributeProperty, maxFID);
617 jobjectArray jValueSet = (jobjectArray) env->GetObjectField(jAttributeProperty, valueSetFID);
618 jobject jChildProperty = env->GetObjectField(jAttributeProperty, childPropFID);
620 int ordinal = env->CallIntMethod(jType, ordinalMID);
621 switch (SimulatorResourceModel::AttributeProperty::Type(ordinal))
623 case SimulatorResourceModel::AttributeProperty::Type::RANGE:
625 attributeProperty = SimulatorResourceModel::AttributeProperty(jMin, jMax);
629 case SimulatorResourceModel::AttributeProperty::Type::VALUE_SET:
631 std::vector<SimulatorResourceModel::ValueVariant> valueSet;
632 size_t length = env->GetArrayLength(jValueSet);
633 for (size_t i = 0; i < length; i++)
635 jobject jAttributeValue = env->GetObjectArrayElement(jValueSet, i);
636 valueSet.push_back(JniAttributeValue::toCpp(env, jAttributeValue));
639 attributeProperty = SimulatorResourceModel::AttributeProperty(valueSet);
644 // Set child property
647 SimulatorResourceModel::AttributeProperty childProperty =
648 JniAttributeProperty::toCpp(env, jAttributeProperty);
649 attributeProperty.setChildProperty(childProperty);
652 return attributeProperty;
656 static jobject createHashMap(JNIEnv *env)
658 static jmethodID hashMapCtor = env->GetMethodID(
659 gSimulatorClassRefs.hashMapCls, "<init>", "()V");
660 return env->NewObject(gSimulatorClassRefs.hashMapCls, hashMapCtor);
663 static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
665 if (!mapobj || !key || !value)
668 static jmethodID hashMapPutMethod = env->GetMethodID(gSimulatorClassRefs.hashMapCls,
669 "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
670 env->CallObjectMethod(mapobj, hashMapPutMethod, key, value);
673 jobject simulatorResourceModelToJava(JNIEnv *env, SimulatorResourceModel &resModel)
675 jobject attributesMap = createHashMap(env);
676 jobject propertiesMap = createHashMap(env);
677 if (!attributesMap || !propertiesMap)
680 for (auto &attributeEntry : resModel.getAttributes())
682 jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
683 jobject jAttributeValue = JniAttributeValue::toJava(env, attributeEntry.second);
684 addEntryToHashMap(env, attributesMap, jAttrName, jAttributeValue);
686 jobject jAttributeProperty = JniAttributeProperty::toJava(env, attributeEntry.second.getProperty());
687 addEntryToHashMap(env, propertiesMap, jAttrName, jAttributeProperty);
690 static jmethodID simulatorResourceModelCtor = env->GetMethodID(
691 gSimulatorClassRefs.simulatorResourceModelCls, "<init>", "(Ljava/util/Map;Ljava/util/Map;)V");
693 return env->NewObject(gSimulatorClassRefs.simulatorResourceModelCls,
694 simulatorResourceModelCtor, attributesMap, propertiesMap);
697 jobject simulatorResourceAttributeToJava(JNIEnv *env, SimulatorResourceModel::Attribute &attribute)
699 static jmethodID simulatorResAttributeCtor = env->GetMethodID(
700 gSimulatorClassRefs.simulatorResourceAttributeCls, "<init>",
701 "(Ljava/lang/String;Lorg/oic/simulator/AttributeValue;Lorg/oic/simulator/AttributeProperty;)V");
703 jstring jAttrName = env->NewStringUTF(attribute.getName().c_str());
704 jobject jAttributeValue = JniAttributeValue::toJava(env, attribute);
705 jobject jAttributeProperty = JniAttributeProperty::toJava(env, attribute.getProperty());
707 return env->NewObject(gSimulatorClassRefs.simulatorResourceAttributeCls,
708 simulatorResAttributeCtor, jAttrName, jAttributeValue, jAttributeProperty);
711 bool simulatorResourceModelToCpp(JNIEnv *env, jobject jResModel, SimulatorResourceModel &resModel)
716 static jfieldID valuesFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceModelCls,
717 "mValues", "Ljava/util/Map;");
718 static jfieldID propertiesFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceModelCls,
719 "mProperties", "Ljava/util/Map;");
720 static jmethodID entrySetMID = env->GetMethodID(gSimulatorClassRefs.mapCls, "entrySet",
721 "()Ljava/util/Set;");
722 static jmethodID iteratorMID = env->GetMethodID(gSimulatorClassRefs.setCls, "iterator",
723 "()Ljava/util/Iterator;");
724 static jmethodID hasNextMID = env->GetMethodID(gSimulatorClassRefs.iteratorCls, "hasNext",
726 static jmethodID nextMID = env->GetMethodID(gSimulatorClassRefs.iteratorCls, "next",
727 "()Ljava/lang/Object;");
728 static jmethodID getKeyMID = env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getKey",
729 "()Ljava/lang/Object;");
730 static jmethodID getValueMID = env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getValue",
731 "()Ljava/lang/Object;");
733 jobject jValues = env->GetObjectField(jResModel, valuesFID);
734 jobject jProperties = env->GetObjectField(jResModel, propertiesFID);
738 jobject entrySet = env->CallObjectMethod(jValues, entrySetMID);
739 jobject iterator = env->CallObjectMethod(entrySet, iteratorMID);
740 if (entrySet && iterator)
742 while (env->CallBooleanMethod(iterator, hasNextMID))
744 jobject entry = env->CallObjectMethod(iterator, nextMID);
745 jstring key = (jstring) env->CallObjectMethod(entry, getKeyMID);
746 jobject value = env->CallObjectMethod(entry, getValueMID);
747 resModel.add(JniString(env, key).get(), JniAttributeValue::toCpp(env, value));
749 env->DeleteLocalRef(entry);
750 env->DeleteLocalRef(key);
751 env->DeleteLocalRef(value);
758 jobject entrySet = env->CallObjectMethod(jProperties, entrySetMID);
759 jobject iterator = env->CallObjectMethod(entrySet, iteratorMID);
760 if (entrySet && iterator)
762 while (env->CallBooleanMethod(iterator, hasNextMID))
764 jobject entry = env->CallObjectMethod(iterator, nextMID);
765 jstring key = (jstring) env->CallObjectMethod(entry, getKeyMID);
766 jobject value = env->CallObjectMethod(entry, getValueMID);
767 resModel.setAttributeProperty(JniString(env, key).get(),
768 JniAttributeProperty::toCpp(env, value));
770 env->DeleteLocalRef(entry);
771 env->DeleteLocalRef(key);
772 env->DeleteLocalRef(value);
780 bool simulatorResourceAttributeToCpp(JNIEnv *env, jobject jAttribute,
781 SimulatorResourceModel::Attribute &attribute)
786 static jfieldID nameFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
787 "mName", "Ljava/lang/String;");
788 static jfieldID valueFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
789 "mValue", "Lorg/oic/simulator/AttributeValue;");
790 static jfieldID propertyFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
791 "mProperty", "Lorg/oic/simulator/AttributeProperty;");
793 jstring jAttributeName = (jstring) env->GetObjectField(jAttribute, nameFID);
794 jobject jAttributeValue = env->GetObjectField(jAttribute, valueFID);
795 jobject jAttributeProperty = env->GetObjectField(jAttribute, propertyFID);
797 if (!jAttributeName || !jAttributeValue)
800 JniString attrName(env, jAttributeName);
801 SimulatorResourceModel::ValueVariant value = JniAttributeValue::toCpp(env, jAttributeValue);
803 attribute.setName(attrName.get());
804 attribute.setValue(value);
805 if (jAttributeProperty)
807 SimulatorResourceModel::AttributeProperty property = JniAttributeProperty::toCpp(env,
809 attribute.setProperty(property);
815 bool AttributeValueToCpp(JNIEnv *env, jobject jAttributeValue,
816 SimulatorResourceModel::ValueVariant &value)
818 if (!jAttributeValue)
821 value = JniAttributeValue::toCpp(env, jAttributeValue);