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 da开发者_运维百科rr;
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() {
}
}
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 = ...;
}
}
}
精彩评论