Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI and a somewhat complex object array

843829Jun 23 2006 — edited Jun 26 2006
Ok, I have a C++ constructor that builds an object that looks like this:
MainClass.cpp

class Mainclass {
 public:
 
    MainClass (char *fileName);
 
    ~MainClass();
 
 private:
 
... some private functions ...
 
    float      version;     
    char      *MPIP_settings; 
    int        trace_depth;  
    int	       num_callsites; 
    int        num_tasks;     
    Struct1  *struct;         // Array of call site info
 
};
I need to get all of this data back into a Java object. I can easily get the primitive types and the strings back to Java simply by setting the field of the Java class I have to take in the data:
MainClass.java

public class MainClass {
 
	int        	trace_depth;   
    	int	       	num_callsites; 
    	int        	num_tasks;    
    	Struct1  	structOne[ ];  
    	
    	public MpipInfo() {
    		
    	}
}
The problem I am running into is when I get to the next step which involves the Struct1 object array:
Struct1.java:

public class Struct1 {
	
	 int   	siteID;	
	 SubStruct1 subOne[ ];
	 SubStruct2 subTwo[ ];
 
}
 
struct Struct1

struct Struct1{
    int   siteID;	
    SubStruct1 *subOne;        
    SubStruct2 *subTwo; 
  
};
I can't figure out how to build this object array that has other object arrays.
I try and attempt to do something like this (Not exact, probably some broken code in here while I was trying to figure it out):
extern "C" jobjectArray Java_JavaMpipWrapper_getArrays(JNIEnv *env, jobject obj) {
	
jobjectArray returnArray;

	jclass jstruct1class = (env)->FindClass("LStruct1;");
	if (jstruct1class == 0) {
		printf("Error finding struct1 class\n");
		return NULL;
	}
	jmethodID construct = (env)->GetMethodID(jstruct1class, "<init>", "(LStruct1;)V");

// I was getting a shadowed error here, so I had to add a level...
	if(1){
	returnArray =      (jobjectArray)(env)->NewObjectArray(numOfStruct1, jstruct1class, NULL);
}
	
	
	for(int i = 0; i < numOfStruct1; i++){
		jobject obj = (jobject)&tempStruct1;
jobject struct1Obj = (env)->NewObject(jstruct1class, construct, (jobject)&tempStruct1[index]);
env->SetObjectArrayElement(returnArray, i, struct1Obj);
}

return returnArray;

}



Of course that didn't work. If anyone has done something like this I'd like their opinion on the best way to handle object arrays like this, or if I am going about it completely the wrong way.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 24 2006
Added on Jun 23 2006
2 comments
817 views