/**
* HELPER: writeJavaInitCallbackPermission() writes the permission for callback
*/
- private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl) {
+ private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) {
- String method = "___initCallBack()";
- int methodNumId = intDecl.getHelperMethodNumId(method);
- Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
- for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
- String newIntface = intMeth.getKey();
- int newObjectId = getNewIntfaceObjectId(newIntface);
- println("set" + newObjectId + "Allowed.add(" + methodNumId + ");");
+ if (callbackExist) {
+ String method = "___initCallBack()";
+ int methodNumId = intDecl.getHelperMethodNumId(method);
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = getNewIntfaceObjectId(newIntface);
+ println("set" + newObjectId + "Allowed.add(" + methodNumId + ");");
+ }
}
}
for (int i = 0; i < methParams.size(); i++) {
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isEnumClass(simpleType)) {
// Check if this is enum type
if (isArray(param)) { // An array
- println("int len" + i + " = " + param + ".length;");
- println("int paramEnum" + i + "[] = new int[len];");
+ println("int len" + i + " = " + getSimpleIdentifier(param) + ".length;");
+ println("int paramEnum" + i + "[] = new int[len" + i + "];");
println("for (int i = 0; i < len" + i + "; i++) {");
- println("paramEnum" + i + "[i] = " + param + "[i].ordinal();");
+ println("paramEnum" + i + "[i] = " + getSimpleIdentifier(param) + "[i].ordinal();");
println("}");
} else if (isList(paramType)) { // A list
- println("int len" + i + " = " + param + ".size();");
- println("int paramEnum" + i + "[] = new int[len];");
+ println("int len" + i + " = " + getSimpleIdentifier(param) + ".size();");
+ println("int paramEnum" + i + "[] = new int[len" + i + "];");
println("for (int i = 0; i < len" + i + "; i++) {");
- println("paramEnum" + i + "[i] = " + param + ".get(i).ordinal();");
+ println("paramEnum" + i + "[i] = " + getSimpleIdentifier(param) + ".get(i).ordinal();");
println("}");
} else { // Just one element
println("int paramEnum" + i + "[] = new int[1];");
private void checkAndWriteEnumRetTypeJavaStub(String retType) {
// Strips off array "[]" for return type
- String pureType = getSimpleArrayType(getSimpleType(retType));
+ String pureType = getSimpleArrayType(getGenericType(retType));
// Take the inner type of generic
if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(retType)[0];
+ pureType = getGenericType(retType);
if (isEnumClass(pureType)) {
// Check if this is enum type
// Enum decoder
if (isStructClass(getGenericType(getSimpleArrayType(retType)))) {
writeStructReturnJavaStub(getGenericType(getSimpleArrayType(retType)), retType);
} else {
- // Check if the return value NONPRIMITIVES
- if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) {
- String[] retGenValType = getTypeOfGeneric(retType);
- println("Class<?> retGenValType = " + retGenValType[0] + ".class;");
- println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);");
- println("return (" + retType + ")retObj;");
- } else if (getParamCategory(retType) == ParamCategory.ENUM) {
// This is an enum type
+ if (getParamCategory(getGenericType(getSimpleArrayType(retType))) == ParamCategory.ENUM) {
println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
checkAndWriteEnumRetTypeJavaStub(retType);
+ } else if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) {
+ // Check if the return value NONPRIMITIVES
+ String retGenValType = getGenericType(retType);
+ println("Class<?> retGenValType = " + retGenValType + ".class;");
+ println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);");
+ println("return (" + retType + ")retObj;");
} else {
println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
println("return (" + retType + ")retObj;");
/**
* HELPER: writeConstructorJavaSkeleton() writes the constructor of the skeleton class
*/
- private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
+ private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods, boolean callbackExist) {
println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
println("mainObj = _mainObj;");
println("rmiObj = new IoTRMIObject(_port);");
// Generate permission control initialization
writeConstructorJavaPermission(intface);
- writeJavaInitCallbackPermission(intface, intDecl);
+ writeJavaInitCallbackPermission(intface, intDecl, callbackExist);
writeStructPermissionJavaSkeleton(methods, intDecl, intface);
println("___waitRequestInvokeMethod();");
println("}\n");
for (int i = 0; i < methParams.size(); i++) {
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isEnumClass(simpleType)) {
// Check if this is enum type
println("int paramInt" + i + "[] = (int[]) paramObj[" + i + "];");
println(simpleType + "[] enumVals = " + simpleType + ".values();");
if (isArray(param)) { // An array
println("int len" + i + " = paramInt" + i + ".length;");
- println(simpleType + "[] paramEnum = new " + simpleType + "[len];");
+ println(simpleType + "[] paramEnum" + i + " = new " + simpleType + "[len" + i + "];");
println("for (int i = 0; i < len" + i + "; i++) {");
- println("paramEnum[i] = enumVals[paramInt" + i + "[i]];");
+ println("paramEnum" + i + "[i] = enumVals[paramInt" + i + "[i]];");
println("}");
} else if (isList(paramType)) { // A list
println("int len" + i + " = paramInt" + i + ".length;");
- println("List<" + simpleType + "> paramEnum = new ArrayList<" + simpleType + ">();");
+ println("List<" + simpleType + "> paramEnum" + i + " = new ArrayList<" + simpleType + ">();");
println("for (int i = 0; i < len" + i + "; i++) {");
- println("paramEnum.add(enumVals[paramInt" + i + "[i]]);");
+ println("paramEnum" + i + ".add(enumVals[paramInt" + i + "[i]]);");
println("}");
} else { // Just one element
println(simpleType + " paramEnum" + i + " = enumVals[paramInt" + i + "[0]];");
private void checkAndWriteEnumRetTypeJavaSkeleton(String retType, String methodId) {
// Strips off array "[]" for return type
- String pureType = getSimpleArrayType(getSimpleType(retType));
+ String pureType = getSimpleArrayType(getGenericType(retType));
// Take the inner type of generic
if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(retType)[0];
+ pureType = getGenericType(retType);
if (isEnumClass(pureType)) {
// Check if this is enum type
// Enum decoder
private void checkAndWriteEnumRetConvJavaSkeleton(String retType) {
// Strips off array "[]" for return type
- String pureType = getSimpleArrayType(getSimpleType(retType));
+ String pureType = getSimpleArrayType(getGenericType(retType));
// Take the inner type of generic
if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(retType)[0];
+ pureType = getGenericType(retType);
if (isEnumClass(pureType)) {
// Check if this is enum type
if (isArray(retType)) { // An array
println("}");
} else if (isList(retType)) { // A list
println("int retLen = retEnum.size();");
- println("List<" + pureType + "> retEnumVal = new ArrayList<" + pureType + ">();");
+ println("int[] retEnumVal = new int[retLen];");
println("for (int i = 0; i < retLen; i++) {");
- println("retEnumVal.add(retEnum[i].ordinal());");
+ println("retEnumVal[i] = retEnum.get(i).ordinal();");
println("}");
} else { // Just one element
println("int[] retEnumVal = new int[1];");
String retType = intDecl.getMethodType(method);
if (retType.equals("void")) {
print(intDecl.getMethodId(method) + "(");
- } else if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) { // Enum type
+ } else if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type
checkAndWriteEnumRetTypeJavaSkeleton(retType, intDecl.getMethodId(method));
- } else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) { // Struct type
+ } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type
print(retType + " retStruct = " + intDecl.getMethodId(method) + "(");
} else { // We do have a return value
print("Object retObj = " + intDecl.getMethodId(method) + "(");
if (isCallbackMethod) {
print(mapStubParam.get(i)); // Get the callback parameter
- } else if (isEnumClass(getSimpleType(methPrmTypes.get(i)))) { // Enum class
+ } else if (isEnumClass(getGenericType(methPrmTypes.get(i)))) { // Enum class
print(getEnumParam(methPrmTypes.get(i), methParams.get(i), i));
- } else if (isStructClass(getSimpleType(methPrmTypes.get(i)))) {
+ } else if (isStructClass(getGenericType(methPrmTypes.get(i)))) {
print("paramStruct" + i);
} else {
String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
}
println(");");
if (!retType.equals("void")) {
- if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) { // Enum type
+ if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type
checkAndWriteEnumRetConvJavaSkeleton(retType);
println("rmiObj.sendReturnObj(retObj);");
- } else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) { // Struct type
- writeStructReturnJavaSkeleton(getSimpleArrayType(getSimpleType(retType)), retType);
+ } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type
+ writeStructReturnJavaSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
println("rmiObj.sendReturnObj(retCls, retObj);");
} else
println("rmiObj.sendReturnObj(retObj);");
print("new Class<?>[] { ");
for (int i = 0; i < methParams.size(); i++) {
String prmType = methPrmTypes.get(i);
- if (getParamCategory(prmType) == ParamCategory.NONPRIMITIVES)
- print(getTypeOfGeneric(prmType)[0] + ".class");
+ if ((getParamCategory(prmType) == ParamCategory.NONPRIMITIVES) &&
+ !isEnumClass(getGenericType(prmType)))
+ print(getGenericType(prmType) + ".class");
else
print("null");
if (i != methParams.size() - 1)
// Write properties
writePropertiesJavaSkeleton(intface, callbackExist, intDecl);
// Write constructor
- writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods);
+ writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
// Write methods
writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false);
// Write method helper
List<String> methParams = intDecl.getMethodParams(method);
List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-
- //System.out.println("\n\nMethod return type: " + checkAndGetCplusType(intDecl.getMethodType(method)) + "\n\n");
-
print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
intDecl.getMethodId(method) + "(");
boolean isCallbackMethod = false;
if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
print("\"int\"");
} else { // Generate normal classes if it's not a callback object
- //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
- //String prmType = getSimpleType(getEnumType(paramTypeC));
String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
- String prmType = getEnumCplusClsType(paramTypeC);
- print("\"" + prmType + "\"");
+ print("\"" + paramTypeC + "\"");
}
if (i != methParams.size() - 1) // Check if this is the last element
print(", ");
for (int i = 0; i < methParams.size(); i++) {
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
- if (isEnumClass(simpleType)) {
+ if (isEnumClass(getGenericType(paramType))) {
// Check if this is enum type
if (isArrayOrList(paramType, param)) { // An array or vector
- println("int len" + i + " = " + param + ".size();");
- println("vector<int> paramEnum" + i + "(len);");
+ println("int len" + i + " = " + getSimpleIdentifier(param) + ".size();");
+ println("vector<int> paramEnum" + i + "(len" + i + ");");
println("for (int i = 0; i < len" + i + "; i++) {");
- println("paramEnum" + i + "[i] = (int) " + param + "[i];");
+ println("paramEnum" + i + "[i] = (int) " + getSimpleIdentifier(param) + "[i];");
println("}");
} else { // Just one element
println("vector<int> paramEnum" + i + "(1);");
private void checkAndWriteEnumRetTypeCplusStub(String retType) {
// Strips off array "[]" for return type
- String pureType = getSimpleArrayType(getSimpleType(retType));
+ String pureType = getSimpleArrayType(getGenericType(retType));
// Take the inner type of generic
if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(retType)[0];
+ pureType = getGenericType(retType);
if (isEnumClass(pureType)) {
// Check if this is enum type
println("vector<int> retEnumInt;");
checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method);
println("int methodId = " + intDecl.getMethodNumId(method) + ";");
String retType = intDecl.getMethodType(method);
- //String retTypeC = checkAndGetCplusType(retType);
- //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";");
println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
// Generate array of parameter types
if (isStructPresent(methParams, methPrmTypes)) {
println("int numParam = " + methParams.size() + ";");
print("string paramCls[] = { ");
for (int i = 0; i < methParams.size(); i++) {
- //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
- //String prmType = getSimpleType(getEnumType(paramTypeC));
String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
- String prmType = getEnumCplusClsType(paramTypeC);
- print("\"" + prmType + "\"");
+ print("\"" + paramTypeC + "\"");
// Check if this is the last element (don't print a comma)
if (i != methParams.size() - 1) {
print(", ");
writeStructReturnCplusStub(getGenericType(getSimpleArrayType(retType)), retType);
} else {
// Check if the return value NONPRIMITIVES
- if (getParamCategory(retType) == ParamCategory.ENUM) {
+ if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) {
checkAndWriteEnumRetTypeCplusStub(retType);
} else {
//if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
for (int i = 0; i < methParams.size(); i++) {
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isEnumClass(simpleType)) {
// Check if this is enum type
if (isArrayOrList(paramType, param)) { // An array
private void checkAndWriteEnumRetTypeCplusSkeleton(String retType) {
// Strips off array "[]" for return type
- String pureType = getSimpleArrayType(getSimpleType(retType));
+ String pureType = getSimpleArrayType(getGenericType(retType));
// Take the inner type of generic
if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
pureType = getTypeOfGeneric(retType)[0];
String paramType = returnGenericCallbackType(methPrmTypes.get(i));
if (callbackClasses.contains(paramType))
print("stub" + i);
- else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type
+ else if (isEnumClass(getGenericType(paramType))) // Check if this is enum type
print("paramEnum" + i);
- else if (isStructClass(getSimpleType(paramType))) // Struct type
+ else if (isStructClass(getGenericType(paramType))) // Struct type
print("paramStruct" + i);
else
print(getSimpleIdentifier(methParams.get(i)));
if (retType.equals("void")) {
writeMethodInputParameters(methParams, methPrmTypes, callbackClasses, methodId);
} else { // We do have a return value
- if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type
+ if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) // Enum type
print(checkAndGetCplusType(retType) + " retEnum = ");
- else if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type
+ else if (isStructClass(getSimpleArrayType(getGenericType(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
+ if (isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+ writeStructReturnCplusSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
+ if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) // Enum type
println("void* retObj = &retEnumInt;");
else
- if (!isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type
+ if (!isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
println("void* retObj = &retVal;");
String retTypeC = checkAndGetCplusType(retType);
- if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type
+ if (isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);");
else
println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusRetClsType(getEnumType(retType)) + "\");");
callbackType = paramType;
print("\"int\"");
} else { // Generate normal classes if it's not a callback object
- //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
- //String prmType = getSimpleType(getEnumType(paramTypeC));
String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
- String prmType = getEnumCplusClsType(paramTypeC);
- print("\"" + prmType + "\"");
+ print("\"" + paramTypeC + "\"");
}
if (i != methParams.size() - 1) {
print(", ");
for (int i = 0; i < methParams.size(); i++) {
String paramType = returnGenericCallbackType(methPrmTypes.get(i));
if (!callbackClasses.contains(paramType)) {
- String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
- if (isEnumClass(getSimpleType(methPrmType))) { // Check if this is enum type
+ String methParamType = methPrmTypes.get(i);
+ if (isEnumClass(getSimpleArrayType(getGenericType(methParamType)))) {
+ // Check if this is enum type
println("vector<int> paramEnumInt" + i + ";");
} else {
+ String methPrmType = checkAndGetCplusType(methParamType);
String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
println(methParamComplete + ";");
}
String paramType = returnGenericCallbackType(methPrmTypes.get(i));
if (callbackClasses.contains(paramType))
print("&numStubs" + i);
- else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type
+ else if (isEnumClass(getGenericType(paramType))) // Check if this is enum type
print("¶mEnumInt" + i);
else
print("&" + getSimpleIdentifier(methParams.get(i)));
} else { // Generate normal classes if it's not a callback object
String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
String prmTypeC = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
- if (isEnumClass(getSimpleType(paramTypeC))) { // Check if this is enum type
+ if (isEnumClass(getGenericType(paramTypeC))) { // Check if this is enum type
println("vector<int> paramEnumInt" + i + ";");
} else {
String methParamComplete = checkAndGetCplusArray(paramTypeC, methParams.get(i));
println(methParamComplete + ";");
}
println("paramCls[pos] = \"" + getEnumType(prmTypeC) + "\";");
- if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type
+ if (isEnumClass(getGenericType(paramType))) // Check if this is enum type
println("paramObj[pos++] = ¶mEnumInt" + i);
else
println("paramObj[pos++] = &" + getSimpleIdentifier(methParams.get(i)) + ";");
/**
* This function converts Java to C++ type for compilation
*/
- private String convertType(String jType) {
+ private String convertType(String type) {
- return mapPrimitives.get(jType);
+ if (mapPrimitives.containsKey(type))
+ return mapPrimitives.get(type);
+ else
+ return type;
}
String pureType = getSimpleArrayType(type);
// Take the inner type of generic
if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(type)[0];
+ pureType = getGenericType(type);
if (isEnumClass(pureType)) {
String enumType = "int[]";
return enumType;
String pureType = getSimpleArrayType(type);
// Take the inner type of generic
if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(type)[0];
+ pureType = getGenericType(type);
if (isEnumClass(pureType)) {
String enumType = "int*";
return enumType;
String pureType = getSimpleArrayType(type);
// Take the inner type of generic
if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
- pureType = getTypeOfGeneric(type)[0];
+ pureType = getGenericType(type);
if (isStructClass(pureType)) {
String structType = "int";
return structType;
// - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
private String checkAndGetCplusArgClsType(String paramType, String param) {
- String paramTypeRet = null;
+ String paramTypeRet = getEnumCplusClsType(paramType);
+ if (!paramTypeRet.equals(paramType))
+ // Just return if it is an enum type
+ // Type will still be the same if it's not an enum type
+ return paramTypeRet;
+
// Check for array declaration
if (param.contains("[]")) {
paramTypeRet = getSimpleArrayType(paramType) + "*";