diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 9e44133..f368601 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -258,65 +258,7 @@ private void deletePutMethodInvocation(ASTRewrite astRewrite, CompilationUnit srcUnit, String resourceName) { var updateMethods = searchMethodDeclarations(srcUnit, "Message"); for (MethodDeclaration md : updateMethods) { - LinkedHashMap> invocations = new LinkedHashMap<>(); - List statements = new ArrayList<>(); - - boolean[] isRegistering = new boolean[1]; - for (var st : md.getBody().statements()) { - Statement statement = (Statement) st; - - statement.accept(new ASTVisitor() { - - @Override - public boolean visit(VariableDeclarationStatement node) { - if (node.getType().toString().equals("Form")) { - startRegister(); - } - if (node.getType().toString().equals("String")) { - if (((VariableDeclarationFragment) node.fragments().get(0)).getName().toString() - .equals("result")) { - - MethodInvocation right = (MethodInvocation) ((VariableDeclarationFragment) node - .fragments().get(0)).getInitializer(); - String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) - .getExpression()).arguments().get(0).toString(); - stopRegister(resourceName); - } - } - return super.visit(node); - } - - @Override - public boolean visit(Assignment node) { - if (node.getLeftHandSide().toString().equals("form")) { - startRegister(); - } - if (node.getLeftHandSide().toString().equals("result")) { - MethodInvocation right = (MethodInvocation) node.getRightHandSide(); - String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) - .getExpression()).arguments().get(0).toString(); - stopRegister(resourceName); - } - return super.visit(node); - } - - private void startRegister() { - isRegistering[0] = true; - - statements.clear(); - } - - private void stopRegister(String resourceName) { - statements.add(statement); - invocations.put(resourceName, List.copyOf(statements)); - isRegistering[0] = false; - } - }); - if (isRegistering[0]) { - statements.add(statement); - } - } - + LinkedHashMap> invocations = getPutMethodInvocations(md); System.out.println(invocations); // ���\�b�h�̌Ăяo���Q��2�ˆȏ�ł���, // invocations����Ή����\�[�X�ւ̃��\�b�h�Ăяo���Q��, @@ -364,14 +306,15 @@ } } + /** * �����Ƃ��ēn���ꂽMethodDeclaration�̃��\�b�h�{�f�B���ɂ���PUT���\�b�h�̌Ăяo�����擾���郁�\�b�h * * @param md * @return */ - private LinkedHashMap> getPutMethodInvocations(MethodDeclaration md){ - LinkedHashMap> invocations = new LinkedHashMap<>(); + private LinkedHashMap> getPutMethodInvocations(MethodDeclaration md) { + LinkedHashMap> invocations = new LinkedHashMap<>(); List statements = new ArrayList<>(); boolean[] isRegistering = new boolean[1]; @@ -389,8 +332,8 @@ if (((VariableDeclarationFragment) node.fragments().get(0)).getName().toString() .equals("result")) { - MethodInvocation right = (MethodInvocation) ((VariableDeclarationFragment) node - .fragments().get(0)).getInitializer(); + MethodInvocation right = (MethodInvocation) ((VariableDeclarationFragment) node.fragments() + .get(0)).getInitializer(); String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) .getExpression()).arguments().get(0).toString(); stopRegister(resourceName); @@ -431,6 +374,7 @@ } return invocations; } + public MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { List result = new ArrayList(); cu.accept(new ASTVisitor() { @@ -499,7 +443,7 @@ if ((type instanceof ParameterizedType && ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) { - return "Map"; + return "Map"; } return type.toString(); } @@ -559,10 +503,40 @@ // Handling �N���X�� updateAvailable() ���\�b�h�� updateRequest() ���\�b�h�̍Ō�ɁCShipping �N���X�� // updateHandling() ���\�b�h���t�B�[���h value �������Ƃ��ēn���ČĂяo���s��lj�����D - for(var update : messages) { + Type srcType = searchFieldDeclaration(srcUnit, "State").getType(); + for (var update : messages) { + var invocations = getPutMethodInvocations(update); + String code = ""; + if (invocations.size() == 0) + code += "Form "; + code += "form = new Form();" + System.getProperty("line.separator"); + if (srcType instanceof ParameterizedType) { + var ptype = (ParameterizedType) srcType; + if (ptype.getType().toString().equals("List")) { + var typeArg = (Type) ptype.typeArguments().get(0); + code += "for ("+typeArg+" i: this.value) {" + System.getProperty("line.separator"); + code += "\t form.param(\"" + srcValue +"\", new ObjectMapper().writeValueAsString(i);"+ System.getProperty("line.separator"); + code += "}" + System.getProperty("line.separator"); + } + if (ptype.getType().toString().equals("Map.Entry")) { + code += "form.param(\"" + srcValue + "\", new ObjectMapper().writeValueAsString(this.value));" + + System.getProperty("line.separator"); + } + }else { + code += "form.param(\"" + srcValue + "\", new ObjectMapper().writeValueAsString(this.value));" + + System.getProperty("line.separator"); + } + if (invocations.size() == 0) + code += "Entity "; + code += "entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);" + + System.getProperty("line.separator"); + if (invocations.size() == 0) + code += "String "; + code += "result = client.target(\"http://localhost:8080\").path(this." + dstValue + + ").request().put(entity, String.class);" + System.getProperty("line.separator"); + addStatementLast(srcUnitRewrite, srcUnit, update.getBody(), code, Block.STATEMENTS_PROPERTY); } - var dstDec = new TypeDeclaration[1]; descriptor.getDistinationClass().accept(new ASTVisitor() { @@ -579,23 +553,22 @@ // Shipping �N���X�ɏo�Ɏw���̓��e��ۑ����邽�߂̃t�B�[���h Item value ��lj�����D Type dstType = searchMethodDeclaration(dstUnit, "Getter").getReturnType2(); - Type srcType = searchFieldDeclaration(srcUnit, "State").getType(); + String stateCode = "@State" + System.getProperty("line.separator") + dstType.toString() + " value;"; addStatement(dstUnitRewrite, dstUnit, dstDec[0], stateCode); // Shipping �N���X�� void updateHandling(ItemHandling handling) ���\�b�h��lj�����D // Shipping �N���X�� getValue() // ���\�b�h���ōs���Ă����C���[�U�̓��͂ɑ΂��鏈�����e(ItemHandling)�����Ƃɏo�Ɏw���̓��e(Item)���쐬���鏈�����CupdateHandling() // ���\�b�h�Ɉړ����C�o�Ɏw���̓��e�� value �t�B�[���h�ɕۑ�����悤�ɂ���D - String updateCode = generatePutMethod(dstType, new Type[] {srcType}, new String[]{srcValue}); + String updateCode = generatePutMethod(dstType, new Type[] { srcType }, new String[] { srcValue }); addStatement(dstUnitRewrite, dstUnit, dstDec[0], updateCode); // Shipping �N���X�� getValue() ���\�b�h�� value �t�B�[���h�̒l��Ԃ��悤�ɏ���������D Statement statement = (Statement) dstUnitRewrite.createStringPlaceholder("{ return this.value; }", ASTNode.VARIABLE_DECLARATION_STATEMENT); replaceASTNode(dstUnitRewrite, dstUnit, searchMethodDeclaration(dstUnit, "Getter").getBody(), statement); - try { - //applyRewrite(srcUnit, srcUnitRewrite, pm); + applyRewrite(srcUnit, srcUnitRewrite, pm); applyRewrite(dstUnit, dstUnitRewrite, pm); } catch (Exception e1) { @@ -604,61 +577,65 @@ } } - public String generatePutMethod(Type updatedValueType ,Type[] paramTypes, String[] srcNames) { + public String generatePutMethod(Type updatedValueType, Type[] paramTypes, String[] srcNames) { var args = "@FormParam(\"" + srcNames[0] + "\") String " + srcNames[0] + "_json"; - String srcs = Arrays.stream(srcNames).map(x -> "\""+x+"\"").collect(Collectors.joining(", ")); - String result = "@PUT" + System.getProperty("line.separator") + "@Message({" +srcs + "})" + String srcs = Arrays.stream(srcNames).map(x -> "\"" + x + "\"").collect(Collectors.joining(", ")); + String result = "@PUT" + System.getProperty("line.separator") + "@Message({" + srcs + "})" + System.getProperty("line.separator") + "public void update" + "(" + args + ") throws JsonProcessingException {" + System.getProperty("line.separator"); - //�^�錾 - for (int i = 0 ; i < paramTypes.length ; i++) { + // �^�錾 + for (int i = 0; i < paramTypes.length; i++) { - - result += paramTypes[i].toString() + " " + srcNames[i]+";" + System.getProperty("line.separator") ; + result += paramTypes[i].toString() + " " + srcNames[i] + ";" + System.getProperty("line.separator"); } - for (int i = 0 ; i < paramTypes.length ; i++) { - result += "{"+ System.getProperty("line.separator") + "\t"; - if(paramTypes[i] instanceof ParameterizedType) { - var ptype =(ParameterizedType) paramTypes[i]; + for (int i = 0; i < paramTypes.length; i++) { + result += "{" + System.getProperty("line.separator") + "\t"; + if (paramTypes[i] instanceof ParameterizedType) { + var ptype = (ParameterizedType) paramTypes[i]; if (ptype.getType().toString().equals("List")) { - + } if (ptype.getType().toString().equals("Map.Entry")) { - result += ptype.toString() +" i = new ObjectMapper().readValue(" + srcNames[i] +"_json, HashMap.class);" + System.getProperty("line.separator") +"\t";; - result += srcNames[i] +" = new AbstractMap.SimpleEntry<>(i.entrySet().iterator.next.getKey(), " - + geterateParameterizedTypeStatements("i","i",(Type)ptype.typeArguments().get(1))+")"+ System.getProperty("line.separator"); + result += ptype.toString() + " i = new ObjectMapper().readValue(" + srcNames[i] + + "_json, HashMap.class);" + System.getProperty("line.separator") + "\t"; + ; + result += srcNames[i] + " = new AbstractMap.SimpleEntry<>(i.entrySet().iterator.next.getKey(), " + + geterateParameterizedTypeStatements("i", "i", (Type) ptype.typeArguments().get(1)) + ")" + + System.getProperty("line.separator"); } } - - result += "}"+ System.getProperty("line.separator"); - + + result += "}" + System.getProperty("line.separator"); + } - + { var returnStatement = new ReturnStatement[1]; - searchMethodDeclaration(descriptor.getDistinationClass(),"Getter").accept(new ASTVisitor() { + searchMethodDeclaration(descriptor.getDistinationClass(), "Getter").accept(new ASTVisitor() { @Override public boolean visit(ReturnStatement node) { returnStatement[0] = node; return super.visit(node); } - });; - result += "this.value = "+returnStatement[0].getExpression()+";" + System.getProperty("line.separator"); + }); + ; + result += "this.value = " + returnStatement[0].getExpression() + ";" + System.getProperty("line.separator"); } result += "}" + System.getProperty("line.separator"); /* - @PUT - public void updateHandling(@FormParam("handling") String handling_json) throws JsonProcessingException { - Map.Entry> handling; - { - Map> i = new ObjectMapper().readValue(handling_json, HashMap.class); - handling = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), new AbstractMap.SimpleEntry<>(Integer.parseInt(i.entrySet().iterator().next().getValue().entrySet().iterator().next().getKey()), i.entrySet().iterator().next().getValue().entrySet().iterator().next().getValue())); - } - this.value = new AbstractMap.SimpleEntry<>(handling.getKey(), handling.getValue().getKey()); - } + * @PUT public void updateHandling(@FormParam("handling") String handling_json) + * throws JsonProcessingException { Map.Entry> handling; { Map> i = new + * ObjectMapper().readValue(handling_json, HashMap.class); handling = new + * AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), new + * AbstractMap.SimpleEntry<>(Integer.parseInt(i.entrySet().iterator().next(). + * getValue().entrySet().iterator().next().getKey()), + * i.entrySet().iterator().next().getValue().entrySet().iterator().next(). + * getValue())); } this.value = new AbstractMap.SimpleEntry<>(handling.getKey(), + * handling.getValue().getKey()); } */ return result; } @@ -713,6 +690,19 @@ dstUnitRewriteList.insertBefore(statement, nextNode, dstEditGroup); } + private void addStatementLast(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode, + ChildListPropertyDescriptor childListPropertyDescriptor) { + ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, childListPropertyDescriptor); + PackageDeclaration dstPackageDeclaration = cu.getPackage(); + + Statement statement = (Statement) astRewrite.createStringPlaceholder(statementCode, + ASTNode.VARIABLE_DECLARATION_STATEMENT); + + astRewrite.set(cu, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null); + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + dstUnitRewriteList.insertLast(statement, dstEditGroup); + } + private void deleteASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode) { TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); astRewrite.remove(astNode, dstEditGroup);