JNI 和访问子类

发布于 2024-12-05 12:15:37 字数 1655 浏览 0 评论 0原文

我有一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2024-12-12 12:15:37

作为一个广泛使用的 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 = ...;
        }
    }
}

As a wide-spread Java convention tip, please start variable names with lower case. Here how you can access "struct" fields in Java.

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 = ...;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文