X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=iotjava%2Fiotpolicy%2FIoTCompiler.java;h=5c3ab9226913c2a771b8cea644078b4ff9d5911d;hb=f972344b2c1afa4169a61f7e61ce9a051d0f48a6;hp=c23896f3e016fa55e74f052c4bc01c5c4fbdcbf0;hpb=2da700a5060b2fbbe59363070515db175a5ce222;p=iot2.git diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index c23896f..5c3ab92 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -924,7 +924,7 @@ public class IoTCompiler { private String returnGenericCallbackType(String paramType) { if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES) - return getTypeOfGeneric(paramType)[0]; + return getGenericType(paramType); else return paramType; } @@ -953,12 +953,12 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String param = methParams.get(i); if (isArrayOrList(paramType, param)) { // Generate loop - println("for (" + paramType + " cb : " + getSimpleIdentifier(param) + ") {"); - println(callbackType + "_CallbackSkeleton skel = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); + println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {"); + println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); } else - println(callbackType + "_CallbackSkeleton skel = new " + callbackType + "_CallbackSkeleton(" + + println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" + getSimpleIdentifier(param) + ", objIdCnt++);"); - println("listCallbackObj.add(skel);"); + println("listCallbackObj.add(skel" + i + ");"); if (isArrayOrList(paramType, param)) println("}"); } @@ -1026,6 +1026,7 @@ public class IoTCompiler { */ private void writeMethodJavaStub(Collection methods, InterfaceDecl intDecl, Set callbackClasses) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -1057,8 +1058,10 @@ public class IoTCompiler { writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method); println("}\n"); // Write the init callback helper method - if (isCallbackMethod) + if (isCallbackMethod && !isDefined) { writeInitCallbackJavaStub(callbackType, intDecl); + isDefined = true; + } } } @@ -1116,7 +1119,7 @@ public class IoTCompiler { println("private String address;"); println("private int[] ports;\n"); // Get the object Id - println("private static int objectId = 0;"); + println("private int objectId = 0;"); if (callbackExist) { // We assume that each class only has one callback interface for now Iterator it = callbackClasses.iterator(); @@ -1301,8 +1304,8 @@ public class IoTCompiler { println("public void ___regCB(IoTRMIObject rmiObj) throws IOException {"); else println("public void ___regCB() throws IOException {"); - println("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class, String.class, int.class },"); - println("\tnew Class[] { null, null, null });"); + print("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class, String.class, int.class },"); + println("new Class[] { null, null, null });"); println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);"); println("}\n"); } @@ -1314,6 +1317,7 @@ public class IoTCompiler { private void writeMethodJavaSkeleton(Collection methods, InterfaceDecl intDecl, Set callbackClasses, boolean callbackSkeleton) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -1340,8 +1344,10 @@ public class IoTCompiler { // Now, write the body of skeleton! writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method)); println("}\n"); - if (isCallbackMethod) + if (isCallbackMethod && !isDefined) { // Make sure that this function is only defined once! writeInitCallbackJavaSkeleton(callbackSkeleton); + isDefined = true; + } } } @@ -1357,10 +1363,9 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - //if (callbackType.equals(paramType)) { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object println("try {"); - String exchParamType = checkAndGetParamClass(paramType); + String exchParamType = checkAndGetParamClass(getGenericType(paramType)); // Print array if this is array or list if this is a list of callback objects if (isArray(param)) { println("int numStubs" + i + " = (int) paramObj[" + i + "];"); @@ -1375,7 +1380,7 @@ public class IoTCompiler { } // Generate a loop if needed if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String exchParamType = checkAndGetParamClass(paramType); + String exchParamType = checkAndGetParamClass(getGenericType(paramType)); if (isArray(param)) { println("for (int objId = 0; objId < numStubs" + i + "; objId++) {"); println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);"); @@ -1520,7 +1525,6 @@ public class IoTCompiler { StructDecl structDecl = getStructDecl(simpleType); List memTypes = structDecl.getMemberTypes(simpleType); List members = structDecl.getMembers(simpleType); - println("int pos = 0;"); if (isArrayOrList(paramType, param)) { // An array or list int methodNumId = intDecl.getMethodNumId(method); String counter = "struct" + methodNumId + "Size" + iVar; @@ -1549,6 +1553,7 @@ public class IoTCompiler { private void writeStructMembersInitJavaSkeleton(InterfaceDecl intDecl, List methParams, List methPrmTypes, String method) { + println("int objPos = 0;"); for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); @@ -1566,7 +1571,6 @@ public class IoTCompiler { println("List<" + simpleType + "> paramStruct" + i + " = new ArrayList<" + simpleType + ">();"); } else println(simpleType + " paramStruct" + i + " = new " + simpleType + "();"); - println("int objPos = 0;"); // Initialize members StructDecl structDecl = getStructDecl(simpleType); List members = structDecl.getMembers(simpleType); @@ -1599,7 +1603,7 @@ public class IoTCompiler { } } else { // Take offsets of parameters - println("int offset" + i +" = objPos;"); + println("int offset" + i +" = objPos++;"); } } } @@ -1669,8 +1673,9 @@ public class IoTCompiler { checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes); Map mapStubParam = null; - if (isCallbackMethod) + if (isCallbackMethod) { mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType); + } // Check if this is "void" String retType = intDecl.getMethodType(method); if (retType.equals("void")) { @@ -1684,14 +1689,15 @@ public class IoTCompiler { } for (int i = 0; i < methParams.size(); i++) { - if (isCallbackMethod) { + String paramType = methPrmTypes.get(i); + if (isCallbackMethod && checkCallbackType(paramType, callbackType)) { 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)))) { + } else if (isEnumClass(getGenericType(paramType))) { // Enum class + print(getEnumParam(paramType, methParams.get(i), i)); + } else if (isStructClass(getGenericType(paramType))) { print("paramStruct" + i); } else { - String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); + String prmType = checkAndGetArray(paramType, methParams.get(i)); if (isStructMethod) print("(" + prmType + ") paramObj[offset" + i + "]"); else @@ -1735,6 +1741,7 @@ public class IoTCompiler { println(";"); println("Class[] paramCls = new Class[paramLen];"); println("Class[] paramClsGen = new Class[paramLen];"); + println("int pos = 0;"); // Iterate again over the parameters for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); @@ -1798,7 +1805,8 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String prmType = methPrmTypes.get(i); if ((getParamCategory(prmType) == ParamCategory.NONPRIMITIVES) && - !isEnumClass(getGenericType(prmType))) + !isEnumClass(getGenericType(prmType)) && + !callbackClasses.contains(getGenericType(prmType))) print(getGenericType(prmType) + ".class"); else print("null"); @@ -2165,7 +2173,7 @@ public class IoTCompiler { println("private " + intface + " mainObj;"); // For callback skeletons, this is its own object Id - println("private static int objectId = 0;"); + println("private int objectId = 0;"); // Callback if (callbackExist) { println("private static int objIdCnt = 0;"); @@ -2568,6 +2576,7 @@ public class IoTCompiler { */ private void writeMethodCplusStub(Collection methods, InterfaceDecl intDecl, Set callbackClasses) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -2578,7 +2587,7 @@ public class IoTCompiler { String callbackType = null; for (int i = 0; i < methParams.size(); i++) { - String paramType = methPrmTypes.get(i); + String paramType = returnGenericCallbackType(methPrmTypes.get(i)); // Check if this has callback object if (callbackClasses.contains(paramType)) { isCallbackMethod = true; @@ -2597,12 +2606,13 @@ public class IoTCompiler { if (isCallbackMethod) writeCallbackMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType); else - writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method); + writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackClasses); println("}\n"); // Write the init callback helper method - if (isCallbackMethod) { + if (isCallbackMethod && !isDefined) { writeInitCallbackCplusStub(callbackType, intDecl); writeInitCallbackSendInfoCplusStub(intDecl); + isDefined = true; } } } @@ -2623,7 +2633,7 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String param = methParams.get(i); if (isArrayOrList(paramType, param)) { // Generate loop - println("for (" + paramType + "* cb : " + getSimpleIdentifier(param) + ") {"); + println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {"); println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); isArrayOrList = true; callbackParam = getSimpleIdentifier(param); @@ -2631,7 +2641,7 @@ public class IoTCompiler { println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(" + getSimpleIdentifier(param) + ", objIdCnt++);"); println("vecCallbackObj.push_back(skel);"); - if (isArrayOrList(paramType, param)) + if (isArrayOrList) println("}"); } } @@ -2667,7 +2677,7 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object print("&___paramCB"); } else - print(getSimpleIdentifier(methParams.get(i))); + print("&" + getSimpleIdentifier(methParams.get(i))); if (i != methParams.size() - 1) print(", "); } @@ -2958,12 +2968,13 @@ public class IoTCompiler { * HELPER: writeStdMethodBodyCplusStub() writes the standard method body in the stub class */ private void writeStdMethodBodyCplusStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method) { + List methPrmTypes, String method, Set callbackClasses) { checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method); println("int methodId = " + intDecl.getMethodNumId(method) + ";"); String retType = intDecl.getMethodType(method); println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";"); + checkAndWriteEnumTypeCplusStub(methParams, methPrmTypes); // Generate array of parameter types if (isStructPresent(methParams, methPrmTypes)) { writeStructParamClassCplusStub(methParams, methPrmTypes); @@ -2971,15 +2982,19 @@ public class IoTCompiler { println("int numParam = " + methParams.size() + ";"); print("string paramCls[] = { "); for (int i = 0; i < methParams.size(); i++) { - String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); - print("\"" + paramTypeC + "\""); + String paramType = returnGenericCallbackType(methPrmTypes.get(i)); + if (callbackClasses.contains(paramType)) { + print("\"int\""); + } else { + 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(", "); } } println(" };"); - checkAndWriteEnumTypeCplusStub(methParams, methPrmTypes); // Generate array of parameter objects print("void* paramObj[] = { "); for (int i = 0; i < methParams.size(); i++) { @@ -3124,7 +3139,7 @@ public class IoTCompiler { 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("return;"); println("}"); } } @@ -3150,7 +3165,7 @@ public class IoTCompiler { 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);"); + println("return;"); println("}"); println("}"); println("}\n"); @@ -3188,7 +3203,7 @@ public class IoTCompiler { println("int methodId = " + methodNumId + ";"); //writeCplusCallbackPermission(intface, methodNumId); println("string retType = \"void\";"); - println("string paramCls[] = { \"int\", \"string\", \"int\" };"); + println("string paramCls[] = { \"int\", \"String\", \"int\" };"); println("int rev = 0;"); println("void* paramObj[] = { &ports[0], &address, &rev };"); println("void* retObj = NULL;"); @@ -3238,8 +3253,14 @@ public class IoTCompiler { // Write methods writeMethodCplusStub(methods, intDecl, callbackClasses); print("}"); println(";"); - if (callbackExist) - writePermissionInitializationCplus(intface, newStubClass, intDecl); + if (callbackExist) { + Iterator it = callbackClasses.iterator(); + String callbackType = (String) it.next(); + // Generate permission stuff for callback stubs + DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType); + InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType); + writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback); + } writeObjectIdCountInitializationCplus(newStubClass, callbackExist); println("#endif"); pw.close(); @@ -3334,8 +3355,14 @@ public class IoTCompiler { // Write methods writeMethodCplusStub(methods, intDecl, callbackClasses); println("};"); - if (callbackExist) - writePermissionInitializationCplus(intface, newStubClass, intDecl); + if (callbackExist) { + Iterator it = callbackClasses.iterator(); + String callbackType = (String) it.next(); + // Generate permission stuff for callback stubs + DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType); + InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType); + writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback); + } writeObjectIdCountInitializationCplus(newStubClass, callbackExist); println("#endif"); pw.close(); @@ -3388,7 +3415,7 @@ public class IoTCompiler { for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); - print("set " + newSkelClass + "::set" + newObjectId + "Allowed {"); + print("set " + newSkelClass + "::set" + newObjectId + "Allowed { "); Set methodIds = intMeth.getValue(); int i = 0; for (String methodId : methodIds) { @@ -3516,7 +3543,7 @@ public class IoTCompiler { println("int param1 = 0;"); println("string param2 = \"\";"); println("int param3 = 0;"); - println("string paramCls[] = { \"int\", \"string\", \"int\" };"); + println("string paramCls[] = { \"int\", \"String\", \"int\" };"); println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };"); println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); println("bool bResult = false;"); @@ -3531,6 +3558,7 @@ public class IoTCompiler { private void writeMethodCplusSkeleton(Collection methods, InterfaceDecl intDecl, Set callbackClasses, boolean callbackSkeleton) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -3560,8 +3588,10 @@ public class IoTCompiler { // Now, write the body of skeleton! writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method)); println("}\n"); - if (isCallbackMethod) + if (isCallbackMethod && !isDefined) { writeInitCallbackCplusSkeleton(callbackSkeleton); + isDefined = true; + } } } @@ -3575,11 +3605,8 @@ public class IoTCompiler { String paramType = methPrmTypes.get(i); String param = methParams.get(i); //if (callbackType.equals(paramType)) { - if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String exchParamType = checkAndGetParamClass(paramType); - // Print array if this is array or list if this is a list of callback objects + if (checkCallbackType(paramType, callbackType)) // Check if this has callback object println("int numStubs" + i + " = 0;"); - } } } @@ -3595,13 +3622,13 @@ public class IoTCompiler { String param = methParams.get(i); // Generate a loop if needed if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String exchParamType = checkAndGetParamClass(paramType); + String exchParamType = checkAndGetParamClass(getGenericType(paramType)); if (isArrayOrList(paramType, param)) { - println("vector<" + exchParamType + "> stub;"); + println("vector<" + exchParamType + "*> stub" + i + ";"); println("for (int objId = 0; objId < numStubs" + i + "; objId++) {"); println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);"); - println("stub" + i + ".push_back(cb);"); - println("vecCallbackObj.push_back(cb);"); + println("stub" + i + ".push_back(cb" + i + ");"); + println("vecCallbackObj.push_back(cb" + i + ");"); println("objIdCnt++;"); println("}"); } else { @@ -3811,7 +3838,6 @@ public class IoTCompiler { List members = structDecl.getMembers(simpleType); int methodNumId = intDecl.getMethodNumId(method); String counter = "struct" + methodNumId + "Size" + iVar; - println("int pos = 0;"); // Set up variables if (isArrayOrList(paramType, param)) { // An array or list for (int i = 0; i < members.size(); i++) { @@ -3944,6 +3970,7 @@ public class IoTCompiler { println(";"); println("string paramCls[numParam];"); println("void* paramObj[numParam];"); + println("int pos = 0;"); // Iterate again over the parameters for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); @@ -3970,7 +3997,7 @@ public class IoTCompiler { 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); + println("paramObj[pos++] = ¶mEnumInt" + i + ";"); else println("paramObj[pos++] = &" + getSimpleIdentifier(methParams.get(i)) + ";"); } @@ -4123,12 +4150,12 @@ public class IoTCompiler { 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("return;"); println("}"); println("}"); println("else {"); println("cerr << \"Object Id: \" << _objectId << \" not recognized!\" << endl;"); - println("exit(-1);"); + println("return;"); println("}"); } } @@ -5058,14 +5085,14 @@ public class IoTCompiler { if (paramType.contains("<") && paramType.contains(">")) { String genericClass = getSimpleType(paramType); - String[] genericType = getTypeOfGeneric(paramType); + String genericType = getGenericType(paramType); String cplusTemplate = null; - if (genericType.length == 1) // Generic/template with one type - cplusTemplate = getNonPrimitiveCplusClass(genericClass) + - "<" + convertType(genericType[0]) + ">"; - else // Generic/template with two types - cplusTemplate = getNonPrimitiveCplusClass(genericClass) + - "<" + convertType(genericType[0]) + "," + convertType(genericType[1]) + ">"; + cplusTemplate = getNonPrimitiveCplusClass(genericClass); + if(getParamCategory(getGenericType(paramType)) == ParamCategory.USERDEFINED) { + cplusTemplate = cplusTemplate + "<" + genericType + "*>"; + } else { + cplusTemplate = cplusTemplate + "<" + convertType(genericType) + ">"; + } return cplusTemplate; } else return getNonPrimitiveCplusClass(paramType); @@ -5077,7 +5104,6 @@ public class IoTCompiler { } else // Just return it as is if it's not non-primitives return paramType; - //return checkAndGetParamClass(paramType, true); } @@ -5242,6 +5268,9 @@ public class IoTCompiler { // Check if this is generics if(getParamCategory(paramType) == ParamCategory.USERDEFINED) { return exchangeParamType(paramType); + } else if (isList(paramType) && + (getParamCategory(getGenericType(paramType)) == ParamCategory.USERDEFINED)) { + return "List<" + exchangeParamType(getGenericType(paramType)) + ">"; } else return paramType; }