Refactoring method signature generations for interfaces and classes
authorrtrimana <rtrimana@uci.edu>
Tue, 8 Nov 2016 21:32:11 +0000 (13:32 -0800)
committerrtrimana <rtrimana@uci.edu>
Tue, 8 Nov 2016 21:32:11 +0000 (13:32 -0800)
iotjava/iotpolicy/IoTCompiler.java

index 4450f1d3c74a7e39b0a00069a92c158e9bb266cc..8849c4d5dc55ea7471a2df4025c536be204e76b3 100644 (file)
@@ -159,6 +159,32 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeMethodJavaInterface() writes the method of the interface
+        */
+       private void writeMethodJavaInterface(Collection<String> methods, InterfaceDecl intDecl) {
+
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> 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.
         * <p>
@@ -185,24 +211,7 @@ public class IoTCompiler {
                        println("");
                        println("public interface " + intface + " {");
                        // Write methods
-                       for (String method : methods) {
-
-                               List<String> methParams = intDecl.getMethodParams(method);
-                               List<String> 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.
-        * <p>
-        * 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 <iostream>");
-                       // Pass in set of methods and get include classes
-                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
-                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
-                       List<String> methods = intDecl.getMethods();
 
-                       // DEBUGGG
-                       Set<String> includeClasses = getIncludeClasses(methods, intDecl);
+                       Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+                       for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
 
-                       printIncludeStatements(includeClasses);
-                       println("");
-                       println("using namespace std;");
-                       println("");
-                       println("class " + intface);
-                       println("{");
-                       println("public:");
-                       // Write methods
-                       for (String method : methods) {
-
-                               List<String> methParams = intDecl.getMethodParams(method);
-                               List<String> 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<String> 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<String> methods, InterfaceDecl intDecl) {
+
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> 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<String> meths = intDecl.getMethods();
+                               println("public class " + newStubClass + " implements " + newIntface + " {");
+                               println("");
                                // Write methods
-                               for (String method : intMeth.getValue()) {
-
-                                       List<String> methParams = intDecl.getMethodParams(method);
-                                       List<String> 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<String> methods, InterfaceDecl intDecl) {
+
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> 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.
+        * <p>
+        * 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 <iostream>");
+                       // Pass in set of methods and get include classes
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                       List<String> methods = intDecl.getMethods();
+                       Set<String> 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<String> methParams = intDecl.getMethodParams(method);
-                                       List<String> 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<String> methods, InterfaceDecl intDecl) {
 
-                       Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-                       for (Map.Entry<String,Set<String>> 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<String> 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<String> methParams = intDecl.getMethodParams(method);
-                                       List<String> 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<String> methParams = intDecl.getMethodParams(method);
+                       List<String> 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<String> methParams = intDecl.getMethodParams(method);
-                                       List<String> 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...");