From: rtrimana Date: Tue, 8 Nov 2016 21:32:11 +0000 (-0800) Subject: Refactoring method signature generations for interfaces and classes X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=c3bf5191e624be8b17e823804c84914afa916a1f;p=iot2.git Refactoring method signature generations for interfaces and classes --- diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 4450f1d..8849c4d 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -159,6 +159,32 @@ public class IoTCompiler { } + /** + * HELPER: writeMethodJavaInterface() writes the method of the interface + */ + private void writeMethodJavaInterface(Collection methods, InterfaceDecl intDecl) { + + for (String method : methods) { + + List methParams = intDecl.getMethodParams(method); + List methPrmTypes = intDecl.getMethodParamTypes(method); + print("public " + intDecl.getMethodType(method) + " " + + intDecl.getMethodId(method) + "("); + for (int i = 0; i < methParams.size(); i++) { + // Check for params with driver class types and exchange it + // with its remote interface + String paramType = checkAndGetParamClass(methPrmTypes.get(i), false); + print(paramType + " " + methParams.get(i)); + // Check if this is the last element (don't print a comma) + if (i != methParams.size() - 1) { + print(", "); + } + } + println(");"); + } + } + + /** * generateJavaLocalInterface() writes the local interface and provides type-checking. *

@@ -185,24 +211,7 @@ public class IoTCompiler { println(""); println("public interface " + intface + " {"); // Write methods - for (String method : methods) { - - List methParams = intDecl.getMethodParams(method); - List methPrmTypes = intDecl.getMethodParamTypes(method); - print("public " + intDecl.getMethodType(method) + " " + - intDecl.getMethodId(method) + "("); - for (int i = 0; i < methParams.size(); i++) { - // Check for params with driver class types and exchange it - // with its remote interface - String paramType = checkAndGetParamClass(methPrmTypes.get(i), false); - print(paramType + " " + methParams.get(i)); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(");"); - } + writeMethodJavaInterface(methods, intDecl); println("}"); pw.close(); System.out.println("IoTCompiler: Generated local interface " + intface + ".java..."); @@ -211,73 +220,73 @@ public class IoTCompiler { /** - * generateCplusLocalInterfaces() writes the local interfaces and provides type-checking. - *

- * It needs to rewrite and exchange USERDEFINED types in input parameters of stub - * and original interfaces, e.g. exchange Camera and CameraWithVideoAndRecording. - * The local interface has to be the input parameter for the stub and the stub - * interface has to be the input parameter for the local class. + * generateJavaInterfaces() generate stub interfaces based on the methods list in Java */ - public void generateCplusLocalInterfaces() throws IOException { + public void generateJavaInterfaces() throws IOException { // Create a new directory - createDirectory(dir); + String path = createDirectories(dir, subdir); for (String intface : mapIntfacePTH.keySet()) { - // Open a new file to write into - FileWriter fw = new FileWriter(dir + "/" + intface + ".hpp"); - pw = new PrintWriter(new BufferedWriter(fw)); - // Write file headers - println("#include "); - // Pass in set of methods and get include classes - DeclarationHandler decHandler = mapIntDeclHand.get(intface); - InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); - List methods = intDecl.getMethods(); - // DEBUGGG - Set includeClasses = getIncludeClasses(methods, intDecl); + Map> mapNewIntMethods = mapInt2NewInts.get(intface); + for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { - printIncludeStatements(includeClasses); - println(""); - println("using namespace std;"); - println(""); - println("class " + intface); - println("{"); - println("public:"); - // Write methods - for (String method : methods) { - - List methParams = intDecl.getMethodParams(method); - List methPrmTypes = intDecl.getMethodParamTypes(method); - print("virtual " + convertType(intDecl.getMethodType(method)) + " " + - intDecl.getMethodId(method) + "("); - for (int i = 0; i < methParams.size(); i++) { - // Check for params with driver class types and exchange it - // with its remote interface - String paramType = checkAndGetParamClass(methPrmTypes.get(i), true); - paramType = checkAndGetCplusType(paramType); - // Check for arrays - translate into vector in C++ - String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i)); - //print(paramType + " " + param); - print(paramComplete); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } + // Open a new file to write into + String newIntface = intMeth.getKey(); + FileWriter fw = new FileWriter(path + "/" + newIntface + ".java"); + pw = new PrintWriter(new BufferedWriter(fw)); + DeclarationHandler decHandler = mapIntDeclHand.get(intface); + InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); + // Pass in set of methods and get import classes + Set importClasses = getImportClasses(intMeth.getValue(), intDecl); + printImportStatements(importClasses); + // Write interface header + println(""); + println("public interface " + newIntface + " {"); + // Write methods + writeMethodJavaInterface(intMeth.getValue(), intDecl); + println("}"); + pw.close(); + System.out.println("IoTCompiler: Generated interface " + newIntface + ".java..."); + } + } + } + + + /** + * HELPER: writeMethodJavaStub() writes the method of the stub class + */ + private void writeMethodJavaStub(Collection methods, InterfaceDecl intDecl) { + + for (String method : methods) { + + List methParams = intDecl.getMethodParams(method); + List methPrmTypes = intDecl.getMethodParamTypes(method); + print("public " + intDecl.getMethodType(method) + " " + + intDecl.getMethodId(method) + "("); + for (int i = 0; i < methParams.size(); i++) { + + print(methPrmTypes.get(i) + " " + methParams.get(i)); + // Check if this is the last element (don't print a comma) + if (i != methParams.size() - 1) { + print(", "); } - println(") = 0;"); } - print("}"); - println(";"); - pw.close(); - System.out.println("IoTCompiler: Generated local interface " + intface + ".hpp..."); + println(") {"); + // Check if this is not "void" + if (!intDecl.getMethodType(method).equals("void")) { + String retStmt = generateReturnStmt(intDecl.getMethodType(method)); + println("return " + retStmt + ";"); + } + println("}"); println(""); } } /** - * generateJavaInterfaces() generate stub interfaces based on the methods list in Java + * generateJavaStubClasses() generate stubs based on the methods list in Java */ - public void generateJavaInterfaces() throws IOException { + public void generateJavaStubClasses() throws IOException { // Create a new directory String path = createDirectories(dir, subdir); @@ -288,7 +297,8 @@ public class IoTCompiler { // Open a new file to write into String newIntface = intMeth.getKey(); - FileWriter fw = new FileWriter(path + "/" + newIntface + ".java"); + String newStubClass = newIntface + "_Stub"; + FileWriter fw = new FileWriter(path + "/" + newStubClass + ".java"); pw = new PrintWriter(new BufferedWriter(fw)); DeclarationHandler decHandler = mapIntDeclHand.get(intface); InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); @@ -297,30 +307,83 @@ public class IoTCompiler { printImportStatements(importClasses); // Write interface header println(""); - println("public interface " + newIntface + " {"); - List meths = intDecl.getMethods(); + println("public class " + newStubClass + " implements " + newIntface + " {"); + println(""); // Write methods - for (String method : intMeth.getValue()) { - - List methParams = intDecl.getMethodParams(method); - List methPrmTypes = intDecl.getMethodParamTypes(method); - print("public " + intDecl.getMethodType(method) + " " + - intDecl.getMethodId(method) + "("); - for (int i = 0; i < methParams.size(); i++) { - String paramType = checkAndGetParamClass(methPrmTypes.get(i), false); - print(paramType + " " + methParams.get(i)); - //print(methPrmTypes.get(i) + " " + methParams.get(i)); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(");"); - } + writeMethodJavaStub(intMeth.getValue(), intDecl); println("}"); pw.close(); - System.out.println("IoTCompiler: Generated interface " + newIntface + ".java..."); + System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java..."); + } + } + } + + + /** + * HELPER: writeMethodCplusInterface() writes the method of the interface + */ + private void writeMethodCplusInterface(Collection methods, InterfaceDecl intDecl) { + + for (String method : methods) { + + List methParams = intDecl.getMethodParams(method); + List methPrmTypes = intDecl.getMethodParamTypes(method); + print("virtual " + convertType(intDecl.getMethodType(method)) + " " + + intDecl.getMethodId(method) + "("); + for (int i = 0; i < methParams.size(); i++) { + // Check for params with driver class types and exchange it + // with its remote interface + String paramType = checkAndGetParamClass(methPrmTypes.get(i), true); + paramType = checkAndGetCplusType(paramType); + // Check for arrays - translate into vector in C++ + String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i)); + print(paramComplete); + // Check if this is the last element (don't print a comma) + if (i != methParams.size() - 1) { + print(", "); + } } + println(") = 0;"); + } + } + + + /** + * generateCplusLocalInterfaces() writes the local interfaces and provides type-checking. + *

+ * It needs to rewrite and exchange USERDEFINED types in input parameters of stub + * and original interfaces, e.g. exchange Camera and CameraWithVideoAndRecording. + * The local interface has to be the input parameter for the stub and the stub + * interface has to be the input parameter for the local class. + */ + public void generateCplusLocalInterfaces() throws IOException { + + // Create a new directory + createDirectory(dir); + for (String intface : mapIntfacePTH.keySet()) { + // Open a new file to write into + FileWriter fw = new FileWriter(dir + "/" + intface + ".hpp"); + pw = new PrintWriter(new BufferedWriter(fw)); + // Write file headers + println("#include "); + // Pass in set of methods and get include classes + DeclarationHandler decHandler = mapIntDeclHand.get(intface); + InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); + List methods = intDecl.getMethods(); + Set includeClasses = getIncludeClasses(methods, intDecl); + printIncludeStatements(includeClasses); + println(""); + println("using namespace std;"); + println(""); + println("class " + intface); + println("{"); + println("public:"); + // Write methods + writeMethodCplusInterface(methods, intDecl); + print("}"); + println(";"); + pw.close(); + System.out.println("IoTCompiler: Generated local interface " + intface + ".hpp..."); } } @@ -357,24 +420,7 @@ public class IoTCompiler { println("{"); println("public:"); // Write methods - for (String method : intMeth.getValue()) { - - List methParams = intDecl.getMethodParams(method); - List methPrmTypes = intDecl.getMethodParamTypes(method); - print("virtual " + convertType(intDecl.getMethodType(method)) + " " + - intDecl.getMethodId(method) + "("); - for (int i = 0; i < methParams.size(); i++) { - String methPrmType = checkAndGetParamClass(methPrmTypes.get(i), true); - methPrmType = checkAndGetCplusType(methPrmType); - String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); - print(methParamComplete); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(") = 0;"); - } + writeMethodCplusInterface(intMeth.getValue(), intDecl); print("}"); println(";"); pw.close(); @@ -385,58 +431,35 @@ public class IoTCompiler { /** - * generateJavaStubClasses() generate stubs based on the methods list in Java + * HELPER: writeMethodCplusStub() writes the method of the stub */ - public void generateJavaStubClasses() throws IOException { - - // Create a new directory - String path = createDirectories(dir, subdir); - for (String intface : mapIntfacePTH.keySet()) { + private void writeMethodCplusStub(Collection methods, InterfaceDecl intDecl) { - Map> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { + for (String method : methods) { - // Open a new file to write into - String newIntface = intMeth.getKey(); - String newStubClass = newIntface + "_Stub"; - FileWriter fw = new FileWriter(path + "/" + newStubClass + ".java"); - pw = new PrintWriter(new BufferedWriter(fw)); - DeclarationHandler decHandler = mapIntDeclHand.get(intface); - InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); - // Pass in set of methods and get import classes - Set importClasses = getImportClasses(intMeth.getValue(), intDecl); - printImportStatements(importClasses); - // Write interface header - println(""); - println("public class " + newStubClass + " implements " + newIntface + " {"); - println(""); - // Write methods - for (String method : intMeth.getValue()) { - - List methParams = intDecl.getMethodParams(method); - List methPrmTypes = intDecl.getMethodParamTypes(method); - print("public " + intDecl.getMethodType(method) + " " + - intDecl.getMethodId(method) + "("); - for (int i = 0; i < methParams.size(); i++) { - - print(methPrmTypes.get(i) + " " + methParams.get(i)); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(") {"); - // Check if this is not "void" - if (!intDecl.getMethodType(method).equals("void")) { - String retStmt = generateReturnStmt(intDecl.getMethodType(method)); - println("return " + retStmt + ";"); - } - println("}"); println(""); + List methParams = intDecl.getMethodParams(method); + List methPrmTypes = intDecl.getMethodParamTypes(method); + print(convertType(intDecl.getMethodType(method)) + " " + + intDecl.getMethodId(method) + "("); + for (int i = 0; i < methParams.size(); i++) { + String methPrmType = checkAndGetCplusType(methPrmTypes.get(i)); + String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); + print(methParamComplete); + // Check if this is the last element (don't print a comma) + if (i != methParams.size() - 1) { + print(", "); } - println("}"); - pw.close(); - System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java..."); } + println(") { "); + // Check if this is not "void" + if (!intDecl.getMethodType(method).equals("void")) { + String retStmt = generateReturnStmt(intDecl.getMethodType(method)); + if (retStmt.equals("null")) { // null = NULL in C++ + retStmt = "NULL"; + } + println("return " + retStmt + ";"); + } + println("}"); println(""); } } @@ -469,32 +492,7 @@ public class IoTCompiler { DeclarationHandler decHandler = mapIntDeclHand.get(intface); InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); // Write methods - for (String method : intMeth.getValue()) { - - List methParams = intDecl.getMethodParams(method); - List methPrmTypes = intDecl.getMethodParamTypes(method); - print(convertType(intDecl.getMethodType(method)) + " " + - intDecl.getMethodId(method) + "("); - for (int i = 0; i < methParams.size(); i++) { - String methPrmType = checkAndGetCplusType(methPrmTypes.get(i)); - String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); - print(methParamComplete); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(") { "); - // Check if this is not "void" - if (!intDecl.getMethodType(method).equals("void")) { - String retStmt = generateReturnStmt(intDecl.getMethodType(method)); - if (retStmt.equals("null")) { // null = NULL in C++ - retStmt = "NULL"; - } - println("return " + retStmt + ";"); - } - println("}"); println(""); - } + writeMethodCplusStub(intMeth.getValue(), intDecl); print("}"); println(";"); pw.close(); System.out.println("IoTCompiler: Generated stub class " + newIntface + ".hpp...");