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 "jni_vector.h"
22 #include "jni_string.h"
23 #include "simulator_utils_jni.h"
25 JniVector::JniVector(JNIEnv *env) : m_env(env) {}
27 jobject JniVector::toJava()
29 static jmethodID vectorCtor = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
31 return m_env->NewObject(gSimulatorClassRefs.vectorCls, vectorCtor);
34 jobject JniVector::toJava(std::vector<std::string> &cppVector)
36 static jmethodID addMethod = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
37 "add", "(Ljava/lang/Object;)Z");
39 jobject vectorObject = toJava();
43 for (size_t index = 0; index < cppVector.size(); index++)
45 jstring element = m_env->NewStringUTF(cppVector[index].c_str());
46 m_env->CallBooleanMethod(vectorObject, addMethod, element);
47 m_env->DeleteLocalRef(element);
53 int JniVector::getSize(jobject jVector)
55 static jmethodID sizeMethodID = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
57 return m_env->CallIntMethod(jVector, sizeMethodID);
60 void JniVector::addElementsCpp(jobject vector, int size, std::vector<int> &result)
62 static jmethodID getMethod = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
63 "get", "(I)Ljava/lang/Object;");
64 static jmethodID intValueMethod = m_env->GetMethodID(gSimulatorClassRefs.integerCls,
67 for (int index = 0; index < size; index++)
69 jobject intObject = m_env->CallObjectMethod(vector, getMethod, index);
70 int value = m_env->CallIntMethod(intObject, intValueMethod);
71 result.push_back(value);
75 void JniVector::addElementsCpp(jobject vector, int size, std::vector<double> &result)
77 static jmethodID getMethod = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
78 "get", "(I)Ljava/lang/Object;");
79 static jmethodID doubleValueMethod = m_env->GetMethodID(gSimulatorClassRefs.doubleCls,
80 "doubleValue", "()D");
82 for (int index = 0; index < size; index++)
84 jobject doubleObject = m_env->CallObjectMethod(vector, getMethod, index);
85 double value = m_env->CallDoubleMethod(doubleObject, doubleValueMethod);
86 result.push_back(value);
90 void JniVector::addElementsCpp(jobject vector, int size, std::vector<std::string> &result)
92 static jmethodID getMethodID = m_env->GetMethodID(gSimulatorClassRefs.vectorCls,
93 "get", "(I)Ljava/lang/Object;");
95 for (int index = 0; index < size; index++)
97 jstring stringObject = (jstring) m_env->CallObjectMethod(vector, getMethodID, index);
98 JniString value(m_env, stringObject);
99 result.push_back(value.get());