PRIMITIVES, // All the primitive types, e.g. byte, short, int, long, etc.
NONPRIMITIVES, // Non-primitive types, e.g. Set, Map, List, etc.
- USERDEFINED // Non-supported type by default; assumed as driver classes
+ ENUM, // Enum type
+ STRUCT, // Struct type
+ USERDEFINED // Assumed as driver classes
}
/**
/**
- * HELPER: writeEnumJava() writes the enumeration declaration
+ * HELPER: generateEnumJava() writes the enumeration declaration
*/
- private void writeEnumJava(EnumDecl enumDecl) {
+ private void generateEnumJava() throws IOException {
- Set<String> enumTypes = enumDecl.getEnumDeclarations();
- // Iterate over enum declarations
- for (String enType : enumTypes) {
-
- println("public enum " + enType + " {");
- List<String> enumMembers = enumDecl.getMembers(enType);
- for (int i = 0; i < enumMembers.size(); i++) {
-
- String member = enumMembers.get(i);
- print(member);
- // Check if this is the last element (don't print a comma)
- if (i != enumMembers.size() - 1)
- println(",");
- else
- println("");
+ // Create a new directory
+ createDirectory(dir);
+ for (String intface : mapIntfacePTH.keySet()) {
+ // Get the right EnumDecl
+ DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+ EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+ Set<String> enumTypes = enumDecl.getEnumDeclarations();
+ // Iterate over enum declarations
+ for (String enType : enumTypes) {
+ // Open a new file to write into
+ FileWriter fw = new FileWriter(dir + "/" + enType + ".java");
+ pw = new PrintWriter(new BufferedWriter(fw));
+ println("public enum " + enType + " {");
+ List<String> enumMembers = enumDecl.getMembers(enType);
+ for (int i = 0; i < enumMembers.size(); i++) {
+
+ String member = enumMembers.get(i);
+ print(member);
+ // Check if this is the last element (don't print a comma)
+ if (i != enumMembers.size() - 1)
+ println(",");
+ else
+ println("");
+ }
+ println("}\n");
+ pw.close();
+ System.out.println("IoTCompiler: Generated enum class " + enType + ".java...");
}
- println("}\n");
}
}
/**
- * HELPER: writeStructJava() writes the struct declaration
+ * HELPER: generateStructJava() writes the struct declaration
*/
- private void writeStructJava(StructDecl structDecl) {
-
- List<String> structTypes = structDecl.getStructTypes();
- // Iterate over enum declarations
- for (String stType : structTypes) {
+ private void generateStructJava() throws IOException {
- println("public class " + stType + " {");
- List<String> structMemberTypes = structDecl.getMemberTypes(stType);
- List<String> structMembers = structDecl.getMembers(stType);
- for (int i = 0; i < structMembers.size(); i++) {
-
- String memberType = structMemberTypes.get(i);
- String member = structMembers.get(i);
- println("public static " + memberType + " " + member + ";");
+ // Create a new directory
+ createDirectory(dir);
+ for (String intface : mapIntfacePTH.keySet()) {
+ // Get the right StructDecl
+ DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+ StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+ List<String> structTypes = structDecl.getStructTypes();
+ // Iterate over enum declarations
+ for (String stType : structTypes) {
+ // Open a new file to write into
+ FileWriter fw = new FileWriter(dir + "/" + stType + ".java");
+ pw = new PrintWriter(new BufferedWriter(fw));
+ println("public class " + stType + " {");
+ List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+ List<String> structMembers = structDecl.getMembers(stType);
+ for (int i = 0; i < structMembers.size(); i++) {
+
+ String memberType = structMemberTypes.get(i);
+ String member = structMembers.get(i);
+ println("public static " + memberType + " " + member + ";");
+ }
+ println("}\n");
+ pw.close();
+ System.out.println("IoTCompiler: Generated struct class " + stType + ".java...");
}
- println("}\n");
}
}
println("");
println("public interface " + intface + " {");
// Write enum if any...
- EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
- writeEnumJava(enumDecl);
+ //EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+ //writeEnumJava(enumDecl);
// Write struct if any...
- StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
- writeStructJava(structDecl);
+ //StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+ //writeStructJava(structDecl);
// Write methods
writeMethodJavaLocalInterface(methods, intDecl);
println("}");
}
+ /**
+ * HELPER: checkAndWriteEnumTypeJavaStub() writes the enum type (convert from enum to int)
+ */
+ private void checkAndWriteEnumTypeJavaStub(List<String> methParams, List<String> methPrmTypes) {
+
+ // Iterate and find enum declarations
+ 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)) {
+ // 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("for (int i = 0; i < len" + i + "; i++) {");
+ println("paramEnum" + i + "[i] = " + param + "[i].ordinal();");
+ println("}");
+ } else if (isList(paramType)) { // A list
+ println("int len" + i + " = " + param + ".size();");
+ println("int paramEnum" + i + "[] = new int[len];");
+ println("for (int i = 0; i < len" + i + "; i++) {");
+ println("paramEnum" + i + "[i] = " + param + ".get(i).ordinal();");
+ println("}");
+ } else { // Just one element
+ println("int paramEnum" + i + "[] = new int[1];");
+ println("paramEnum" + i + "[0] = " + param + ".ordinal();");
+ }
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: checkAndWriteEnumRetTypeJavaStub() writes the enum return type (convert from enum to int)
+ */
+ private void checkAndWriteEnumRetTypeJavaStub(String retType) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(getSimpleType(retType));
+ // Take the inner type of generic
+ if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(retType)[0];
+ if (isEnumClass(pureType)) {
+ // Check if this is enum type
+ // Enum decoder
+ println("int[] retEnum = (int[]) retObj;");
+ println(pureType + "[] enumVals = " + pureType + ".values();");
+ if (isArray(retType)) { // An array
+ println("int retLen = retEnum.length;");
+ println(pureType + "[] enumRetVal = new " + pureType + "[retLen];");
+ println("for (int i = 0; i < retLen; i++) {");
+ println("enumRetVal[i] = enumVals[retEnum[i]];");
+ println("}");
+ } else if (isList(retType)) { // A list
+ println("int retLen = retEnum.length;");
+ println("List<" + pureType + "> enumRetVal = new ArrayList<" + pureType + ">();");
+ println("for (int i = 0; i < retLen; i++) {");
+ println("enumRetVal.add(enumVals[retEnum[i]]);");
+ println("}");
+ } else { // Just one element
+ println(pureType + " enumRetVal = enumVals[retEnum[0]];");
+ }
+ println("return enumRetVal;");
+ }
+ }
+
+
/**
* HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class
*/
println("int methodId = " + intDecl.getMethodNumId(method) + ";");
String retType = intDecl.getMethodType(method);
- println("Class<?> retType = " + getSimpleType(retType) + ".class;");
+ println("Class<?> retType = " + getSimpleType(getEnumType(retType)) + ".class;");
+ checkAndWriteEnumTypeJavaStub(methParams, methPrmTypes);
// Generate array of parameter types
print("Class<?>[] paramCls = new Class<?>[] { ");
for (int i = 0; i < methParams.size(); i++) {
String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
- print(getSimpleType(paramType) + ".class");
+ print(getSimpleType(getEnumType(paramType)) + ".class");
// Check if this is the last element (don't print a comma)
if (i != methParams.size() - 1) {
print(", ");
// Generate array of parameter objects
print("Object[] paramObj = new Object[] { ");
for (int i = 0; i < methParams.size(); i++) {
- print(getSimpleIdentifier(methParams.get(i)));
+ print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
// Check if this is the last element (don't print a comma)
if (i != methParams.size() - 1) {
print(", ");
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
+ println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
+ checkAndWriteEnumRetTypeJavaStub(retType);
} else {
println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
println("return (" + retType + ")retObj;");
println("}\n");
println("int methodId = " + intDecl.getMethodNumId(method) + ";");
String retType = intDecl.getMethodType(method);
- println("Class<?> retType = " + getSimpleType(retType) + ".class;");
+ println("Class<?> retType = " + getSimpleType(getEnumType(retType)) + ".class;");
// Generate array of parameter types
print("Class<?>[] paramCls = new Class<?>[] { ");
for (int i = 0; i < methParams.size(); i++) {
for (int i = 0; i < methParams.size(); i++) {
String paramType = methPrmTypes.get(i);
if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
- if (isArray(methPrmTypes.get(i), methParams.get(i)))
+ //if (isArray(methPrmTypes.get(i), methParams.get(i)))
+ if (isArray(methParams.get(i)))
print(getSimpleIdentifier(methParams.get(i)) + ".length");
- else if (isList(methPrmTypes.get(i), methParams.get(i)))
+ else if (isList(methPrmTypes.get(i)))
print(getSimpleIdentifier(methParams.get(i)) + ".size()");
else
print("new Integer(1)");
println("try {");
String exchParamType = checkAndGetParamClass(paramType);
// Print array if this is array or list if this is a list of callback objects
- if (isArray(paramType, param)) {
+ if (isArray(param)) {
println("int numStubs" + i + " = (int) paramObj[" + i + "];");
println(exchParamType + "[] stub" + i + " = new " + exchParamType + "[numStubs" + i + "];");
- } else if (isList(paramType, param)) {
+ } else if (isList(paramType)) {
println("int numStubs" + i + " = (int) paramObj[" + i + "];");
println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();");
} else {
// Generate a loop if needed
if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
String exchParamType = checkAndGetParamClass(paramType);
- if (isArray(paramType, param)) {
+ if (isArray(param)) {
println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
println("objIdCnt++;");
println("}");
- } else if (isList(paramType, param)) {
+ } else if (isList(paramType)) {
println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt));");
println("objIdCnt++;");
}
+ /**
+ * HELPER: checkAndWriteEnumTypeJavaSkeleton() writes the enum type (convert from enum to int)
+ */
+ private void checkAndWriteEnumTypeJavaSkeleton(List<String> methParams, List<String> methPrmTypes) {
+
+ // Iterate and find enum declarations
+ 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)) {
+ // 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("for (int i = 0; i < len" + i + "; i++) {");
+ println("paramEnum[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("for (int i = 0; i < len" + i + "; i++) {");
+ println("paramEnum.add(enumVals[paramInt" + i + "[i]]);");
+ println("}");
+ } else { // Just one element
+ println(simpleType + " paramEnum" + i + " = enumVals[paramInt" + i + "[0]];");
+ }
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: checkAndWriteEnumRetTypeJavaSkeleton() writes the enum return type (convert from enum to int)
+ */
+ private void checkAndWriteEnumRetTypeJavaSkeleton(String retType, String methodId) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(getSimpleType(retType));
+ // Take the inner type of generic
+ if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(retType)[0];
+ if (isEnumClass(pureType)) {
+ // Check if this is enum type
+ // Enum decoder
+ if (isArray(retType)) { // An array
+ print(pureType + "[] retEnum = " + methodId + "(");
+ } else if (isList(retType)) { // A list
+ print("List<" + pureType + "> retEnum = " + methodId + "(");
+ } else { // Just one element
+ print(pureType + " retEnum = " + methodId + "(");
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: checkAndWriteEnumRetConvJavaSkeleton() writes the enum return type (convert from enum to int)
+ */
+ private void checkAndWriteEnumRetConvJavaSkeleton(String retType) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(getSimpleType(retType));
+ // Take the inner type of generic
+ if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(retType)[0];
+ if (isEnumClass(pureType)) {
+ // Check if this is enum type
+ if (isArray(retType)) { // An array
+ println("int retLen = retEnum.length;");
+ println("int[] retEnumVal = new int[retLen];");
+ println("for (int i = 0; i < retLen; i++) {");
+ println("retEnumVal[i] = retEnum[i].ordinal();");
+ println("}");
+ } else if (isList(retType)) { // A list
+ println("int retLen = retEnum.size();");
+ println("List<" + pureType + "> retEnumVal = new ArrayList<" + pureType + ">();");
+ println("for (int i = 0; i < retLen; i++) {");
+ println("retEnumVal.add(retEnum[i].ordinal());");
+ println("}");
+ } else { // Just one element
+ println("int[] retEnumVal = new int[1];");
+ println("retEnumVal[0] = retEnum.ordinal();");
+ }
+ println("Object retObj = retEnumVal;");
+ }
+ }
+
+
/**
* HELPER: writeStdMethodHelperBodyJavaSkeleton() writes the standard method body helper in the skeleton class
*/
print("int.class");
} else { // Generate normal classes if it's not a callback object
String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
- print(getSimpleType(prmType) + ".class");
+ print(getSimpleType(getEnumType(prmType)) + ".class");
}
if (i != methParams.size() - 1)
print(", ");
print(", ");
}
println(" });");
+ checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes);
Map<Integer,String> mapStubParam = null;
if (isCallbackMethod)
mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType);
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 { // 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
+ print(getEnumParam(methPrmTypes.get(i), methParams.get(i), i));
} else {
String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
print("(" + prmType + ") paramObj[" + i + "]");
print(", ");
}
println(");");
- if (!retType.equals("void"))
+ if (!retType.equals("void")) {
+ if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type
+ checkAndWriteEnumRetConvJavaSkeleton(retType);
println("rmiObj.sendReturnObj(retObj);");
+ }
if (isCallbackMethod) { // Catch exception if this is callback
println("} catch(Exception ex) {");
println("ex.printStackTrace();");
/**
- * HELPER: writeEnumCplus() writes the enumeration declaration
+ * HELPER: generateEnumCplus() writes the enumeration declaration
*/
- private void writeEnumCplus(EnumDecl enumDecl) {
-
- Set<String> enumTypes = enumDecl.getEnumDeclarations();
- // Iterate over enum declarations
- for (String enType : enumTypes) {
-
- println("enum " + enType + " {");
- List<String> enumMembers = enumDecl.getMembers(enType);
- for (int i = 0; i < enumMembers.size(); i++) {
+ public void generateEnumCplus() throws IOException {
- String member = enumMembers.get(i);
- print(member);
- // Check if this is the last element (don't print a comma)
- if (i != enumMembers.size() - 1)
- println(",");
- else
- println("");
+ // Create a new directory
+ createDirectory(dir);
+ for (String intface : mapIntfacePTH.keySet()) {
+ // Get the right StructDecl
+ DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+ EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+ Set<String> enumTypes = enumDecl.getEnumDeclarations();
+ // Iterate over enum declarations
+ for (String enType : enumTypes) {
+ // Open a new file to write into
+ FileWriter fw = new FileWriter(dir + "/" + enType + ".hpp");
+ pw = new PrintWriter(new BufferedWriter(fw));
+ // Write file headers
+ println("#ifndef _" + enType.toUpperCase() + "_HPP__");
+ println("#define _" + enType.toUpperCase() + "_HPP__");
+ println("enum " + enType + " {");
+ List<String> enumMembers = enumDecl.getMembers(enType);
+ for (int i = 0; i < enumMembers.size(); i++) {
+
+ String member = enumMembers.get(i);
+ print(member);
+ // Check if this is the last element (don't print a comma)
+ if (i != enumMembers.size() - 1)
+ println(",");
+ else
+ println("");
+ }
+ println("};\n");
+ println("#endif");
+ pw.close();
+ System.out.println("IoTCompiler: Generated enum " + enType + ".hpp...");
}
- println("};\n");
}
}
/**
- * HELPER: writeStructCplus() writes the struct declaration
+ * HELPER: generateStructCplus() writes the struct declaration
*/
- private void writeStructCplus(StructDecl structDecl) {
-
- List<String> structTypes = structDecl.getStructTypes();
- // Iterate over enum declarations
- for (String stType : structTypes) {
+ public void generateStructCplus() throws IOException {
- println("struct " + stType + " {");
- List<String> structMemberTypes = structDecl.getMemberTypes(stType);
- List<String> structMembers = structDecl.getMembers(stType);
- for (int i = 0; i < structMembers.size(); i++) {
-
- String memberType = structMemberTypes.get(i);
- String member = structMembers.get(i);
- String structTypeC = checkAndGetCplusType(memberType);
- String structComplete = checkAndGetCplusArray(structTypeC, member);
- println(structComplete + ";");
+ // Create a new directory
+ createDirectory(dir);
+ for (String intface : mapIntfacePTH.keySet()) {
+ // Get the right StructDecl
+ DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+ StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+ List<String> structTypes = structDecl.getStructTypes();
+ // Iterate over enum declarations
+ for (String stType : structTypes) {
+ // Open a new file to write into
+ FileWriter fw = new FileWriter(dir + "/" + stType + ".hpp");
+ pw = new PrintWriter(new BufferedWriter(fw));
+ // Write file headers
+ println("#ifndef _" + stType.toUpperCase() + "_HPP__");
+ println("#define _" + stType.toUpperCase() + "_HPP__");
+ println("struct " + stType + " {");
+ List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+ List<String> structMembers = structDecl.getMembers(stType);
+ for (int i = 0; i < structMembers.size(); i++) {
+
+ String memberType = structMemberTypes.get(i);
+ String member = structMembers.get(i);
+ String structTypeC = checkAndGetCplusType(memberType);
+ String structComplete = checkAndGetCplusArray(structTypeC, member);
+ println(structComplete + ";");
+ }
+ println("};\n");
+ println("#endif");
+ pw.close();
+ System.out.println("IoTCompiler: Generated struct " + stType + ".hpp...");
}
- println("};\n");
}
}
DeclarationHandler decHandler = mapIntDeclHand.get(intface);
InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
List<String> methods = intDecl.getMethods();
- Set<String> includeClasses = getIncludeClasses(methods, intDecl, true);
+ Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
printIncludeStatements(includeClasses); println("");
println("using namespace std;\n");
// Write enum if any...
- EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
- writeEnumCplus(enumDecl);
+ //EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+ //writeEnumCplus(enumDecl);
// Write struct if any...
- StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
- writeStructCplus(structDecl);
+ //StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+ //writeStructCplus(structDecl);
println("class " + intface); println("{");
println("public:");
// Write methods
println("#define _" + newIntface.toUpperCase() + "_HPP__");
println("#include <iostream>");
// Pass in set of methods and get import classes
- Set<String> includeClasses = getIncludeClasses(intMeth.getValue(), intDecl, false);
+ Set<String> includeClasses = getIncludeClasses(intMeth.getValue(), intDecl, intface, false);
List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
printIncludeStatements(allIncludeClasses); println("");
}
+ /**
+ * HELPER: checkAndWriteEnumTypeCplusStub() writes the enum type (convert from enum to int)
+ */
+ private void checkAndWriteEnumTypeCplusStub(List<String> methParams, List<String> methPrmTypes) {
+
+ // Iterate and find enum declarations
+ 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)) {
+ // 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("for (int i = 0; i < len" + i + "; i++) {");
+ println("paramEnum" + i + "[i] = (int) " + param + "[i];");
+ println("}");
+ } else { // Just one element
+ println("vector<int> paramEnum" + i + "(1);");
+ println("paramEnum" + i + "[0] = (int) " + param + ";");
+ }
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: checkAndWriteEnumRetTypeCplusStub() writes the enum return type (convert from enum to int)
+ */
+ private void checkAndWriteEnumRetTypeCplusStub(String retType) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(getSimpleType(retType));
+ // Take the inner type of generic
+ if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(retType)[0];
+ if (isEnumClass(pureType)) {
+ // Check if this is enum type
+ println("vector<int> retEnumInt;");
+ println("void* retObj = &retEnumInt;");
+ println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
+ if (isArrayOrList(retType, retType)) { // An array or vector
+ println("int retLen = retEnumInt.size();");
+ println("vector<" + pureType + "> retVal(retLen);");
+ println("for (int i = 0; i < retLen; i++) {");
+ println("retVal[i] = (" + pureType + ") retEnumInt[i];");
+ println("}");
+ } else { // Just one element
+ println(pureType + " retVal = (" + pureType + ") retEnumInt[0];");
+ }
+ println("return retVal;");
+ }
+ }
+
+
/**
* HELPER: writeStdMethodBodyCplusStub() writes the standard method body in the stub class
*/
println("int methodId = " + intDecl.getMethodNumId(method) + ";");
String retType = intDecl.getMethodType(method);
String retTypeC = checkAndGetCplusType(retType);
- println("string retType = \"" + checkAndGetCplusArrayType(retTypeC) + "\";");
+ println("string retType = \"" + checkAndGetCplusArrayType(getEnumType(retTypeC)) + "\";");
// Generate array of parameter types
print("string paramCls[] = { ");
for (int i = 0; i < methParams.size(); i++) {
String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
String paramType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
- print("\"" + paramType + "\"");
+ print("\"" + getEnumType(paramType) + "\"");
// Check if this is the last element (don't print a comma)
if (i != methParams.size() - 1) {
print(", ");
}
}
println(" };");
+ checkAndWriteEnumTypeCplusStub(methParams, methPrmTypes);
// Generate array of parameter objects
print("void* paramObj[] = { ");
for (int i = 0; i < methParams.size(); i++) {
- print("&" + getSimpleIdentifier(methParams.get(i)));
+ print("&" + getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
// Check if this is the last element (don't print a comma)
if (i != methParams.size() - 1) {
print(", ");
println("void* retObj = NULL;");
println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
} else { // We do have a return value
- if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
- println(checkAndGetCplusType(retType) + " retVal;");
- else
- println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";");
- println("void* retObj = &retVal;");
- println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
- println("return retVal;");
+ if (getParamCategory(retType) == ParamCategory.ENUM) {
+ checkAndWriteEnumRetTypeCplusStub(retType);
+ } else {
+ if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ println(checkAndGetCplusType(retType) + " retVal;");
+ else
+ println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";");
+ println("void* retObj = &retVal;");
+ println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
+ println("return retVal;");
+ }
}
}
}
+ /**
+ * HELPER: checkAndWriteEnumTypeCplusSkeleton() writes the enum type (convert from enum to int)
+ */
+ private void checkAndWriteEnumTypeCplusSkeleton(List<String> methParams, List<String> methPrmTypes) {
+
+ // Iterate and find enum declarations
+ 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)) {
+ // Check if this is enum type
+ if (isArrayOrList(paramType, param)) { // An array
+ println("int len" + i + " = paramEnumInt" + i + ".size();");
+ println("vector<" + simpleType + "> paramEnum" + i + "(len" + i + ");");
+ println("for (int i=0; i < len" + i + "; i++) {");
+ println("paramEnum" + i + "[i] = (" + simpleType + ") paramEnumInt" + i + "[i];");
+ println("}");
+ } else { // Just one element
+ println(simpleType + " paramEnum" + i + ";");
+ println("paramEnum" + i + " = (" + simpleType + ") paramEnumInt" + i + "[0];");
+ }
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: checkAndWriteEnumRetTypeCplusSkeleton() writes the enum return type (convert from enum to int)
+ */
+ private void checkAndWriteEnumRetTypeCplusSkeleton(String retType) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(getSimpleType(retType));
+ // Take the inner type of generic
+ if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(retType)[0];
+ if (isEnumClass(pureType)) {
+ // Check if this is enum type
+ // Enum decoder
+ if (isArrayOrList(retType, retType)) { // An array
+ println("int retLen = retEnum.size();");
+ println("vector<int> retEnumInt(retLen);");
+ println("for (int i=0; i < retLen; i++) {");
+ println("retEnumInt[i] = (int) retEnum[i];");
+ println("}");
+ } else { // Just one element
+ println("vector<int> retEnumInt(1);");
+ println("retEnumInt[0] = (int) retEnum;");
+ }
+ }
+ }
+
+
/**
* HELPER: writeStdMethodHelperBodyCplusSkeleton() writes the standard method body helper in the skeleton class
*/
} else { // Generate normal classes if it's not a callback object
String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
String prmType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
- print("\"" + prmType + "\"");
+ print("\"" + getEnumType(prmType) + "\"");
}
if (i != methParams.size() - 1) {
print(", ");
// Generate parameters
for (int i = 0; i < methParams.size(); i++) {
String paramType = returnGenericCallbackType(methPrmTypes.get(i));
- if (!callbackClasses.contains(paramType)) {
+ if (!callbackClasses.contains(paramType)) {
String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
- String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
- println(methParamComplete + ";");
+ if (isEnumClass(getSimpleType(methPrmType))) { // Check if this is enum type
+ println("vector<int> paramEnumInt" + i + ";");
+ } else {
+ String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
+ println(methParamComplete + ";");
+ }
}
}
// Generate array of parameter objects
String paramType = returnGenericCallbackType(methPrmTypes.get(i));
if (callbackClasses.contains(paramType))
print("&numStubs" + i);
+ else if (isEnumClass(getSimpleType(paramType))) // Check if this is enum type
+ print("¶mEnumInt" + i);
else
print("&" + getSimpleIdentifier(methParams.get(i)));
if (i != methParams.size() - 1) {
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")) {
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) {
}
println(");");
} else { // We do have a return value
- print(checkAndGetCplusType(retType) + " retVal = " + methodId + "(");
+ if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type
+ print(checkAndGetCplusType(retType) + " retEnum = ");
+ 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) {
}
}
println(");");
- println("void* retObj = &retVal;");
+ checkAndWriteEnumRetTypeCplusSkeleton(retType);
+ if (isEnumClass(getSimpleArrayType(getSimpleType(retType)))) // Enum type
+ println("void* retObj = &retEnumInt;");
+ else
+ println("void* retObj = &retVal;");
String retTypeC = checkAndGetCplusType(retType);
- println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusArrayType(retTypeC) + "\");");
+ println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(checkAndGetCplusArrayType(retTypeC)) + "\");");
}
}
DeclarationHandler decHandler = mapIntDeclHand.get(intface);
InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
List<String> methods = intDecl.getMethods();
- Set<String> includeClasses = getIncludeClasses(methods, intDecl, true);
+ Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
printIncludeStatements(allIncludeClasses); println("");
DeclarationHandler decHandler = mapIntDeclHand.get(intface);
InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
List<String> methods = intDecl.getMethods();
- Set<String> includeClasses = getIncludeClasses(methods, intDecl, true);
+ Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
printIncludeStatements(allIncludeClasses); println("");
// We can either use mapNonPrimitivesJava or mapNonPrimitivesCplus here
} else if (mapNonPrimitivesJava.containsKey(getSimpleType(paramType))) {
return ParamCategory.NONPRIMITIVES;
+ } else if (isEnumClass(paramType)) {
+ return ParamCategory.ENUM;
+ } else if (isStructClass(paramType)) {
+ return ParamCategory.STRUCT;
} else
return ParamCategory.USERDEFINED;
}
}
+ // Handle and return the correct enum declaration
+ // In Java, if we declare enum in Camera interface, then it becomes "Camera.<enum>"
+ private String getEnumParamDecl(String type, InterfaceDecl intDecl) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(type);
+ // Take the inner type of generic
+ if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(type)[0];
+ if (isEnumClass(pureType)) {
+ String enumType = intDecl.getInterface() + "." + type;
+ return enumType;
+ } else
+ return type;
+ }
+
+
+ // Handle and return the correct type
+ private String getEnumParam(String type, String param, int i) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(type);
+ // Take the inner type of generic
+ if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(type)[0];
+ if (isEnumClass(pureType)) {
+ String enumParam = "paramEnum" + i;
+ return enumParam;
+ } else
+ return param;
+ }
+
+
+ // Handle and return the correct enum declaration translate into int[]
+ private String getEnumType(String type) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(type);
+ // Take the inner type of generic
+ if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+ pureType = getTypeOfGeneric(type)[0];
+ if (isEnumClass(pureType)) {
+ String enumType = "int[]";
+ return enumType;
+ } else
+ return type;
+ }
+
+
+ // Handle and return the correct struct declaration
+ private String getStructType(String type) {
+
+ if (isStructClass(type)) {
+ // TODO: complete this method
+ return type;
+ } else
+ return type;
+ }
+
+
+ // Check if this an enum declaration
+ private boolean isEnumClass(String type) {
+
+ // Just iterate over the set of interfaces
+ for (String intface : mapIntfacePTH.keySet()) {
+ DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+ EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+ Set<String> setEnumDecl = enumDecl.getEnumDeclarations();
+ if (setEnumDecl.contains(type))
+ return true;
+ }
+ return false;
+ }
+
+
+ // Check if this an struct declaration
+ private boolean isStructClass(String type) {
+
+ // Just iterate over the set of interfaces
+ for (String intface : mapIntfacePTH.keySet()) {
+ DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+ StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+ List<String> listStructDecl = structDecl.getStructTypes();
+ if (listStructDecl.contains(type))
+ return true;
+ }
+ return false;
+ }
+
+
// Generate a set of classes for include statements
- private Set<String> getIncludeClasses(Collection<String> methods, InterfaceDecl intDecl, boolean needExchange) {
+ private Set<String> getIncludeClasses(Collection<String> methods, InterfaceDecl intDecl, String intface, boolean needExchange) {
Set<String> includeClasses = new HashSet<String>();
for (String method : methods) {
includeClasses.add("\"" + simpleType + ".hpp\"");
includeClasses.add("\"" + simpleType + "_CallbackSkeleton.hpp\"");
}
+ } else if (getParamCategory(getSimpleArrayType(simpleType)) == ParamCategory.ENUM) {
+ includeClasses.add("\"" + simpleType + ".hpp\"");
} else if (param.contains("[]")) {
// Check if this is array for C++; translate into vector
includeClasses.add("<vector>");
}
+ // This helper function strips off array declaration, e.g. int[] becomes int
+ private String getSimpleArrayType(String type) {
+
+ // Handle [ for array declaration
+ String substr = type;
+ if (type.contains("[]")) {
+ substr = type.split("\\[\\]")[0];
+ }
+ return substr;
+ }
+
+
// This helper function strips off array declaration, e.g. D[] becomes D
private String getSimpleIdentifier(String ident) {
private boolean isArrayOrList(String paramType, String param) {
// Check for array declaration
- if (isArray(paramType, param))
+ if (isArray(param))
return true;
- else if (isList(paramType, param))
+ else if (isList(paramType))
return true;
else
return false;
}
- // Is array or list?
- private boolean isArray(String paramType, String param) {
+ // Is array? For return type we put the return type as input parameter
+ private boolean isArray(String param) {
// Check for array declaration
if (param.contains("[]"))
}
- // Is array or list?
- private boolean isList(String paramType, String param) {
+ // Is list?
+ private boolean isList(String paramType) {
// Check for array declaration
if (paramType.contains("List"))
// Generate everything if we don't see "-java" or "-cplus"
if (i == args.length) {
+ comp.generateEnumJava();
+ comp.generateStructJava();
comp.generateJavaLocalInterfaces();
comp.generateJavaInterfaces();
comp.generateJavaStubClasses();
comp.generateJavaCallbackStubClasses();
comp.generateJavaSkeletonClass();
comp.generateJavaCallbackSkeletonClass();
+ comp.generateEnumCplus();
+ comp.generateStructCplus();
comp.generateCplusLocalInterfaces();
comp.generateCPlusInterfaces();
comp.generateCPlusStubClasses();
throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i]);
if (args[i].equals("-java")) {
+ comp.generateEnumJava();
+ comp.generateStructJava();
comp.generateJavaLocalInterfaces();
comp.generateJavaInterfaces();
comp.generateJavaStubClasses();
comp.generateJavaSkeletonClass();
comp.generateJavaCallbackSkeletonClass();
} else {
+ comp.generateEnumCplus();
+ comp.generateStructCplus();
comp.generateCplusLocalInterfaces();
comp.generateCPlusInterfaces();
comp.generateCPlusStubClasses();