JNI和访问子类

时间:2011-09-19 18:13:50

标签: java c java-native-interface

我有一个与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);
}

爪哇

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() {
    }
}

1 个答案:

答案 0 :(得分:0)

作为一个广泛的Java常规提示,请使用小写字母启动变量名称。在这里您可以如何访问Java中的“struct”字段。

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() {
    }

    private void loopThoughtFeeOutput() {
        for(FeeOutput feeOutput : FeeStruct) {
            feeOutput.CompType = ...;
        }
        // or
        for(int i = 0; i < FeeStruct.length; i++) {
            FeeStruct[0].CompType = ...;
        }
    }
}