JNI 和访问子类
我有一个与 C 程序通信的 Java 程序。我以前写过 JNI,但我的输出结构更简单,C 结构只包含双精度/整数和双精度/整数数组。
现在我的结构包含一个子结构(类/子类),我不知道如何更改代码以访问子类数据/字段。
我的 C 代码看起来像这样,但是如果您查看此代码下面的 Java 类,我如何访问像 DefaultFeeAmount
这样的值......我如何获取子类中的元素?
C 简单明了....
{
jclass out_rec_cls = jenv->GetObjectClass(ptrTo_out_rec);
jfieldID fldID, fldID2;
jintArray arr;
jdoubleArray darr;
jobjectArray oarr;
jsize len;//,len2;
jint *arrElems;
jdouble *darrElems;
jobject *oarrElems;
int i;
char temp_str[100],temp_str2[10000];
fldID = jenv->GetFieldID(out_rec_cls, "ErrorCode", "I");
if(fldID != NULL)
jenv->SetIntField(ptrTo_out_rec, fldID, out_rec->error_code);
}
Java
class FeeOutput {
public double DefaultFeeAmount;
public double MaximumAmount;
public int FeeID;
public int CompType;
public int Handling;
public int CapType;
public int ProfitType;
public int EffectiveDateMonth;
public int EffectiveDateDay;
public int EffectiveDateYear;
public int VendorBasedFee;
public int DealerRequestedFee;
public int DealerFixedTranFee;
public double FeeAmount;
public int FeeCompliant;
public String FeeName = "";
public FeeOutput() {
}
}
public class VFeeOutput {
public static final int NUM_FEES = 100;
public FeeOutput[] FeeStruct = new FeeOutput[NUM_FEES];
public int ErrorCode;
public String ErrorString = "";
public String Version = "";
public VFeeOutput() {
}
}
I have a Java program that communicates to a C program. I have written JNI before but my output structure was more simplistic and the C structure just contained doubles/ints and arrays of doubles/ints.
Now my structure contains a substructure (class/subclass) and I don't know how to change the code to access the subclass data/fields.
My C code looked like this but how do I access a value like DefaultFeeAmount
if you look at my Java Class below this code....how do I get to the elements within the subclass?
C straightforward....
{
jclass out_rec_cls = jenv->GetObjectClass(ptrTo_out_rec);
jfieldID fldID, fldID2;
jintArray arr;
jdoubleArray darr;
jobjectArray oarr;
jsize len;//,len2;
jint *arrElems;
jdouble *darrElems;
jobject *oarrElems;
int i;
char temp_str[100],temp_str2[10000];
fldID = jenv->GetFieldID(out_rec_cls, "ErrorCode", "I");
if(fldID != NULL)
jenv->SetIntField(ptrTo_out_rec, fldID, out_rec->error_code);
}
Java
class FeeOutput {
public double DefaultFeeAmount;
public double MaximumAmount;
public int FeeID;
public int CompType;
public int Handling;
public int CapType;
public int ProfitType;
public int EffectiveDateMonth;
public int EffectiveDateDay;
public int EffectiveDateYear;
public int VendorBasedFee;
public int DealerRequestedFee;
public int DealerFixedTranFee;
public double FeeAmount;
public int FeeCompliant;
public String FeeName = "";
public FeeOutput() {
}
}
public class VFeeOutput {
public static final int NUM_FEES = 100;
public FeeOutput[] FeeStruct = new FeeOutput[NUM_FEES];
public int ErrorCode;
public String ErrorString = "";
public String Version = "";
public VFeeOutput() {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一个广泛使用的 Java 约定技巧,变量名请以小写字母开头。下面介绍如何访问 Java 中的“struct”字段。
As a wide-spread Java convention tip, please start variable names with lower case. Here how you can access "struct" fields in Java.