#include "CplusToJava.h" #include jobjectArray CplusToJava::GetjobjectArray(JNIEnv* env, jclass jcls, jobject jstruct, std::string var) { // 获取成员变量var的值 jfieldID fieldIDlist = env->GetFieldID(jcls, var.c_str(), "Ljava/util/List;"); jobject listObject = env->GetObjectField(jstruct, fieldIDlist); jclass listClass = env->FindClass("java/util/List"); jmethodID toArrayMethodID = env->GetMethodID(listClass, "toArray", "()[Ljava/lang/Object;"); jobjectArray listValue = (jobjectArray)env->CallObjectMethod(listObject, toArrayMethodID); return listValue; } jobjectArray CplusToJava::QVectorD2jobjectArray(QVector vec, JNIEnv* env) { jint arraySize = vec.size(); jclass doubleClass = env->FindClass("java/lang/Double"); jobjectArray array = env->NewObjectArray(arraySize, doubleClass, NULL); jmethodID constructorID = env->GetMethodID(doubleClass, "", "(D)V"); for (int i = 0; i < arraySize; i++) { jobject doubleObject = env->NewObject(doubleClass, constructorID, vec[i]); env->SetObjectArrayElement(array, i, doubleObject); } return array; } jobjectArray CplusToJava::QVectorQS2jobjectArray(QVector vec, JNIEnv* env) { jint arraySize = vec.size(); jclass stringClass = env->FindClass("java/lang/String"); jobjectArray array = env->NewObjectArray(arraySize, stringClass, NULL); jmethodID constructorID = env->GetMethodID(stringClass, "", "(Ljava/lang/String;)V"); for (int i = 0; i < arraySize; i++) { jstring tempstr = env->NewStringUTF(vec[i].toStdString().c_str()); jobject stringObject = env->NewObject(stringClass, constructorID, tempstr); env->SetObjectArrayElement(array, i, stringObject); } return array; } jobjectArray CplusToJava::QVectorL2jobjectArray(QVector vec, JNIEnv* env) { jint arraySize = vec.size(); jclass longClass = env->FindClass("java/lang/Long"); jobjectArray array = env->NewObjectArray(arraySize, longClass, NULL); jmethodID constructorID = env->GetMethodID(longClass, "", "(J)V"); for (int i = 0; i < arraySize; i++) { jobject longObject = env->NewObject(longClass, constructorID, vec[i]); env->SetObjectArrayElement(array, i, longObject); } return array; } jobjectArray CplusToJava::QVectorLL2jobjectArray(QVector vec, JNIEnv* env) { jint arraySize = vec.size(); jclass longClass = env->FindClass("java/lang/Long"); jobjectArray array = env->NewObjectArray(arraySize, longClass, NULL); jmethodID constructorID = env->GetMethodID(longClass, "", "(J)V"); for (int i = 0; i < arraySize; i++) { jobject longObject = env->NewObject(longClass, constructorID, vec[i]); env->SetObjectArrayElement(array, i, longObject); } return array; } jobjectArray CplusToJava::QVectorI2jobjectArray(QVector vec, JNIEnv* env) { jint arraySize = vec.size(); jclass intClass = env->FindClass("java/lang/Integer"); jobjectArray array = env->NewObjectArray(arraySize, intClass, NULL); jmethodID constructorID = env->GetMethodID(intClass, "", "(I)V"); for (int i = 0; i < arraySize; i++) { jobject intObject = env->NewObject(intClass, constructorID, vec[i]); env->SetObjectArrayElement(array, i, intObject); } return array; } jobjectArray CplusToJava::QVectorS2jobjectArray(QVector vec, JNIEnv* env) { jint arraySize = vec.size(); jclass intClass = env->FindClass("java/lang/Short"); jobjectArray array = env->NewObjectArray(arraySize, intClass, NULL); jmethodID constructorID = env->GetMethodID(intClass, "", "(S)V"); for (int i = 0; i < arraySize; i++) { jobject intObject = env->NewObject(intClass, constructorID, vec[i]); env->SetObjectArrayElement(array, i, intObject); } return array; } jobject CplusToJava::Createjobject(JNIEnv* env, jint esize, jobjectArray array) { jobject arrayList = env->NewObject(env->FindClass("java/util/ArrayList"), env->GetMethodID(env->FindClass("java/util/ArrayList"), "", "()V")); jmethodID addMethodID = env->GetMethodID(env->FindClass("java/util/ArrayList"), "add", "(Ljava/lang/Object;)Z"); for (int i = 0; i < esize; i++) { jobject element = env->GetObjectArrayElement(array, i); env->CallBooleanMethod(arrayList, addMethodID, element); } return arrayList; } jobject CplusToJava::Create2Djobject(JNIEnv *env, jint esize, jobjectArray array, jint row, jint col) { jclass byteArrayClass = env->FindClass("[J"); jobjectArray result = env->NewObjectArray(row, byteArrayClass, NULL); for (int r = 0; r < row; r++) { jlongArray innerArray = env->NewLongArray(col); jlong longArray[col]; for (int c = 0; c < col; c++) { jint pos = r * col + c; jobject element = env->GetObjectArrayElement(array, pos); jlong v = env->CallLongMethod(element, env->GetMethodID(env->FindClass("java/lang/Long"), "longValue", "()J")); longArray[c] = v; } env->SetLongArrayRegion(innerArray, 0, col, longArray); env->SetObjectArrayElement(result, r, innerArray); } return result; } jobjectArray CplusToJava::jobject2jobjectArray(JNIEnv* env, jobject array) { jclass listClass = env->FindClass("java/util/List"); jmethodID toArrayMethodID = env->GetMethodID(listClass, "toArray", "()[Ljava/lang/Object;"); jobjectArray listValue = (jobjectArray)env->CallObjectMethod(array, toArrayMethodID); return listValue; } QVector CplusToJava::jobjectArray2QVectorD(JNIEnv* env, jobjectArray listValue) { // 处理数组元素 jsize arrayLen = env->GetArrayLength(listValue); QVector results; for (int i = 0; i < arrayLen; i++) { jobject element = env->GetObjectArrayElement(listValue, i); jdouble value = env->CallDoubleMethod(element, env->GetMethodID(env->FindClass("java/lang/Double"), "doubleValue", "()D")); results.push_back(value); } return results; } QString CplusToJava::jstring2QString(JNIEnv* env, jstring jstr) { const char* str = env->GetStringUTFChars(jstr, NULL); std::string filename(str); QString qstr = QString::fromStdString(filename); // 释放字符串内存 env->ReleaseStringUTFChars(jstr, str); return qstr; } QString CplusToJava::getStructString(JNIEnv* env,jclass jcls,jobject jstruct, const char* name) { jfieldID fieldIDname = env->GetFieldID(jcls, name, SIG_STRING); jstring nameValue = (jstring)env->GetObjectField(jstruct, fieldIDname); const char* aStr = env->GetStringUTFChars(nameValue, NULL); QString result(aStr); return result; } double CplusToJava::DoubleLimit(const QString& _data) { double rData = _data.toDouble(); QString first = _data; QStringList lstTemp = _data.split('.'); if (lstTemp.size() > 1) { first = lstTemp.at(0); // return rData; } if (first.size() > 3) { QString temp = QLatin1String("-"); for (int pos = 1; pos < first.size(); pos++) { temp.append('9'); } if (temp == first) { return -999; } } return rData; } double CplusToJava::DoubleLimit(const double& _data) { QString limit = QString::number(_data, 'f'); double rData = DoubleLimit(limit); return rData; } QVector CplusToJava::DoubleLimit(QVector& data) { for (int pos = 0; pos < data.size(); pos++) { data[pos] = QString::number(DoubleLimit(data.at(pos)), 'f'); } return data; } QVector CplusToJava::DoubleLimit(QVector _data) { QVector rdata; for (int pos = 0; pos < _data.size(); pos++) { rdata << QString::number(DoubleLimit(_data.at(pos)), 'f'); } return rdata; } QString CplusToJava::GetSqlBy(const QVector& _data, bool bSFlag) { QString rData; for (int pos = 0; pos < _data.size(); pos++) { if (bSFlag) { rData += QLatin1String("'"); rData += _data.at(pos); rData += QLatin1String("'"); } else { rData += _data.at(pos); } if (pos != _data.size() - 1) { rData += QLatin1String(","); } } return rData; }