| |
---|
| | if (update.getBody() == null || !update.getBody().getStatements().contains(cashStatement)) { |
---|
| | update.addFirstStatement(cashStatement); |
---|
| | } |
---|
| | } |
---|
| | // to convert a json param to a tuple object. |
---|
| | // to convert a json param to a tuple or pair object. |
---|
| | for (VariableDeclaration param: update.getParameters()) { |
---|
| | Type paramType = param.getType(); |
---|
| | String paramName = param.getName(); |
---|
| | String paramConverter = ""; |
---|
| |
---|
| | paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(str, HashMap.class);\n"; |
---|
| | paramConverter += "\t" + paramName + ".add(" + getCodeForConversionFromMapToTuple(compType, "i") + ");\n"; |
---|
| | paramConverter += "}"; |
---|
| | update.addThrow("JsonProcessingException"); |
---|
| | } else if (DataConstraintModel.typePair.isAncestorOf(compType)) { |
---|
| | param.setType(DataConstraintModel.typeListStr); |
---|
| | param.setName(paramName + "_json"); |
---|
| | paramConverter += paramType.getInterfaceTypeName() + " " + paramName + " = new " + paramType.getImplementationTypeName() + "();\n"; |
---|
| | paramConverter += "for (String str: " + param.getName() + ") {\n"; |
---|
| | String mapTypeName = convertFromEntryToMapType(compType); |
---|
| | paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(str, HashMap.class);\n"; |
---|
| | paramConverter += "\t" + paramName + ".add(" + getCodeForConversionFromMapToPair(compType, "i") + ");\n"; |
---|
| | paramConverter += "}"; |
---|
| | update.addThrow("JsonProcessingException"); |
---|
| | } |
---|
| | } else if (DataConstraintModel.typeTuple.isAncestorOf(paramType)) { |
---|
| | param.setType(DataConstraintModel.typeString); |
---|
| | param.setName(paramName + "_json"); |
---|
| | paramConverter += paramType.getInterfaceTypeName() + " " + paramName + ";\n"; |
---|
| | paramConverter += "{\n"; |
---|
| | String mapTypeName = convertFromEntryToMapType(paramType); |
---|
| | paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(" + paramName + "_json" + ", HashMap.class);\n"; |
---|
| | paramConverter += "\t" + paramName + " = " + getCodeForConversionFromMapToTuple(paramType, "i") + ";\n"; |
---|
| | paramConverter += "}"; |
---|
| | update.addThrow("JsonProcessingException"); |
---|
| | } else if (DataConstraintModel.typePair.isAncestorOf(paramType)) { |
---|
| | param.setType(DataConstraintModel.typeString); |
---|
| | param.setName(paramName + "_json"); |
---|
| | paramConverter += paramType.getInterfaceTypeName() + " " + paramName + ";\n"; |
---|
| | paramConverter += "{\n"; |
---|
| | String mapTypeName = convertFromEntryToMapType(paramType); |
---|
| | paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(" + paramName + "_json" + ", HashMap.class);\n"; |
---|
| | paramConverter += "\t" + paramName + " = " + getCodeForConversionFromMapToPair(paramType, "i") + ";\n"; |
---|
| | paramConverter += "}"; |
---|
| | update.addThrow("JsonProcessingException"); |
---|
| | } |
---|
| | if (paramConverter.length() > 0) update.addFirstStatement(paramConverter); |
---|
| |
---|
| | |
---|
| | private static void generatePullDataTransfer(MethodDeclaration methodBody, String fromResourceName, Type fromResourceType) { |
---|
| | String varName = new String(fromResourceName); |
---|
| | String respTypeName = fromResourceType.getInterfaceTypeName(); |
---|
| | String respImplTypeName = fromResourceType.getImplementationTypeName(); |
---|
| | String respConverter = ""; |
---|
| | if (DataConstraintModel.typeList.isAncestorOf(fromResourceType) && fromResourceType != DataConstraintModel.typeList) { |
---|
| | Type compType = TypeInference.getListComponentType(fromResourceType); |
---|
| | if (DataConstraintModel.typeTuple.isAncestorOf(compType)) { |
---|
| |
---|
| | respConverter += "}"; |
---|
| | methodBody.addThrow("JsonProcessingException"); |
---|
| | } |
---|
| | } else if (DataConstraintModel.typeTuple.isAncestorOf(fromResourceType)) { |
---|
| | // varName += "_json"; |
---|
| | respTypeName = fromResourceType.getInterfaceTypeName(); |
---|
| | // respConverter += fromResourceName + " = " + getCodeForConversionFromMapToTuple(fromResourceType, varName) + ";"; |
---|
| | // methodBody.addThrow("JsonProcessingException"); |
---|
| | varName += "_json"; |
---|
| | respTypeName = convertFromEntryToMapType(fromResourceType); |
---|
| | respConverter += fromResourceType.getInterfaceTypeName() + " " + fromResourceName + " = " + getCodeForConversionFromMapToTuple(fromResourceType, varName) + ";"; |
---|
| | respImplTypeName = "HashMap"; |
---|
| | } else if (DataConstraintModel.typePair.isAncestorOf(fromResourceType)) { |
---|
| | varName += "_json"; |
---|
| | respTypeName = convertFromEntryToMapType(fromResourceType); |
---|
| | respConverter += fromResourceType.getInterfaceTypeName() + " " + fromResourceName + " = " + getCodeForConversionFromMapToPair(fromResourceType, varName) + ";"; |
---|
| | respImplTypeName = "HashMap"; |
---|
| | } |
---|
| | if (respConverter.length() > 0) { |
---|
| | methodBody.addFirstStatement(respConverter); |
---|
| | } |
---|
| | methodBody.addFirstStatement(respTypeName + " " + varName + " = " + getHttpMethodCallStatementWithResponse(baseURL, fromResourceName, "get", fromResourceType.getImplementationTypeName())); |
---|
| | methodBody.addFirstStatement(respTypeName + " " + varName + " = " + getHttpMethodCallStatementWithResponse(baseURL, fromResourceName, "get", respImplTypeName)); |
---|
| | } |
---|
| | |
---|
| | private static String convertFromEntryToMapType(Type type) { |
---|
| | String mapTypeName = type.getInterfaceTypeName(); |
---|
| | mapTypeName = mapTypeName.replace("Map.Entry", "Map"); |
---|
| | for (int idx = mapTypeName.indexOf("<", 0); idx >= 0; idx = mapTypeName.indexOf("<", idx + 1)) { |
---|
| | int to = mapTypeName.indexOf(",", idx); |
---|
| | if (to > idx) { |
---|
| | mapTypeName = mapTypeName.substring(0, idx + 1) + "String" + mapTypeName.substring(to); // All elements except for the last one have the string type. |
---|
| | String mapTypeName = null; |
---|
| | if (DataConstraintModel.typePair.isAncestorOf(type)) { |
---|
| | Type compType = TypeInference.getPairComponentType(type); |
---|
| | String wrapperType = DataConstraintModel.getWrapperType(compType); |
---|
| | if (wrapperType != null) { |
---|
| | mapTypeName = "Map<String, " + wrapperType + ">"; |
---|
| | } else { |
---|
| | mapTypeName = "Map<String, " + compType.getInterfaceTypeName() + ">"; |
---|
| | } |
---|
| | } else { |
---|
| | mapTypeName = type.getInterfaceTypeName(); |
---|
| | mapTypeName = mapTypeName.replace("Map.Entry", "Map"); |
---|
| | for (int idx = mapTypeName.indexOf("<", 0); idx >= 0; idx = mapTypeName.indexOf("<", idx + 1)) { |
---|
| | int to = mapTypeName.indexOf(",", idx); |
---|
| | if (to > idx) { |
---|
| | mapTypeName = mapTypeName.substring(0, idx + 1) + "String" + mapTypeName.substring(to); // All elements except for the last one have the string type. |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | return mapTypeName; |
---|
| | } |
---|
| |
---|
| | List<Type> elementsTypes = TypeInference.getTupleComponentTypes(tupleType); |
---|
| | String elementBase = mapVar; |
---|
| | for (Type elmType: elementsTypes.subList(0, elementsTypes.size() - 1)) { |
---|
| | elementBase += ".entrySet().iterator().next()"; |
---|
| | if (elmType == DataConstraintModel.typeBoolean) { |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Boolean.parseBoolean(" + elementBase + ".getKey()), $x)"); |
---|
| | } else if (elmType == DataConstraintModel.typeInt) { |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Integer.parseInt(" + elementBase + ".getKey()), $x)"); |
---|
| | } else if (elmType == DataConstraintModel.typeLong) { |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Long.parseLong(" + elementBase + ".getKey()), $x)"); |
---|
| | } else if (elmType == DataConstraintModel.typeFloat) { |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Float.parseFloat(" + elementBase + ".getKey()), $x)"); |
---|
| | } else if (elmType == DataConstraintModel.typeDouble) { |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Double.parseDouble(" + elementBase + ".getKey()), $x)"); |
---|
| | if (elmType == DataConstraintModel.typeBoolean |
---|
| | || elmType == DataConstraintModel.typeInt |
---|
| | || elmType == DataConstraintModel.typeLong |
---|
| | || elmType == DataConstraintModel.typeFloat |
---|
| | || elmType == DataConstraintModel.typeDouble) { |
---|
| | String elmVal = CodeUtil.getToValueExp(elmType.getImplementationTypeName(), elementBase + ".getKey()"); |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(" + elmVal + ", $x)"); |
---|
| | } else if (elmType == DataConstraintModel.typeString) { |
---|
| | decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(" + elementBase + ".getKey(), $x)"); |
---|
| | } else { |
---|
| | // To do. |
---|
| | } |
---|
| | elementBase += ".getValue()"; |
---|
| | } |
---|
| | decoded = decoded.replace("$x", elementBase); |
---|
| | return decoded; |
---|
| | } |
---|
| | |
---|
| | private static String getCodeForConversionFromMapToPair(Type pairType, String mapVar) { |
---|
| | String decoded = "$x"; |
---|
| | decoded = decoded.replace("$x", "new Pair<>(" + mapVar + ".get(\"left\"), $x)"); |
---|
| | decoded = decoded.replace("$x", mapVar + ".get(\"right\")"); |
---|
| | return decoded; |
---|
| | } |
---|
| | |
---|
| | private static String getHttpMethodParamsStatement(String callerResourceName, Type paramType, String paramName, String value) { |
---|
| |
---|
| | statements += "for (" + compType.getInterfaceTypeName() + " i: " + value + ") {\n"; |
---|
| | } else { |
---|
| | statements += "for (" + wrapperType + " i: " + value + ") {\n"; |
---|
| | } |
---|
| | if (DataConstraintModel.typeTuple.isAncestorOf(compType) || DataConstraintModel.typeList.isAncestorOf(compType)) { |
---|
| | statements += "\tform.param(\"" + paramName + "\", new ObjectMapper().writeValueAsString(i));\n"; |
---|
| | if (DataConstraintModel.typeTuple.isAncestorOf(compType) || DataConstraintModel.typePair.isAncestorOf(paramType) || DataConstraintModel.typeList.isAncestorOf(compType)) { |
---|
| | statements += "\tform.param(\"" + paramName + "\", new ObjectMapper().writeValueAsString(i));\n"; // typeTuple: {"1.0":2.0}, typePair: {"left": 1.0, "right":2.0} |
---|
| | } else { |
---|
| | statements += "\tform.param(\"" + paramName + "\", i.toString());\n"; |
---|
| | } |
---|
| | statements += "}\n"; |
---|
| | statements += "Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);"; |
---|
| | return statements; |
---|
| | // return "Entity<String> entity = Entity.entity(" + paramName + ".toString(), MediaType.APPLICATION_JSON);"; |
---|
| | } |
---|
| | return "Entity<Form> entity = Entity.entity(new Form().param(\"" + paramName + "\", " + CodeUtil.getToStringExp(paramType.getImplementationTypeName(), value) + "), MediaType.APPLICATION_FORM_URLENCODED_TYPE);"; |
---|
| | } else if (DataConstraintModel.typeTuple.isAncestorOf(paramType) || DataConstraintModel.typePair.isAncestorOf(paramType)) { |
---|
| | // typeTuple: {"1.0":2.0}, typePair: {"left": 1.0, "right":2.0} |
---|
| | return "Entity<Form> entity = Entity.entity(new Form().param(\"" + paramName + "\", new ObjectMapper().writeValueAsString(" + value + ")), MediaType.APPLICATION_FORM_URLENCODED_TYPE);"; |
---|
| | } else { |
---|
| | return "Entity<Form> entity = Entity.entity(new Form().param(\"" + paramName + "\", " + CodeUtil.getToStringExp(paramType.getImplementationTypeName(), value) + "), MediaType.APPLICATION_FORM_URLENCODED_TYPE);"; |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | private static String getHttpMethodCallStatement(String baseURL, String resourceName, String srcResName, String httpMethod) { |
---|
| | if (srcResName == null) { |
---|
| |
---|
| | |