private String dir;
private String subdir;
+
/**
* Class constants
*/
USERDEFINED // Assumed as driver classes
}
+
/**
* Class constructors
*/
* data structures.
* Additionally, the data structure handles are
* returned from tree-parsing for further process.
- *
*/
public void setDataStructures(String origInt, ParseNode pnPol, ParseNode pnReq) {
/**
- * HELPER: writeMethodJavaLocalInterface() writes the method of the interface
+ * HELPER: writeMethodJavaLocalInterface() writes the method of the local interface
*/
private void writeMethodJavaLocalInterface(Collection<String> methods, InterfaceDecl intDecl) {
InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
List<String> methods = intDecl.getMethods();
Set<String> importClasses = getImportClasses(methods, intDecl);
- printImportStatements(importClasses);
+ List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
+ List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+ printImportStatements(allImportClasses);
// Write interface header
println("");
println("public interface " + intface + " {");
- // Write enum if any...
- //EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
- //writeEnumJava(enumDecl);
- // Write struct if any...
- //StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
- //writeStructJava(structDecl);
// Write methods
writeMethodJavaLocalInterface(methods, intDecl);
println("}");
DeclarationHandler decHandler = mapIntDeclHand.get(intface);
InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
// Pass in set of methods and get import classes
- Set<String> importClasses = getImportClasses(intMeth.getValue(), intDecl);
- printImportStatements(importClasses);
+ List<String> methods = intDecl.getMethods();
+ Set<String> importClasses = getImportClasses(methods, intDecl);
+ List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
+ List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+ printImportStatements(allImportClasses);
// Write interface header
println("");
println("public interface " + newIntface + " {\n");
// Write methods
- writeMethodJavaInterface(intMeth.getValue(), intDecl);
+ writeMethodJavaInterface(methods, intDecl);
println("}");
pw.close();
System.out.println("IoTCompiler: Generated interface " + newIntface + ".java...");
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ int newObjectId = getNewIntfaceObjectId(newIntface);
println("private final static int object" + newObjectId + "Id = " +
newObjectId + ";\t//" + newIntface);
Set<String> methodIds = intMeth.getValue();
i++;
}
println(" };");
- println("private List<Integer> set" + newObjectId + "Allowed;");
+ println("private static List<Integer> set" + newObjectId + "Allowed;");
}
}
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
- println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);");
+ int newObjectId = getNewIntfaceObjectId(newIntface);
+ println("set" + newObjectId + "Allowed = new ArrayList<Integer>(Arrays.asList(object" + newObjectId +"Permission));");
}
}
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ int newObjectId = getNewIntfaceObjectId(newIntface);
println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);");
println("}");
/**
- * HELPER: writeConstructorJavaStub() writes the constructor of the stub class
+ * HELPER: writeJavaInitCallbackPermission() writes the permission for callback
+ */
+ private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) {
+
+ 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 + ");");
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
*/
private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl) {
println(intface + "_CallbackSkeleton skel = (" + intface + "_CallbackSkeleton) listCallbackObj.get(objId);");
println("if (skel != null) {");
println("skel.invokeMethod(rmiObj);");
- println("} else {");
+ print("}");
+ println(" else {");
println("throw new Error(\"" + intface + ": Object with Id \" + objId + \" not found!\");");
println("}");
println("}");
- println("} catch (Exception ex) {");
+ print("}");
+ println(" catch (Exception ex) {");
println("ex.printStackTrace();");
println("throw new Error(\"Error instantiating class " + intface + "_CallbackSkeleton!\");");
println("}");
println("thread.start();\n");
// Generate info sending part
String method = "___initCallBack()";
- println("int methodId = " + intDecl.getHelperMethodNumId(method) + ";");
+ int methodNumId = intDecl.getHelperMethodNumId(method);
+ println("int methodId = " + methodNumId + ";");
println("Class<?> retType = void.class;");
println("Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };");
println("Object[] paramObj = new Object[] { ports[0], address, 0 };");
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
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 (isStructClass(simpleType)) {
// Check if this is enum type
int methodNumId = intDecl.getMethodNumId(method);
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 (isStructClass(simpleType))
return true;
}
/**
- * HELPER: writeLengthStructParamClassJavaStub() writes lengths of params
+ * HELPER: writeLengthStructParamClassJavaStub() writes lengths of parameters
*/
private void writeLengthStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes) {
if (isStructClass(simpleType)) {
int members = getNumOfMembers(simpleType);
if (isArray(param)) { // An array
- String structLen = param + ".length";
+ String structLen = getSimpleArrayType(param) + ".length";
print(members + "*" + structLen);
} else if (isList(paramType)) { // A list
- String structLen = param + ".size()";
+ String structLen = getSimpleArrayType(param) + ".size()";
print(members + "*" + structLen);
} else
print(Integer.toString(members));
List<String> memTypes = structDecl.getMemberTypes(simpleType);
List<String> members = structDecl.getMembers(simpleType);
if (isArray(param)) { // An array
- println("for(int i = 0; i < " + param + ".length; i++) {");
+ println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".length; 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;");
+ print("paramObj[pos++] = " + getSimpleIdentifier(param) + "[i].");
+ print(getSimpleIdentifier(members.get(i)));
+ println(";");
+ }
+ println("}");
} else if (isList(paramType)) { // A list
- println("for(int i = 0; i < " + param + ".size(); i++) {");
- }
- if (isArrayOrList(param, paramType)) { // An array or list
+ println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); 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;");
- print("paramObj[pos++] = " + param + "[i].");
+ print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".get(i).");
print(getSimpleIdentifier(members.get(i)));
println(";");
}
for (int i = 0; i < members.size(); i++) {
String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
- print("paramObj[pos++] = " + param + ".");
+ print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".");
print(getSimpleIdentifier(members.get(i)));
println(";");
}
/**
- * HELPER: writeStructRetMembersJavaStub() writes parameters of struct
+ * HELPER: writeStructRetMembersJavaStub() writes parameters of struct for return statement
*/
private void writeStructRetMembersJavaStub(String simpleType, String retType) {
/**
- * HELPER: writeStructReturnJavaStub() writes parameters if struct is present
+ * HELPER: writeStructReturnJavaStub() writes parameters if struct is present for return statement
*/
private void writeStructReturnJavaStub(String simpleType, String retType) {
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;");
println("try {");
// Check if this is single object, array, or list of objects
for (int i = 0; i < methParams.size(); i++) {
-
String paramType = methPrmTypes.get(i);
if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
String param = methParams.get(i);
println("}");
}
}
- println("} catch (Exception ex) {");
+ print("}");
+ println(" catch (Exception ex) {");
println("ex.printStackTrace();");
println("throw new Error(\"Exception when generating skeleton objects!\");");
println("}\n");
/**
- * HELPER: writeMethodJavaStub() writes the method of the stub class
+ * HELPER: writeMethodJavaStub() writes the methods of the stub class
*/
private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
}
+ /**
+ * HELPER: writeStructPermissionJavaSkeleton() writes permission for struct helper
+ */
+ private void writeStructPermissionJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, String intface) {
+
+ // Use this set to handle two same methodIds
+ for (String method : methods) {
+ List<String> methParams = intDecl.getMethodParams(method);
+ List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+ // Check for params with structs
+ 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 helperMethod = methodNumId + "struct" + i;
+ int methodHelperNumId = intDecl.getHelperMethodNumId(helperMethod);
+ // Iterate over interfaces to give permissions to
+ 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(" + methodHelperNumId + ");");
+ }
+ }
+ }
+ }
+ }
+
+
/**
* HELPER: writeConstructorJavaSkeleton() writes the constructor of the skeleton class
*/
- private void writeConstructorJavaSkeleton(String newSkelClass, String intface) {
+ 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, callbackExist);
+ writeStructPermissionJavaSkeleton(methods, intDecl, intface);
println("___waitRequestInvokeMethod();");
println("}\n");
}
/**
* HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part
*/
- private Map<Integer,String> writeCallbackJavaStubGeneration(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+ private Map<Integer,String> writeCallbackJavaStubGeneration(List<String> methParams, List<String> methPrmTypes,
+ String callbackType) {
Map<Integer,String> mapStubParam = new HashMap<Integer,String>();
// Iterate over callback objects
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];");
/**
- * HELPER: writeStructMembersJavaSkeleton() writes parameters of struct
+ * HELPER: writeStructMembersJavaSkeleton() writes member parameters of struct
*/
private void writeStructMembersJavaSkeleton(String simpleType, String paramType,
String param, String method, InterfaceDecl intDecl, int iVar) {
StructDecl structDecl = getStructDecl(simpleType);
List<String> memTypes = structDecl.getMemberTypes(simpleType);
List<String> members = structDecl.getMembers(simpleType);
- if (isArrayOrList(param, paramType)) { // An array or list
+ println("int pos = 0;");
+ if (isArrayOrList(paramType, param)) { // 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++) {");
+ if (isArrayOrList(paramType, param)) { // An array or list
for (int i = 0; i < members.size(); i++) {
String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
}
-
/**
- * HELPER: writeStructMembersInitJavaSkeleton() writes parameters of struct
+ * HELPER: writeStructMembersInitJavaSkeleton() writes member parameters initialization of struct
*/
private void writeStructMembersInitJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
List<String> methPrmTypes, String method) {
StructDecl structDecl = getStructDecl(simpleType);
List<String> members = structDecl.getMembers(simpleType);
List<String> memTypes = structDecl.getMemberTypes(simpleType);
- if (isArrayOrList(param, paramType)) { // An array or list
+ if (isArrayOrList(paramType, param)) { // An array or list
println("for(int i = 0; i < " + counter + "; i++) {");
}
if (isArray(param)) { // An array
/**
- * HELPER: writeStructReturnJavaSkeleton() writes parameters if struct is present
+ * HELPER: writeStructReturnJavaSkeleton() writes struct for return statement
*/
private void writeStructReturnJavaSkeleton(String simpleType, String retType) {
StructDecl structDecl = getStructDecl(simpleType);
List<String> memTypes = structDecl.getMemberTypes(simpleType);
List<String> members = structDecl.getMembers(simpleType);
- if (isArrayOrList(retType, retType)) { // An array or list
+ if (isArray(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(";");
}
println("}");
+ } else if (isList(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.get(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));
}
+ /**
+ * HELPER: writeMethodHelperReturnJavaSkeleton() writes return statement part in skeleton
+ */
+ private void writeMethodHelperReturnJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
+ List<String> methPrmTypes, String method, boolean isCallbackMethod, String callbackType,
+ boolean isStructMethod) {
+
+ 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(getGenericType(retType)))) { // Enum type
+ checkAndWriteEnumRetTypeJavaSkeleton(retType, intDecl.getMethodId(method));
+ } 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) + "(");
+ }
+ for (int i = 0; i < methParams.size(); i++) {
+
+ if (isCallbackMethod) {
+ print(mapStubParam.get(i)); // Get the callback parameter
+ } else if (isEnumClass(getGenericType(methPrmTypes.get(i)))) { // Enum class
+ print(getEnumParam(methPrmTypes.get(i), methParams.get(i), i));
+ } else if (isStructClass(getGenericType(methPrmTypes.get(i)))) {
+ print("paramStruct" + i);
+ } else {
+ String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+ if (isStructMethod)
+ print("(" + prmType + ") paramObj[offset" + i + "]");
+ else
+ print("(" + prmType + ") paramObj[" + i + "]");
+ }
+ if (i != methParams.size() - 1)
+ print(", ");
+ }
+ println(");");
+ if (!retType.equals("void")) {
+ if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type
+ checkAndWriteEnumRetConvJavaSkeleton(retType);
+ println("rmiObj.sendReturnObj(retObj);");
+ } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type
+ writeStructReturnJavaSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
+ println("rmiObj.sendReturnObj(retCls, retObj);");
+ } else
+ println("rmiObj.sendReturnObj(retObj);");
+ }
+ if (isCallbackMethod) { // Catch exception if this is callback
+ print("}");
+ println(" catch(Exception ex) {");
+ println("ex.printStackTrace();");
+ println("throw new Error(\"Exception from callback object instantiation!\");");
+ println("}");
+ }
+ }
+
+
/**
* HELPER: writeMethodHelperStructJavaSkeleton() writes the struct in skeleton
*/
}
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("}");
- }
+ // Write the return value part
+ writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, true);
}
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)
print(", ");
}
println(" });");
- 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 {
- 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, false);
}
for (int i = 0; i < methParams.size(); i++) { // Print size variables
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isStructClass(simpleType)) {
if (!begin) { // Generate comma for not the beginning variable
print(", "); begin = false;
/**
- * HELPER: writeMethodHelperStructSetupJavaSkeleton() writes the method helper of the struct in skeleton class
+ * HELPER: writeMethodHelperStructSetupJavaSkeleton() writes the method helper of struct setup in skeleton class
*/
private void writeMethodHelperStructSetupJavaSkeleton(Collection<String> methods,
InterfaceDecl intDecl) {
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
print("public int ___");
/**
- * HELPER: writeMethodHelperStructSetupJavaCallbackSkeleton() writes the method helper of the struct in skeleton class
+ * HELPER: writeMethodHelperStructSetupJavaCallbackSkeleton() writes the method helper of struct setup in callback skeleton class
*/
private void writeMethodHelperStructSetupJavaCallbackSkeleton(Collection<String> methods,
InterfaceDecl intDecl) {
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
print("public int ___");
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
println("int struct" + methodNumId + "Size" + i + " = 0;");
/**
- * HELPER: writeInputCountVarStructSkeleton() writes counter variable of struct for skeleton
+ * HELPER: writeInputCountVarStructSkeleton() writes input counter variable of struct for skeleton
*/
private boolean writeInputCountVarStructSkeleton(String method, InterfaceDecl intDecl) {
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);
boolean begin = true;
if (isStructClass(simpleType)) {
structExist = true;
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
print("case ");
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
print("case ");
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ int newObjectId = getNewIntfaceObjectId(newIntface);
println("if (_objectId == object" + newObjectId + "Id) {");
println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
println("throw new Error(\"Object with object Id: \" + _objectId + \" is not allowed to access method: \" + methodId);");
println("}");
+ println("}");
println("else {");
println("throw new Error(\"Object Id: \" + _objectId + \" not recognized!\");");
println("}");
- println("}");
}
}
// Write properties
writePropertiesJavaSkeleton(intface, callbackExist, intDecl);
// Write constructor
- writeConstructorJavaSkeleton(newSkelClass, intface);
+ writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
// Write methods
writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false);
// Write method helper
/**
* HELPER: writeConstructorJavaCallbackSkeleton() writes the constructor of the skeleton class
*/
- private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface) {
+ private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
println("public " + newSkelClass + "(" + intface + " _mainObj, int _objectId) throws Exception {");
println("mainObj = _mainObj;");
for (int i = 0; i < methParams.size(); i++) { // Print size variables
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isStructClass(simpleType)) {
if (!begin) { // Generate comma for not the beginning variable
print(", "); begin = false;
/**
- * HELPER: writeJavaCallbackWaitRequestInvokeMethod() writes the main loop of the skeleton class
+ * HELPER: writeJavaCallbackWaitRequestInvokeMethod() writes the request invoke method of the callback skeleton class
*/
private void writeJavaCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
// Write properties
writePropertiesJavaCallbackSkeleton(intface, callbackExist);
// Write constructor
- writeConstructorJavaCallbackSkeleton(newSkelClass, intface);
+ writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods);
// Write methods
writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true);
// Write method helper
/**
- * HELPER: writeMethodCplusLocalInterface() writes the method of the interface
+ * HELPER: writeMethodCplusLocalInterface() writes the method of the local interface
*/
private void writeMethodCplusLocalInterface(Collection<String> methods, InterfaceDecl intDecl) {
// Write file headers
println("#ifndef _" + stType.toUpperCase() + "_HPP__");
println("#define _" + stType.toUpperCase() + "_HPP__");
+ println("using namespace std;");
println("struct " + stType + " {");
List<String> structMemberTypes = structDecl.getMemberTypes(stType);
List<String> structMembers = structDecl.getMembers(stType);
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);
- // Write struct if any...
- //StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
//writeStructCplus(structDecl);
println("class " + intface); println("{");
println("public:");
/**
- * HELPER: writeMethodCplusStub() writes the method of the stub
+ * HELPER: writeMethodCplusStub() writes the methods of the stub
*/
private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
println("int numParam = " + methParams.size() + ";");
println("int methodId = " + intDecl.getMethodNumId(method) + ";");
String retType = intDecl.getMethodType(method);
- String retTypeC = checkAndGetCplusType(retType);
- println("string retType = \"" + checkAndGetCplusArrayType(retTypeC) + "\";");
+ //String retTypeC = checkAndGetCplusType(retType);
+ //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";");
+ println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
// Generate array of parameter types
print("string paramCls[] = { ");
for (int i = 0; i < methParams.size(); i++) {
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 = checkAndGetCplusType(methPrmTypes.get(i));
- String prmType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
- print("\"" + prmType + "\"");
+ String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+ 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;");
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 (isStructClass(simpleType)) {
// Check if this is enum type
println("int numParam" + i + " = 1;");
println("string retTypeStruct" + i + " = \"void\";");
println("string paramClsStruct" + i + "[] = { \"int\" };");
print("int structLen" + i + " = ");
- if (isArrayOrList(param, paramType)) { // An array
+ if (isArrayOrList(paramType, param)) { // An array
println(getSimpleArrayType(param) + ".size();");
} else { // Just one element
println("1;");
String simpleType = getGenericType(paramType);
if (isStructClass(simpleType)) {
int members = getNumOfMembers(simpleType);
- if (isArrayOrList(param, paramType)) { // An array
- String structLen = param + ".size()";
+ if (isArrayOrList(paramType, param)) { // An array or list
+ String structLen = getSimpleIdentifier(param) + ".size()";
print(members + "*" + structLen);
} else
print(Integer.toString(members));
/**
- * HELPER: writeStructMembersCplusStub() writes parameters of struct
+ * HELPER: writeStructMembersCplusStub() writes member parameters of struct
*/
private void writeStructMembersCplusStub(String simpleType, String paramType, String param) {
StructDecl structDecl = getStructDecl(simpleType);
List<String> memTypes = structDecl.getMemberTypes(simpleType);
List<String> members = structDecl.getMembers(simpleType);
- if (isArrayOrList(param, paramType)) { // An array or list
- println("for(int i = 0; i < " + param + ".size(); i++) {");
+ if (isArrayOrList(paramType, param)) { // An array or list
+ println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); i++) {");
}
- if (isArrayOrList(param, paramType)) { // An array or list
+ if (isArrayOrList(paramType, param)) { // An array or list
for (int i = 0; i < members.size(); i++) {
- String prmTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
- println("paramCls[pos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
- print("paramObj[pos++] = &" + param + "[i].");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("paramCls[pos] = \"" + prmTypeC + "\";");
+ print("paramObj[pos++] = &" + getSimpleIdentifier(param) + "[i].");
print(getSimpleIdentifier(members.get(i)));
println(";");
}
println("}");
} else { // Just one struct element
for (int i = 0; i < members.size(); i++) {
- String prmTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
- println("paramCls[pos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("paramCls[pos] = \"" + prmTypeC + "\";");
print("paramObj[pos++] = &" + param + ".");
print(getSimpleIdentifier(members.get(i)));
println(";");
/**
- * HELPER: writeStructParamClassCplusStub() writes parameters if struct is present
+ * HELPER: writeStructParamClassCplusStub() writes member parameters of struct
*/
private void writeStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes) {
if (isStructClass(simpleType)) {
writeStructMembersCplusStub(simpleType, paramType, param);
} else {
- String prmTypeC = checkAndGetCplusType(methPrmTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, methParams.get(i));
- println("paramCls[pos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+ println("paramCls[pos] = \"" + prmTypeC + "\";");
print("paramObj[pos++] = &");
print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
println(";");
/**
- * HELPER: writeStructRetMembersCplusStub() writes parameters of struct
+ * HELPER: writeStructRetMembersCplusStub() writes member parameters of struct for return statement
*/
private void writeStructRetMembersCplusStub(String simpleType, String retType) {
/**
- * HELPER: writeStructReturnCplusStub() writes parameters if struct is present
+ * HELPER: writeStructReturnCplusStub() writes member parameters of struct for return statement
*/
private void writeStructReturnCplusStub(String simpleType, String retType) {
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 prmTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
- println("retCls[retPos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("retCls[retPos] = \"" + prmTypeC + "\";");
println("retObj[retPos++] = &retParam" + i + "[i];");
}
println("}");
} else { // Just one struct element
for (int i = 0; i < members.size(); i++) {
- String prmTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
- println("retCls[retPos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("retCls[retPos] = \"" + prmTypeC + "\";");
println("retObj[retPos++] = &retParam" + i + ";");
}
}
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)) {
writeStructParamClassCplusStub(methParams, methPrmTypes);
println("int numParam = " + methParams.size() + ";");
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("\"" + getEnumType(paramType) + "\"");
+ String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+ 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)
+ //if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+ if (isArrayOrList(retType,retType))
println(checkAndGetCplusType(retType) + " retVal;");
- else
+ else {
println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";");
+ }
println("void* retObj = &retVal;");
println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
println("return retVal;");
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
- println("const static int object" + newObjectId + "Id = " + newObjectId + ";");
- println("const static set<int> set" + newObjectId + "Allowed;");
+ int newObjectId = getNewIntfaceObjectId(newIntface);
+ println("const static int object" + newObjectId + "Id = " + newObjectId + ";\t//" + newIntface);
+ println("static set<int> set" + newObjectId + "Allowed;");
}
}
println("ports = _ports;");
println("rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);");
if (callbackExist) {
- println("objIdCnt = 0;");
Iterator it = callbackClasses.iterator();
String callbackType = (String) it.next();
println("thread th1 (&" + newStubClass + "::___initCallBack, this);");
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ int newObjectId = getNewIntfaceObjectId(newIntface);
println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
println("cerr << \"Callback object for " + intface + " is not allowed to access method: \" << methodId;");
println("exit(-1);");
println(intface + "_CallbackSkeleton* skel = dynamic_cast<" + intface +
"_CallbackSkeleton*> (vecCallbackObj.at(objId));");
println("skel->invokeMethod(rmiObj);");
- println("} else {");
+ print("}");
+ println(" else {");
println("cerr << \"Illegal object Id: \" << to_string(objId);");
// TODO: perhaps need to change this into "throw" to make it cleaner (allow stack unfolding)
println("exit(-1);");
/**
- * HELPER: writeInitCallbackSendInfoCplusStub() writes the initialization of callback
+ * HELPER: writeCplusInitCallbackPermission() writes the permission for callback
+ */
+ private void writeCplusInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) {
+
+ 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.insert(" + methodNumId + ");");
+ }
+ }
+ }
+
+
+ /**
+ * HELPER: writeInitCallbackSendInfoCplusStub() writes the initialization (send info part) of callback
*/
private void writeInitCallbackSendInfoCplusStub(InterfaceDecl intDecl) {
println("void ___regCB() {");
println("int numParam = 3;");
String method = "___initCallBack()";
- println("int methodId = " + intDecl.getHelperMethodNumId(method) + ";");
+ int methodNumId = intDecl.getHelperMethodNumId(method);
+ println("int methodId = " + methodNumId + ";");
+ //writeCplusCallbackPermission(intface, methodNumId);
println("string retType = \"void\";");
println("string paramCls[] = { \"int\", \"string\", \"int\" };");
println("int rev = 0;");
print("}"); println(";");
if (callbackExist)
writePermissionInitializationCplus(intface, newStubClass, intDecl);
+ writeObjectIdCountInitializationCplus(newStubClass, callbackExist);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".hpp...");
println("IoTRMICall *rmiCall;");
// Get the object Id
- println("static int objectId;");
+ println("int objectId;");
if (callbackExist) {
// We assume that each class only has one callback interface for now
Iterator it = callbackClasses.iterator();
println("};");
if (callbackExist)
writePermissionInitializationCplus(intface, newStubClass, intDecl);
+ writeObjectIdCountInitializationCplus(newStubClass, callbackExist);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated callback stub class " + newIntface + ".hpp...");
}
+ /**
+ * HELPER: writeObjectIdCountInitializationCplus() writes the initialization of objIdCnt variable
+ */
+ private void writeObjectIdCountInitializationCplus(String newSkelClass, boolean callbackExist) {
+
+ if (callbackExist)
+ println("int " + newSkelClass + "::objIdCnt = 0;");
+ }
+
+
/**
* HELPER: writePermissionInitializationCplus() writes the initialization of permission set
*/
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
- print("const set<int> " + newSkelClass + "::set" + newObjectId + "Allowed {");
+ int newObjectId = getNewIntfaceObjectId(newIntface);
+ print("set<int> " + newSkelClass + "::set" + newObjectId + "Allowed {");
Set<String> methodIds = intMeth.getValue();
int i = 0;
for (String methodId : methodIds) {
}
+ /**
+ * HELPER: writeStructPermissionCplusSkeleton() writes permission for struct helper
+ */
+ private void writeStructPermissionCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, String intface) {
+
+ // Use this set to handle two same methodIds
+ for (String method : methods) {
+ List<String> methParams = intDecl.getMethodParams(method);
+ List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+ // Check for params with structs
+ 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 helperMethod = methodNumId + "struct" + i;
+ int helperMethodNumId = intDecl.getHelperMethodNumId(helperMethod);
+ // Iterate over interfaces to give permissions to
+ 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.insert(" + helperMethodNumId + ");");
+ }
+ }
+ }
+ }
+ }
+
+
/**
* HELPER: writeConstructorCplusSkeleton() writes the constructor of the skeleton class
*/
- private void writeConstructorCplusSkeleton(String newSkelClass, String intface, boolean callbackExist) {
+ private void writeConstructorCplusSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
println(newSkelClass + "(" + intface + " *_mainObj, int _port) {");
println("bool _bResult = false;");
println("mainObj = _mainObj;");
println("rmiObj = new IoTRMIObject(_port, &_bResult);");
- // Callback
- if (callbackExist) {
- println("objIdCnt = 0;");
- }
- //println("set0Allowed = Arrays.asList(object0Permission);");
+ writeCplusInitCallbackPermission(intface, intDecl, callbackExist);
+ writeStructPermissionCplusSkeleton(methods, intDecl, intface);
println("___waitRequestInvokeMethod();");
println("}\n");
}
println("int param1 = 0;");
println("string param2 = \"\";");
println("int param3 = 0;");
+ println("string paramCls[] = { \"int\", \"string\", \"int\" };");
println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };");
+ println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
println("bool bResult = false;");
println("rmiCall = new IoTRMICall(param1, param2.c_str(), param3, &bResult);");
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
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];
+ pureType = getGenericType(retType);
if (isEnumClass(pureType)) {
// Check if this is enum type
// Enum decoder
}
+ /**
+ * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return statement part in skeleton
+ */
+ private void writeMethodInputParameters(List<String> methParams, List<String> methPrmTypes,
+ Set<String> 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(getGenericType(paramType))) // Check if this is enum type
+ print("paramEnum" + i);
+ else if (isStructClass(getGenericType(paramType))) // Struct type
+ print("paramStruct" + i);
+ else
+ print(getSimpleIdentifier(methParams.get(i)));
+ if (i != methParams.size() - 1) {
+ print(", ");
+ }
+ }
+ println(");");
+ }
+
+
+ /**
+ * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return statement part in skeleton
+ */
+ private void writeMethodHelperReturnCplusSkeleton(InterfaceDecl intDecl, List<String> methParams,
+ List<String> methPrmTypes, String method, boolean isCallbackMethod, String callbackType,
+ String methodId, Set<String> 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(getGenericType(retType)))) // Enum type
+ print(checkAndGetCplusType(retType) + " retEnum = ");
+ 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(getGenericType(retType)))) // Struct type
+ writeStructReturnCplusSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
+ if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) // Enum type
+ println("void* retObj = &retEnumInt;");
+ else
+ if (!isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+ println("void* retObj = &retVal;");
+ String retTypeC = checkAndGetCplusType(retType);
+ if (isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+ println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);");
+ else
+ println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusRetClsType(getEnumType(retType)) + "\");");
+ }
+ }
+
+
/**
* HELPER: writeStdMethodHelperBodyCplusSkeleton() writes the standard method body helper in the skeleton class
*/
callbackType = paramType;
print("\"int\"");
} 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("\"" + getEnumType(prmType) + "\"");
+ String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+ 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 + ";");
+ 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)));
}
}
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);
}
/**
- * HELPER: writeStructMembersCplusSkeleton() writes parameters of struct
+ * HELPER: writeStructMembersCplusSkeleton() writes member parameters of struct
*/
private void writeStructMembersCplusSkeleton(String simpleType, String paramType,
String param, String method, InterfaceDecl intDecl, int iVar) {
List<String> members = structDecl.getMembers(simpleType);
int methodNumId = intDecl.getMethodNumId(method);
String counter = "struct" + methodNumId + "Size" + iVar;
- if (isArrayOrList(param, paramType)) { // An array or list
- println("for(int i = 0; i < " + counter + "; i++) {");
- }
+ println("int pos = 0;");
// Set up variables
- if (isArrayOrList(param, paramType)) { // An array or list
+ if (isArrayOrList(paramType, param)) { // An array or list
for (int i = 0; i < members.size(); i++) {
String prmTypeC = checkAndGetCplusType(memTypes.get(i));
String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
println(getSimpleType(getEnumType(prmType)) + " param" + i + ";");
}
}
- println("int pos = 0;");
- if (isArrayOrList(param, paramType)) { // An array or list
- println("for(int i = 0; i < retLen; i++) {");
+ if (isArrayOrList(paramType, param)) { // An array or list
+ println("for(int i = 0; i < " + counter + "; i++) {");
+ }
+ if (isArrayOrList(paramType, param)) { // An array or list
for (int i = 0; i < members.size(); i++) {
- String prmTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
- println("paramCls[pos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("paramCls[pos] = \"" + prmTypeC + "\";");
println("paramObj[pos++] = ¶m" + i + "[i];");
}
println("}");
} else { // Just one struct element
for (int i = 0; i < members.size(); i++) {
- String prmTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
- println("paramCls[pos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("paramCls[pos] = \"" + prmTypeC + "\";");
println("paramObj[pos++] = ¶m" + i + ";");
}
}
/**
- * HELPER: writeStructMembersInitCplusSkeleton() writes parameters of struct
+ * HELPER: writeStructMembersInitCplusSkeleton() writes member parameters initialization of struct
*/
private void writeStructMembersInitCplusSkeleton(InterfaceDecl intDecl, List<String> methParams,
List<String> methPrmTypes, String method) {
int methodNumId = intDecl.getMethodNumId(method);
String counter = "struct" + methodNumId + "Size" + i;
// Declaration
- if (isArrayOrList(param, paramType)) { // An array or list
- println("vector<" + simpleType + "> paramStruct" + i + ";");
+ if (isArrayOrList(paramType, param)) { // An array or list
+ println("vector<" + simpleType + "> paramStruct" + i + "(" + counter + ");");
} else
println(simpleType + " paramStruct" + i + ";");
// 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
+ if (isArrayOrList(paramType, param)) { // An array or list
println("for(int i = 0; i < " + counter + "; i++) {");
for (int j = 0; j < members.size(); j++) {
print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j)));
/**
- * HELPER: writeStructReturnCplusSkeleton() writes parameters if struct is present
+ * HELPER: writeStructReturnCplusSkeleton() writes parameters of struct for return statement
*/
private void writeStructReturnCplusSkeleton(String simpleType, String retType) {
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 paramTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(paramTypeC, members.get(i));
- println("retCls[retPos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("retCls[retPos] = \"" + prmTypeC + "\";");
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 paramTypeC = checkAndGetCplusType(memTypes.get(i));
- String prmType = checkAndGetCplusArrayType(paramTypeC, members.get(i));
- println("retCls[retPos] = \"" + getSimpleType(getEnumType(prmType)) + "\";");
+ String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+ println("retCls[retPos] = \"" + prmTypeC + "\";");
print("retObj[retPos++] = &retStruct.");
print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
println(";");
println("paramObj[pos++] = &numStubs" + 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
+ String prmTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+ println("paramCls[pos] = \"" + prmTypeC + "\";");
+ if (isEnumClass(getGenericType(paramType))) // Check if this is enum type
println("paramObj[pos++] = ¶mEnumInt" + i);
else
println("paramObj[pos++] = &" + getSimpleIdentifier(methParams.get(i)) + ";");
}
}
}
- 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);
}
for (int i = 0; i < methParams.size(); i++) { // Print size variables
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isStructClass(simpleType)) {
if (!begin) { // Generate comma for not the beginning variable
print(", "); begin = false;
/**
- * HELPER: writeMethodHelperStructSetupCplusSkeleton() writes the method helper of the struct in skeleton class
+ * HELPER: writeMethodHelperStructSetupCplusSkeleton() writes the method helper of struct in skeleton class
*/
private void writeMethodHelperStructSetupCplusSkeleton(Collection<String> methods,
InterfaceDecl intDecl) {
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
print("int ___");
/**
- * HELPER: writeMethodHelperStructSetupCplusCallbackSkeleton() writes the method helper of the struct in skeleton class
+ * HELPER: writeMethodHelperStructSetupCplusCallbackSkeleton() writes the method helper of struct in skeleton class
*/
private void writeMethodHelperStructSetupCplusCallbackSkeleton(Collection<String> methods,
InterfaceDecl intDecl) {
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 (isStructClass(simpleType)) {
int methodNumId = intDecl.getMethodNumId(method);
print("int ___");
Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
- int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ int newObjectId = getNewIntfaceObjectId(newIntface);
println("if (_objectId == object" + newObjectId + "Id) {");
println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
println("cerr << \"Object with object Id: \" << _objectId << \" is not allowed to access method: \" << methodId << endl;");
println("exit(-1);");
println("}");
+ println("}");
println("else {");
println("cerr << \"Object Id: \" << _objectId << \" not recognized!\" << endl;");
println("exit(-1);");
println("}");
- println("}");
}
}
writePropertiesCplusSkeleton(intface, callbackExist, callbackClasses);
println("public:\n");
// Write constructor
- writeConstructorCplusSkeleton(newSkelClass, intface, callbackExist);
+ writeConstructorCplusSkeleton(newSkelClass, intface, callbackExist, intDecl, methods);
// Write deconstructor
writeDeconstructorCplusSkeleton(newSkelClass, callbackExist, callbackClasses);
// Write methods
writeCplusWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
println("};");
writePermissionInitializationCplus(intface, newSkelClass, intDecl);
+ writeObjectIdCountInitializationCplus(newSkelClass, callbackExist);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".hpp...");
println(intface + " *mainObj;");
// Keep track of object Ids of all stubs registered to this interface
- println("static int objectId;");
+ println("int objectId;");
// Callback
if (callbackExist) {
Iterator it = callbackClasses.iterator();
/**
* HELPER: writeConstructorCplusCallbackSkeleton() writes the constructor of the skeleton class
*/
- private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface, boolean callbackExist) {
+ private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
println(newSkelClass + "(" + intface + " *_mainObj, int _objectId) {");
println("mainObj = _mainObj;");
println("objectId = _objectId;");
- // Callback
- if (callbackExist) {
- println("objIdCnt = 0;");
- }
println("}\n");
}
/**
- * HELPER: writeMethodHelperCplusCallbackSkeleton() writes the method helper of the callback skeleton class
+ * HELPER: writeMethodHelperCplusCallbackSkeleton() writes the method helper of callback skeleton class
*/
private void writeMethodHelperCplusCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl,
Set<String> callbackClasses) {
for (int i = 0; i < methParams.size(); i++) { // Print size variables
String paramType = methPrmTypes.get(i);
String param = methParams.get(i);
- String simpleType = getSimpleType(paramType);
+ String simpleType = getGenericType(paramType);
if (isStructClass(simpleType)) {
if (!begin) { // Generate comma for not the beginning variable
print(", "); begin = false;
/**
- * HELPER: writeCplusCallbackWaitRequestInvokeMethod() writes the main loop of the skeleton class
+ * HELPER: writeCplusCallbackWaitRequestInvokeMethod() writes the request invoke method of the skeleton callback class
*/
private void writeCplusCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl,
boolean callbackExist) {
}
-
/**
* generateCplusCallbackSkeletonClass() generate callback skeletons based on the methods list in C++
*/
writePropertiesCplusCallbackSkeleton(intface, callbackExist, callbackClasses);
println("public:\n");
// Write constructor
- writeConstructorCplusCallbackSkeleton(newSkelClass, intface, callbackExist);
+ writeConstructorCplusCallbackSkeleton(newSkelClass, intface, callbackExist, intDecl, methods);
// Write deconstructor
writeDeconstructorCplusCallbackSkeleton(newSkelClass, callbackExist, callbackClasses);
// Write methods
// Write waitRequestInvokeMethod() - main loop
writeCplusCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist);
println("};");
+ writeObjectIdCountInitializationCplus(newSkelClass, callbackExist);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".hpp...");
}
- /**
- * generateReturnStmt() generate return statement based on methType
- */
- public String generateReturnStmt(String methType) {
-
- // Generate dummy returns for now
- if (methType.equals("short")||
- methType.equals("int") ||
- methType.equals("long") ||
- methType.equals("float")||
- methType.equals("double")) {
-
- return "1";
- } else if ( methType.equals("String")) {
-
- return "\"a\"";
- } else if ( methType.equals("char") ||
- methType.equals("byte")) {
-
- return "\'a\'";
- } else if ( methType.equals("boolean")) {
-
- return "true";
- } else {
- return "null";
- }
- }
-
-
/**
* setDirectory() sets a new directory for stub files
*/
pn = (ParseNode) parse.parse().value;
} catch (Exception e) {
e.printStackTrace();
- throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file);
+ throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file + "\n");
}
return pn;
/**================
- * Helper functions
+ * Basic helper functions
**================
*/
boolean newline=true;
/**
* 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;
}
+ /**
+ * A collection of methods with print-to-file functionality
+ */
private void println(String str) {
if (newline) {
int tab = tablevel;
}
- // Return parameter category, i.e. PRIMITIVES, NONPRIMITIVES, or USERDEFINED
+ // Check and find object Id for new interface in mapNewIntfaceObjId (callbacks)
+ // Throw an error if the new interface is not found!
+ // Basically the compiler needs to parse the policy (and requires) files for callback class first
+ private int getNewIntfaceObjectId(String newIntface) {
+
+ if (!mapNewIntfaceObjId.containsKey(newIntface)) {
+ throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
+ "Please place the two files for callback class in front...\n");
+ } else {
+ int retObjId = mapNewIntfaceObjId.get(newIntface);
+ return retObjId;
+ }
+ }
+
+
+ // Return parameter category, i.e. PRIMITIVES, NONPRIMITIVES, USERDEFINED, ENUM, or STRUCT
private ParamCategory getParamCategory(String paramType) {
if (mapPrimitives.containsKey(paramType)) {
// Return full class name for non-primitives to generate Java import statements
- // e.g. java.util.Set for Set, java.util.Map for Map
+ // e.g. java.util.Set for Set
private String getNonPrimitiveJavaClass(String paramNonPrimitives) {
return mapNonPrimitivesJava.get(paramNonPrimitives);
// Return full class name for non-primitives to generate Cplus include statements
- // e.g. #include <set> for Set, #include <map> for Map
+ // e.g. #include <set> for Set
private String getNonPrimitiveCplusClass(String paramNonPrimitives) {
return mapNonPrimitivesCplus.get(paramNonPrimitives);
}
+ // Generate a set of standard classes for import statements
+ private List<String> getStandardJavaIntfaceImportClasses() {
+
+ List<String> importClasses = new ArrayList<String>();
+ // Add the standard list first
+ importClasses.add("java.util.List");
+ importClasses.add("java.util.ArrayList");
+
+ return importClasses;
+ }
+
+
// Generate a set of standard classes for import statements
private List<String> getStandardJavaImportClasses() {
}
- // Generate a set of standard classes for import statements
+ // Combine all classes for import statements
private List<String> getAllLibClasses(Collection<String> stdLibClasses, Collection<String> libClasses) {
List<String> allLibClasses = new ArrayList<String>(stdLibClasses);
allLibClasses.add(str);
}
}
-
return allLibClasses;
}
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;
return type;
}
+ // Handle and return the correct enum declaration translate into int* for C
+ private String getEnumCplusClsType(String type) {
+
+ // Strips off array "[]" for return type
+ String pureType = getSimpleArrayType(type);
+ // Take the inner type of generic
+ if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+ pureType = getGenericType(type);
+ if (isEnumClass(pureType)) {
+ String enumType = "int*";
+ return enumType;
+ } else
+ return type;
+ }
+
// Handle and return the correct struct declaration
private String getStructType(String type) {
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;
}
+ // Print import statements into file
private void printImportStatements(Collection<String> importClasses) {
for(String cls : importClasses) {
}
+ // Print include statements into file
private void printIncludeStatements(Collection<String> includeClasses) {
for(String cls : includeClasses) {
}
+ // Checks and gets type in C++
private String checkAndGetCplusType(String paramType) {
if (getParamCategory(paramType) == ParamCategory.PRIMITIVES) {
} else
return getNonPrimitiveCplusClass(paramType);
} else if(paramType.contains("[]")) { // Array type (used for return type only)
- String cArray = "vector<" + getSimpleArrayType(paramType) + ">";
+ String cArray = "vector<" + convertType(getSimpleArrayType(paramType)) + ">";
return cArray;
} else if(getParamCategory(paramType) == ParamCategory.USERDEFINED) {
return paramType + "*";
}
+ // Return the class type for class resolution (for return value)
+ // - Check and return C++ array class, e.g. int A[] into int*
+ // - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
+ private String checkAndGetCplusRetClsType(String paramType) {
+
+ String paramTypeRet = null;
+ // Check for array declaration
+ if (paramType.contains("[]")) {
+ String type = paramType.split("\\[\\]")[0];
+ paramTypeRet = getSimpleArrayType(type) + "*";
+ } else if (paramType.contains("<") && paramType.contains(">")) {
+ // Just return it as is if it's not an array
+ String type = paramType.split("<")[1].split(">")[0];
+ paramTypeRet = "vector<" + getGenericType(type) + ">";
+ } else
+ paramTypeRet = paramType;
+
+ return paramTypeRet;
+ }
+
+
+ // Return the class type for class resolution (for method arguments)
+ // - Check and return C++ array class, e.g. int A[] into int*
+ // - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
+ private String checkAndGetCplusArgClsType(String paramType, String param) {
+
+ 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) + "*";
+ } else if (paramType.contains("<") && paramType.contains(">")) {
+ // Just return it as is if it's not an array
+ String type = paramType.split("<")[1].split(">")[0];
+ paramTypeRet = "vector<" + getGenericType(type) + ">";
+ } else
+ paramTypeRet = paramType;
+
+ return paramTypeRet;
+ }
+
+
// Detect array declaration, e.g. int A[],
// then generate type "int[]"
private String checkAndGetArray(String paramType, String param) {
}
- // Is array? For return type we put the return type as input parameter
+ // Is array?
+ // For return type we use retType as input parameter
private boolean isArray(String param) {
// Check for array declaration
} else {
throw new Error("IoTCompiler: Ambiguous stub interfaces: " + setExchInt.toString() +
". Only one new interface can be declared if the object " + intface +
- " needs to be passed in as an input parameter!");
+ " needs to be passed in as an input parameter!\n");
}
} else {
// NULL value - this means policy files missing
" If this is an array please type the brackets after the variable name," +
" e.g. \"String str[]\", not \"String[] str\"." +
" If this is a Collections (Java) / STL (C++) type, this compiler only" +
- " supports List/ArrayList (Java) or list (C++).");
+ " supports List/ArrayList (Java) or list (C++).\n");
}
}
// Error checking
if (!args[i].equals("-java") &&
!args[i].equals("-cplus")) {
- throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i]);
+ throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i] + "\n");
} else {
if (i + 1 < args.length) {
comp.setDirectory(args[i+1]);
} else
- throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i]);
+ throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i] + "\n");
if (args[i].equals("-java")) {
comp.generateEnumJava();
} else {
// Need to at least have exactly 2 parameters, i.e. main policy file and requires file
IoTCompiler.printUsage();
- throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!");
+ throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!\n");
}
}
}
-
-