- * generateJavaSkeletonClass() generate skeletons based on the methods list in Java
- */
- public void generateJavaSkeletonClass() throws IOException {
-
- // Create a new directory
- String path = createDirectories(dir, subdir);
- for (String intface : mapIntfacePTH.keySet()) {
- // Open a new file to write into
- String newSkelClass = intface + "_Skeleton";
- FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".java");
- pw = new PrintWriter(new BufferedWriter(fw));
- // Pass in set of methods and get import classes
- DeclarationHandler decHandler = mapIntDeclHand.get(intface);
- InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
- List<String> methods = intDecl.getMethods();
- Set<String> importClasses = getImportClasses(methods, intDecl);
- List<String> stdImportClasses = getStandardJavaImportClasses();
- List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
- printImportStatements(allImportClasses);
- // Find out if there are callback objects
- Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
- boolean callbackExist = !callbackClasses.isEmpty();
- // Write class header
- println("");
- println("public class " + newSkelClass + " implements " + intface + " {\n");
- // Write properties
- writePropertiesJavaSkeleton(intface, callbackExist, intDecl);
- // Write constructor
- writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
- // Write methods
- writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false);
- // Write method helper
- writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
- // Write waitRequestInvokeMethod() - main loop
- writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
- println("}");
- pw.close();
- System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java...");
- }
- }
-
-
- /**
- * HELPER: writePropertiesJavaCallbackSkeleton() writes the properties of the callback skeleton class
- */
- private void writePropertiesJavaCallbackSkeleton(String intface, boolean callbackExist) {
-
- println("private " + intface + " mainObj;");
- // For callback skeletons, this is its own object Id
- println("private static int objectId = 0;");
- // Callback
- if (callbackExist) {
- println("private static int objIdCnt = 0;");
- println("private IoTRMICall rmiCall;");
- }
- println("\n");
- }
-
-
- /**
- * HELPER: writeConstructorJavaCallbackSkeleton() writes the constructor of the skeleton class
- */
- private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
-
- println("public " + newSkelClass + "(" + intface + " _mainObj, int _objectId) throws Exception {");
- println("mainObj = _mainObj;");
- println("objectId = _objectId;");
- writeStructPermissionJavaSkeleton(methods, intDecl, intface);
- println("}\n");
- }
-
-
- /**
- * HELPER: writeMethodHelperJavaCallbackSkeleton() writes the method helper of the callback skeleton class
- */
- private void writeMethodHelperJavaCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
-
- // Use this set to handle two same methodIds
- Set<String> uniqueMethodIds = new HashSet<String>();
- for (String method : methods) {
-
- List<String> methParams = intDecl.getMethodParams(method);
- List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
- if (isStructPresent(methParams, methPrmTypes)) { // Treat struct differently
- String methodId = intDecl.getMethodId(method);
- print("public void ___");
- String helperMethod = methodId;
- if (uniqueMethodIds.contains(methodId))
- helperMethod = helperMethod + intDecl.getMethodNumId(method);
- else
- uniqueMethodIds.add(methodId);
- String retType = intDecl.getMethodType(method);
- print(helperMethod + "(");
- boolean begin = true;
- 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);
- if (isStructClass(simpleType)) {
- if (!begin) { // Generate comma for not the beginning variable
- print(", "); begin = false;
- }
- int methodNumId = intDecl.getMethodNumId(method);
- print("int struct" + methodNumId + "Size" + i);
- }
- }
- // Check if this is "void"
- if (retType.equals("void"))
- println(", IoTRMIObject rmiObj) {");
- else
- println(", IoTRMIObject rmiObj) throws IOException {");
- writeMethodHelperStructJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
- println("}\n");
- } else {
- String methodId = intDecl.getMethodId(method);
- print("public void ___");
- String helperMethod = methodId;
- if (uniqueMethodIds.contains(methodId))
- helperMethod = helperMethod + intDecl.getMethodNumId(method);
- else
- uniqueMethodIds.add(methodId);
- // Check if this is "void"
- String retType = intDecl.getMethodType(method);
- if (retType.equals("void"))
- println(helperMethod + "(IoTRMIObject rmiObj) {");
- else
- println(helperMethod + "(IoTRMIObject rmiObj) throws IOException {");
- // Now, write the helper body of skeleton!
- writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
- println("}\n");
- }
- }
- // Write method helper for structs
- writeMethodHelperStructSetupJavaCallbackSkeleton(methods, intDecl);
- }
-
-
- /**
- * HELPER: writeJavaCallbackWaitRequestInvokeMethod() writes the request invoke method of the callback skeleton class