From ec994e5bf613b1edc9ae9d1eed88276675d296e3 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Wed, 23 Nov 2016 16:02:16 -0800 Subject: [PATCH] Code refactoring for skeleton generation in C++ and Java, and a bit of clean-up --- iotjava/iotpolicy/IoTCompiler.java | 329 +++++++++++------------------ 1 file changed, 126 insertions(+), 203 deletions(-) diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 36ef1a8..180de2e 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -1592,47 +1592,11 @@ public class IoTCompiler { /** - * HELPER: writeMethodHelperStructJavaSkeleton() writes the struct in skeleton + * HELPER: writeMethodHelperReturnJavaSkeleton() writes the return value part in skeleton */ - private void writeMethodHelperStructJavaSkeleton(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, Set callbackClasses) { + private void writeMethodHelperReturnJavaSkeleton(InterfaceDecl intDecl, List methParams, + List methPrmTypes, String method, boolean isCallbackMethod, String callbackType) { - // Generate array of parameter objects - boolean isCallbackMethod = false; - String callbackType = null; - print("int paramLen = "); - writeLengthStructParamClassSkeleton(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; - println("paramCls[pos] = int.class;"); - println("paramClsGen[pos++] = null;"); - } 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 mapStubParam = null; if (isCallbackMethod) @@ -1679,7 +1643,54 @@ public class IoTCompiler { println("ex.printStackTrace();"); println("throw new Error(\"Exception from callback object instantiation!\");"); println("}"); - } + } + } + + + /** + * HELPER: writeMethodHelperStructJavaSkeleton() writes the struct in skeleton + */ + private void writeMethodHelperStructJavaSkeleton(InterfaceDecl intDecl, List methParams, + List methPrmTypes, String method, Set callbackClasses) { + + // Generate array of parameter objects + boolean isCallbackMethod = false; + String callbackType = null; + print("int paramLen = "); + writeLengthStructParamClassSkeleton(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; + println("paramCls[pos] = int.class;"); + println("paramClsGen[pos++] = null;"); + } 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); + // Write the return value part + writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType); } @@ -1720,51 +1731,8 @@ public class IoTCompiler { print(", "); } println(" });"); - checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes); - Map 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 { - String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); - print("(" + prmType + ") paramObj[" + 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("}"); - } + // Write the return value part + writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType); } @@ -3573,6 +3541,73 @@ public class IoTCompiler { } + /** + * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return value part in skeleton + */ + private void writeMethodInputParameters(List methParams, List methPrmTypes, + Set callbackClasses, String methodId) { + + print(methodId + "("); + for (int i = 0; i < methParams.size(); i++) { + String paramType = returnGenericCallbackType(methPrmTypes.get(i)); + if (callbackClasses.contains(paramType)) + print("stub" + i); + else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type + print("paramEnum" + i); + else if (isStructClass(getSimpleType(paramType))) // Struct type + print("paramStruct" + i); + else + print(getSimpleIdentifier(methParams.get(i))); + if (i != methParams.size() - 1) { + print(", "); + } + } + println(");"); + } + + + /** + * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return value part in skeleton + */ + private void writeMethodHelperReturnCplusSkeleton(InterfaceDecl intDecl, List methParams, + List methPrmTypes, String method, boolean isCallbackMethod, String callbackType, + String methodId, Set callbackClasses) { + + println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); + if (isCallbackMethod) + writeCallbackCplusStubGeneration(methParams, methPrmTypes, callbackType); + checkAndWriteEnumTypeCplusSkeleton(methParams, methPrmTypes); + writeStructMembersInitCplusSkeleton(intDecl, methParams, methPrmTypes, method); + // Check if this is "void" + String retType = intDecl.getMethodType(method); + // Check if this is "void" + if (retType.equals("void")) { + writeMethodInputParameters(methParams, methPrmTypes, callbackClasses, methodId); + } else { // We do have a return value + if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type + print(checkAndGetCplusType(retType) + " retEnum = "); + else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type + print(checkAndGetCplusType(retType) + " retStruct = "); + else + print(checkAndGetCplusType(retType) + " retVal = "); + writeMethodInputParameters(methParams, methPrmTypes, callbackClasses, methodId); + checkAndWriteEnumRetTypeCplusSkeleton(retType); + if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type + writeStructReturnCplusSkeleton(getSimpleArrayType(getSimpleType(retType)), retType); + if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type + println("void* retObj = &retEnumInt;"); + else + if (!isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type + println("void* retObj = &retVal;"); + String retTypeC = checkAndGetCplusType(retType); + if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type + println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);"); + else + println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(checkAndGetCplusArrayType(retTypeC)) + "\");"); + } + } + + /** * HELPER: writeStdMethodHelperBodyCplusSkeleton() writes the standard method body helper in the skeleton class */ @@ -3630,62 +3665,9 @@ public class IoTCompiler { } } println(" };"); - println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); - if (isCallbackMethod) - writeCallbackCplusStubGeneration(methParams, methPrmTypes, callbackType); - checkAndWriteEnumTypeCplusSkeleton(methParams, methPrmTypes); - String retType = intDecl.getMethodType(method); - // Check if this is "void" - if (retType.equals("void")) { - print(methodId + "("); - for (int i = 0; i < methParams.size(); i++) { - String paramType = returnGenericCallbackType(methPrmTypes.get(i)); - if (callbackClasses.contains(paramType)) - print("stub" + i); - else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type - print("paramEnum" + i); - else - print(getSimpleIdentifier(methParams.get(i))); - if (i != methParams.size() - 1) { - print(", "); - } - } - println(");"); - } else { // We do have a return value - if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type - print(checkAndGetCplusType(retType) + " retEnum = "); - else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - print(checkAndGetCplusType(retType) + " retStruct = "); - else - print(checkAndGetCplusType(retType) + " retVal = "); - print(methodId + "("); - for (int i = 0; i < methParams.size(); i++) { - String paramType = returnGenericCallbackType(methPrmTypes.get(i)); - if (callbackClasses.contains(paramType)) - print("stub" + i); - else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type - print("paramEnum" + i); - else - print(getSimpleIdentifier(methParams.get(i))); - if (i != methParams.size() - 1) { - print(", "); - } - } - println(");"); - checkAndWriteEnumRetTypeCplusSkeleton(retType); - if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - writeStructReturnCplusSkeleton(getSimpleArrayType(getSimpleType(retType)), retType); - if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type - println("void* retObj = &retEnumInt;"); - else - if (!isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - println("void* retObj = &retVal;"); - String retTypeC = checkAndGetCplusType(retType); - if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);"); - else - println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(checkAndGetCplusArrayType(retTypeC)) + "\");"); - } + // Write the return value part + writeMethodHelperReturnCplusSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, + callbackType, methodId, callbackClasses); } @@ -3871,68 +3853,9 @@ public class IoTCompiler { } } } - println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); - if (isCallbackMethod) - writeCallbackCplusStubGeneration(methParams, methPrmTypes, callbackType); - checkAndWriteEnumTypeCplusSkeleton(methParams, methPrmTypes); - writeStructMembersInitCplusSkeleton(intDecl, methParams, methPrmTypes, method); - // Check if this is "void" - String retType = intDecl.getMethodType(method); - // Check if this is "void" - if (retType.equals("void")) { - print(methodId + "("); - for (int i = 0; i < methParams.size(); i++) { - String paramType = returnGenericCallbackType(methPrmTypes.get(i)); - if (callbackClasses.contains(paramType)) - print("stub" + i); - else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type - print("paramEnum" + i); - else if (isStructClass(getSimpleType(paramType))) // Struct type - print("paramStruct" + i); - else - print(getSimpleIdentifier(methParams.get(i))); - if (i != methParams.size() - 1) { - print(", "); - } - } - println(");"); - } else { // We do have a return value - if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type - print(checkAndGetCplusType(retType) + " retEnum = "); - else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - print(checkAndGetCplusType(retType) + " retStruct = "); - else - print(checkAndGetCplusType(retType) + " retVal = "); - print(methodId + "("); - for (int i = 0; i < methParams.size(); i++) { - String paramType = returnGenericCallbackType(methPrmTypes.get(i)); - if (callbackClasses.contains(paramType)) - print("stub" + i); - else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type - print("paramEnum" + i); - else if (isStructClass(getSimpleType(paramType))) // Struct type - print("paramStruct" + i); - else - print(getSimpleIdentifier(methParams.get(i))); - if (i != methParams.size() - 1) { - print(", "); - } - } - println(");"); - checkAndWriteEnumRetTypeCplusSkeleton(retType); - if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - writeStructReturnCplusSkeleton(getSimpleArrayType(getSimpleType(retType)), retType); - if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type - println("void* retObj = &retEnumInt;"); - else - if (!isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - println("void* retObj = &retVal;"); - String retTypeC = checkAndGetCplusType(retType); - if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type - println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);"); - else - println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(checkAndGetCplusArrayType(retTypeC)) + "\");"); - } + // Write the return value part + writeMethodHelperReturnCplusSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, + callbackType, methodId, callbackClasses); } -- 2.34.1