AnalysisSystemForRadionucli.../CplusToJava.cpp
2024-09-11 16:44:40 +08:00

237 lines
8.2 KiB
C++

#include "CplusToJava.h"
#include <QStringList>
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<double> 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, "<init>", "(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<QString> 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, "<init>", "(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<long> 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, "<init>", "(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<long long> 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, "<init>", "(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<int> 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, "<init>", "(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<short> 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, "<init>", "(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"), "<init>", "()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<double> CplusToJava::jobjectArray2QVectorD(JNIEnv* env, jobjectArray listValue)
{
// ´¦ÀíÊý×éÔªËØ
jsize arrayLen = env->GetArrayLength(listValue);
QVector<double> 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<QString> CplusToJava::DoubleLimit(QVector<QString>& data)
{
for (int pos = 0; pos < data.size(); pos++)
{
data[pos] = QString::number(DoubleLimit(data.at(pos)), 'f');
}
return data;
}
QVector<QString> CplusToJava::DoubleLimit(QVector<double> _data)
{
QVector<QString> rdata;
for (int pos = 0; pos < _data.size(); pos++)
{
rdata << QString::number(DoubleLimit(_data.at(pos)), 'f');
}
return rdata;
}
QString CplusToJava::GetSqlBy(const QVector<QString>& _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;
}