+
+
+ /**
+ * HELPER: writeLengthStructParamClassJavaSkeleton() writes lengths of params
+ */
+ private void writeLengthStructParamClassJavaSkeleton(List<String> methParams, List<String> methPrmTypes,
+ String method, InterfaceDecl intDecl) {
+
+ // Iterate and find struct declarations - count number of params
+ for (int i = 0; i < methParams.size(); i++) {
+ String paramType = methPrmTypes.get(i);
+ String param = methParams.get(i);
+ String simpleType = getGenericType(paramType);
+ if (isStructClass(simpleType)) {
+ int members = getNumOfMembers(simpleType);
+ print(Integer.toString(members) + "*");
+ int methodNumId = intDecl.getMethodNumId(method);
+ print("struct" + methodNumId + "Size" + i);
+ } else
+ print("1");
+ if (i != methParams.size() - 1) {
+ print("+");
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: writeStructMembersJavaSkeleton() writes parameters of struct
+ */
+ private void writeStructMembersJavaSkeleton(String simpleType, String paramType,
+ String param, String method, InterfaceDecl intDecl, int iVar) {
+
+ // Get the struct declaration for this struct and generate initialization code
+ StructDecl structDecl = getStructDecl(simpleType);
+ List<String> memTypes = structDecl.getMemberTypes(simpleType);
+ List<String> members = structDecl.getMembers(simpleType);
+ if (isArrayOrList(param, paramType)) { // An array or list
+ int methodNumId = intDecl.getMethodNumId(method);
+ String counter = "struct" + methodNumId + "Size" + iVar;
+ println("for(int i = 0; i < " + counter + "; i++) {");
+ }
+ println("int pos = 0;");
+ if (isArrayOrList(param, paramType)) { // An array or list
+ println("for(int i = 0; i < retLen; i++) {");
+ for (int i = 0; i < members.size(); i++) {
+ String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+ println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+ println("paramClsGen[pos++] = null;");
+ }
+ println("}");
+ } else { // Just one struct element
+ for (int i = 0; i < members.size(); i++) {
+ String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+ println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+ println("paramClsGen[pos++] = null;");
+ }
+ }
+ }
+
+
+
+ /**
+ * HELPER: writeStructMembersInitJavaSkeleton() writes parameters of struct
+ */
+ private void writeStructMembersInitJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
+ List<String> methPrmTypes, String method) {
+
+ for (int i = 0; i < methParams.size(); i++) {
+ String paramType = methPrmTypes.get(i);
+ String param = methParams.get(i);
+ String simpleType = getGenericType(paramType);
+ if (isStructClass(simpleType)) {
+ int methodNumId = intDecl.getMethodNumId(method);
+ String counter = "struct" + methodNumId + "Size" + i;
+ // Declaration
+ if (isArray(param)) { // An array
+ println(simpleType + "[] paramStruct" + i + " = new " + simpleType + "[" + counter + "];");
+ println("for(int i = 0; i < " + counter + "; i++) {");
+ println("paramStruct" + i + "[i] = new " + simpleType + "();");
+ println("}");
+ } else if (isList(paramType)) { // A list
+ println("List<" + simpleType + "> structRet = new ArrayList<" + simpleType + ">();");
+ } else
+ println(simpleType + " paramStruct" + i + " = new " + simpleType + "();");
+ println("int objPos = 0;");
+ // Initialize members
+ StructDecl structDecl = getStructDecl(simpleType);
+ List<String> members = structDecl.getMembers(simpleType);
+ List<String> memTypes = structDecl.getMemberTypes(simpleType);
+ if (isArrayOrList(param, paramType)) { // An array or list
+ println("for(int i = 0; i < " + counter + "; i++) {");
+ }
+ if (isArray(param)) { // An array
+ for (int j = 0; j < members.size(); j++) {
+ String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
+ print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j)));
+ println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
+ }
+ println("}");
+ } else if (isList(paramType)) { // A list
+ println(simpleType + " paramStructMem = new " + simpleType + "();");
+ for (int j = 0; j < members.size(); j++) {
+ String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
+ print("paramStructMem." + getSimpleIdentifier(members.get(j)));
+ println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
+ }
+ println("paramStruct" + i + ".add(paramStructMem);");
+ println("}");
+ } else { // Just one struct element
+ for (int j = 0; j < members.size(); j++) {
+ String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
+ print("paramStruct" + i + "." + getSimpleIdentifier(members.get(j)));
+ println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
+ }
+ }
+ } else {
+ // Take offsets of parameters
+ println("int offset" + i +" = objPos;");
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: writeStructReturnJavaSkeleton() writes parameters if struct is present
+ */
+ private void writeStructReturnJavaSkeleton(String simpleType, String retType) {
+
+ // Minimum retLen is 1 if this is a single struct object
+ if (isArray(retType))
+ println("int retLen = retStruct.length;");
+ else if (isList(retType))
+ println("int retLen = retStruct.size();");
+ else // Just single struct object
+ println("int retLen = 1;");
+ println("Object retLenObj = retLen;");
+ println("rmiObj.sendReturnObj(retLenObj);");
+ int numMem = getNumOfMembers(simpleType);
+ println("Class<?>[] retCls = new Class<?>[" + numMem + "*retLen];");
+ println("Object[] retObj = new Object[" + numMem + "*retLen];");
+ println("int retPos = 0;");
+ // Get the struct declaration for this struct and generate initialization code
+ StructDecl structDecl = getStructDecl(simpleType);
+ List<String> memTypes = structDecl.getMemberTypes(simpleType);
+ List<String> members = structDecl.getMembers(simpleType);
+ if (isArrayOrList(retType, retType)) { // An array or list
+ println("for(int i = 0; i < retLen; i++) {");
+ for (int i = 0; i < members.size(); i++) {
+ String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+ println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+ print("retObj[retPos++] = retStruct[i].");
+ print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+ println(";");
+ }
+ println("}");
+ } else { // Just one struct element
+ for (int i = 0; i < members.size(); i++) {
+ String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+ println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+ print("retObj[retPos++] = retStruct.");
+ print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+ println(";");
+ }
+ }
+
+ }
+
+
+ /**
+ * HELPER: writeMethodHelperStructJavaSkeleton() writes the struct in skeleton
+ */
+ private void writeMethodHelperStructJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
+ List<String> methPrmTypes, String method, Set<String> callbackClasses) {
+
+ // Generate array of parameter objects
+ boolean isCallbackMethod = false;
+ String callbackType = null;
+ print("int paramLen = ");
+ writeLengthStructParamClassJavaSkeleton(methParams, methPrmTypes, method, intDecl);
+ println(";");
+ println("Class<?>[] paramCls = new Class<?>[paramLen];");
+ println("Class<?>[] paramClsGen = new Class<?>[paramLen];");
+ // Iterate again over the parameters
+ for (int i = 0; i < methParams.size(); i++) {
+ String paramType = methPrmTypes.get(i);
+ String param = methParams.get(i);
+ String simpleType = getGenericType(paramType);
+ if (isStructClass(simpleType)) {
+ writeStructMembersJavaSkeleton(simpleType, paramType, param, method, intDecl, i);
+ } else {
+ String prmType = returnGenericCallbackType(methPrmTypes.get(i));
+ if (callbackClasses.contains(prmType)) {
+ isCallbackMethod = true;
+ callbackType = prmType;
+ print("int.class");
+ } else { // Generate normal classes if it's not a callback object
+ String paramTypeOth = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+ println("paramCls[pos] = " + getSimpleType(getEnumType(paramTypeOth)) + ".class;");
+ print("paramClsGen[pos++] = ");
+ String prmTypeOth = methPrmTypes.get(i);
+ if (getParamCategory(prmTypeOth) == ParamCategory.NONPRIMITIVES)
+ println(getTypeOfGeneric(prmType)[0] + ".class;");
+ else
+ println("null;");
+ }
+ }
+ }
+ println("Object[] paramObj = rmiObj.getMethodParams(paramCls, paramClsGen);");
+ writeStructMembersInitJavaSkeleton(intDecl, methParams, methPrmTypes, method);
+ checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes);
+ Map<Integer,String> mapStubParam = null;
+ if (isCallbackMethod)
+ mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType);
+ // Check if this is "void"
+ String retType = intDecl.getMethodType(method);
+ if (retType.equals("void")) {
+ print(intDecl.getMethodId(method) + "(");
+ } else if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) { // Enum type
+ checkAndWriteEnumRetTypeJavaSkeleton(retType, intDecl.getMethodId(method));
+ } else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) { // Struct type
+ print(retType + " retStruct = " + intDecl.getMethodId(method) + "(");
+ } else { // We do have a return value
+ print("Object retObj = " + intDecl.getMethodId(method) + "(");
+ }
+ for (int i = 0; i < methParams.size(); i++) {
+
+ if (isCallbackMethod) {
+ print(mapStubParam.get(i)); // Get the callback parameter
+ } else if (isEnumClass(getSimpleType(methPrmTypes.get(i)))) { // Enum class
+ print(getEnumParam(methPrmTypes.get(i), methParams.get(i), i));
+ } else if (isStructClass(getSimpleType(methPrmTypes.get(i)))) {
+ print("paramStruct" + i);
+ } else {
+ String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+ print("(" + prmType + ") paramObj[offset" + i + "]");
+ }
+ if (i != methParams.size() - 1)
+ print(", ");
+ }
+ println(");");
+ if (!retType.equals("void")) {
+ if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) { // Enum type
+ checkAndWriteEnumRetConvJavaSkeleton(retType);
+ println("rmiObj.sendReturnObj(retObj);");
+ } else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) { // Struct type
+ writeStructReturnJavaSkeleton(getSimpleArrayType(getSimpleType(retType)), retType);
+ println("rmiObj.sendReturnObj(retCls, retObj);");
+ } else
+ println("rmiObj.sendReturnObj(retObj);");
+ }
+ if (isCallbackMethod) { // Catch exception if this is callback
+ println("} catch(Exception ex) {");
+ println("ex.printStackTrace();");
+ println("throw new Error(\"Exception from callback object instantiation!\");");
+ println("}");
+ }
+ }