AnalysisSystemForRadionucli.../CplusToJava.cpp
2024-09-21 10:28:30 +08:00

171 lines
6.6 KiB
C++

#include "CplusToJava.h"
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::QVectorI2jobjectArray(QVector<int> vec, JNIEnv* env)
{
jint arraySize = vec.size();
jclass intClass = env->FindClass("java/lang/Double");
jobjectArray array = env->NewObjectArray(arraySize, intClass, NULL);
jmethodID constructorID = env->GetMethodID(intClass, "<init>", "(D)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;
}
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;
}
std::vector<double> CplusToJava::jobjectArray2vectorD(JNIEnv* env, jobjectArray listValue)
{
// ´¦ÀíÊý×éÔªËØ
jsize arrayLen = env->GetArrayLength(listValue);
std::vector<double> results;
for (int i = 0; i < arrayLen; i++) {
jobject element = env->GetObjectArrayElement(listValue, i);
if (element)
{
jdouble value = env->CallDoubleMethod(element, env->GetMethodID(env->FindClass("java/lang/Double"), "doubleValue", "()D"));
results.push_back(value);
}
else
{
results.push_back(qQNaN());
}
}
return results;
}
jobjectArray CplusToJava::VectorD2jobjectArray(std::vector<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::uvec2jobjectArray(arma::uvec 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;
}