・JAX-RS 出力での Pair クラス対応。
・Tuple 型のパラメータとレスポンスの受け渡しが正しくできていなかった?のを修正。
・if 文で用いるローカル変数の名前が競合していたのを修正。
1 parent 3eabcb3 commit e1a5a0f83e4b0f50a2499da20fda1a990580619a
Naoya Nitta authored on 1 Jan 2022
Showing 3 changed files
View
6
AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java
getter.setBody(new Block());
getter.getBody().addStatement("return " + field.getName() + ";");
type.addMethod(getter);
}
// MethodDeclaration toStr = new MethodDeclaration("toString", false, DataConstraintModel.typeString, null);
// block = new Block();
// block.addStatement("return \"{\\\"\" + left + \"\\\":\\\"\" + right + \"\\\"}\";");
// toStr.setBody(block);
// type.addMethod(toStr);
CompilationUnit cu = new CompilationUnit(type);
cu.addImport(new ImportDeclaration("java.util.*"));
codes.add(cu);
View
121
AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java
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) {
View
2
■■■
AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java
final int count[] = {0};
@Override
public String generate(Type type, String[] children, String[] sideEffects) {
// TODO Auto-generated method stub
String temp = "temp_l" + count[0];
String temp = "temp_if" + count[0];
String impl = "";
impl += type.getInterfaceTypeName() + " " + temp + ";\n";
impl += "if (" + children[0] + ") {\n";