diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 04fe38f..c7dab65 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -2,12 +2,14 @@ import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; +import java.util.stream.Collectors; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBuffer; @@ -214,7 +216,7 @@ addStatement(dstUnitRewrite, dstUnit, getter.getBody(), getterStatements, Block.STATEMENTS_PROPERTY); replaceASTNode(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); - + var srcDec = new TypeDeclaration[1]; descriptor.getSourceClass().accept(new ASTVisitor() { @Override @@ -227,19 +229,21 @@ // �]�����̕ύX deletePutMethodInvocation(srcUnitRewrite, srcUnit, dstValue); // @PushReference���t�^���ꂽ�t�B�[���h���폜 - deleteASTNode(srcUnitRewrite, srcUnit, searchFieldDeclaration(descriptor.getSourceClass(), "PushReference", dstValue)); + deleteASTNode(srcUnitRewrite, srcUnit, + searchFieldDeclaration(descriptor.getSourceClass(), "PushReference", dstValue)); System.out.println(); try { applyRewrite(srcUnit, srcUnitRewrite, pm); applyRewrite(dstUnit, dstUnitRewrite, pm); - + } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } + private void applyRewrite(CompilationUnit unit, ASTRewrite unitRewrite, IProgressMonitor pm) throws Exception { IPath path = unit.getJavaElement().getPath(); ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); @@ -271,9 +275,11 @@ 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(); + + MethodInvocation right = (MethodInvocation) ((VariableDeclarationFragment) node + .fragments().get(0)).getInitializer(); + String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) + .getExpression()).arguments().get(0).toString(); stopRegister(resourceName); } } @@ -287,7 +293,8 @@ } if (node.getLeftHandSide().toString().equals("result")) { MethodInvocation right = (MethodInvocation) node.getRightHandSide(); - String resourceName =((MethodInvocation) ((MethodInvocation) right.getExpression()).getExpression()).arguments().get(0).toString(); + String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) + .getExpression()).arguments().get(0).toString(); stopRegister(resourceName); } return super.visit(node); @@ -295,7 +302,7 @@ private void startRegister() { isRegistering[0] = true; - + statements.clear(); } @@ -309,31 +316,30 @@ statements.add(statement); } } - + System.out.println(invocations); - //���\�b�h�̌Ăяo���Q��2�ˆȏ�ł���, - //invocations����Ή����\�[�X�ւ̃��\�b�h�Ăяo���Q��, - //invocations�̐擪�ɂ��郁�\�b�h�Ăяo���Q����v���邩�𔻒� - Iterator>> invocationsIterator = invocations.entrySet().iterator(); - var g =invocations.get(resourceName); + // ���\�b�h�̌Ăяo���Q��2�ˆȏ�ł���, + // invocations����Ή����\�[�X�ւ̃��\�b�h�Ăяo���Q��, + // invocations�̐擪�ɂ��郁�\�b�h�Ăяo���Q����v���邩�𔻒� + Iterator>> invocationsIterator = invocations.entrySet().iterator(); + var g = invocations.get(resourceName); var i = (invocationsIterator.next()).getKey().equals(resourceName); - if(invocations.size() > 1 - && i ){ - + if (invocations.size() > 1 && i) { + var entityStatement = ""; var formStatement = ""; var resultStatement = ""; - for(Statement st : (List)invocationsIterator.next().getValue()) { + for (Statement st : (List) invocationsIterator.next().getValue()) { ASTNode[] n = new ASTNode[1]; - st.accept(new ASTVisitor () { + st.accept(new ASTVisitor() { @Override public boolean visit(Assignment node) { if (node.getLeftHandSide().toString().equals("form")) { - n[0] = node; + n[0] = node; addDecStatement(node, "Form "); } if (node.getLeftHandSide().toString().equals("entity")) { - n[0] = node; + n[0] = node; addDecStatement(node, "Entity
"); } if (node.getLeftHandSide().toString().equals("result")) { @@ -343,24 +349,26 @@ } return super.visit(node); } + public void addDecStatement(Assignment node, String typeName) { - // addStatement(astRewrite, srcUnit, node, typeName, Assignment ); - addStatementBefore(astRewrite, srcUnit, md.getBody(), node.getParent(), typeName, Block.STATEMENTS_PROPERTY ); + // addStatement(astRewrite, srcUnit, node, typeName, Assignment ); + addStatementBefore(astRewrite, srcUnit, md.getBody(), node.getParent(), typeName, + Block.STATEMENTS_PROPERTY); } }); - + } } - for(Statement st : invocations.get(resourceName)) { - deleteASTNode(astRewrite, srcUnit ,st ); + for (Statement st : invocations.get(resourceName)) { + deleteASTNode(astRewrite, srcUnit, st); } - + } } - public MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { + public MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { List result = new ArrayList(); cu.accept(new ASTVisitor() { @Override @@ -375,54 +383,51 @@ } private String generateGetStatement(String assignedValue, String path, Type type) { - //�Ԃ��{�f�B�S�̂̃\�[�X�R�[�h + // �Ԃ��{�f�B�S�̂̃\�[�X�R�[�h String result = ""; - //�]�����̒l��������ϐ��� + // �]�����̒l��������ϐ��� String tmp = assignedValue; - //GET���\�b�h�̈����œn���^�� + // GET���\�b�h�̈����œn���^�� String simpleType = type.toString(); if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; if (ptype.getType().toString().equals("List")) { simpleType = "ArrayList"; tmp = assignedValue + "_json"; - + result += "List<" + convertType((Type) ptype.typeArguments().get(0)) + "> "; result += tmp + " = client.target(\"http://localhost:8080\").path(\"/" + path + "\").request().get(" + simpleType + ".class);" + System.getProperty("line.separator"); - + result += "List<" + ptype.typeArguments().get(0) + "> " + assignedValue + " = new ArrayList<>();" + System.getProperty("line.separator"); - - + result += geterateParameterizedTypeStatements(assignedValue, tmp, type); } if (ptype.getType().toString().equals("Map.Entry")) { simpleType = "HashMap"; tmp = assignedValue + "_json"; - result += convertType((Type) ptype) +" " ; + result += convertType((Type) ptype) + " "; result += tmp + " = client.target(\"http://localhost:8080\").path(\"/" + path + "\").request().get(" + simpleType + ".class);" + System.getProperty("line.separator"); - - result += (Type) ptype + " " + assignedValue - + " = new AbstractMap.SimpleEntry<>(" - + tmp +".entrySet().iterator().next().getKey(), " - + geterateParameterizedTypeStatements(assignedValue , tmp+ ".entrySet().iterator().next().getValue()",(Type) ptype.typeArguments().get(1)) - +");" + System.getProperty("line.separator"); - - //result += convertType((Type) ptype.typeArguments().get(0)); - - + + result += (Type) ptype + " " + assignedValue + " = new AbstractMap.SimpleEntry<>(" + tmp + + ".entrySet().iterator().next().getKey(), " + + geterateParameterizedTypeStatements(assignedValue, + tmp + ".entrySet().iterator().next().getValue()", (Type) ptype.typeArguments().get(1)) + + ");" + System.getProperty("line.separator"); + + // result += convertType((Type) ptype.typeArguments().get(0)); + } } - - return result; } /** * Map.Entry��Map�ɕϊ����郁�\�b�h + * * @param type * @return */ @@ -431,7 +436,7 @@ if ((type instanceof ParameterizedType && ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) { - return "Map"; + return "Map"; } return type.toString(); } @@ -448,14 +453,15 @@ return str; } if (ptype.getType().toString().equals("Map.Entry")) { - String parsedKey =""; - if(ptype.typeArguments().get(0).toString().equals("Integer")) { - parsedKey += ptype.typeArguments().get(0) + ".parseInt(" + tmpValue + ".entrySet().iterator().next().getKey())"; - }else { - parsedKey += tmpValue + ".entrySet().iterator().next().getKey()"; + String parsedKey = ""; + if (ptype.typeArguments().get(0).toString().equals("Integer")) { + parsedKey += ptype.typeArguments().get(0) + ".parseInt(" + tmpValue + + ".entrySet().iterator().next().getKey())"; + } else { + parsedKey += tmpValue + ".entrySet().iterator().next().getKey()"; } - - String str = "new AbstractMap.SimpleEntry<>(" + parsedKey +", " + tmpValue + + String str = "new AbstractMap.SimpleEntry<>(" + parsedKey + ", " + tmpValue + ".entrySet().iterator().next().getValue())"; return str; } @@ -467,7 +473,127 @@ } private void changePull2Push(IProgressMonitor pm) { + var srcUnit = descriptor.getSourceClass(); + var dstUnit = descriptor.getDistinationClass(); + var srcDec = new TypeDeclaration[1]; + var messages = searchMethodDeclarations(srcUnit, "Message"); + var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName())); + descriptor.getSourceClass().accept(new ASTVisitor() { + @Override + public boolean visit(TypeDeclaration node) { + srcDec[0] = node; + return false; + } + }); + ASTRewrite srcUnitRewrite = ASTRewrite.create(srcDec[0].getAST()); + String srcValue = annotationValueCase(descriptor.getSourceClassName()); + String dstValue = annotationValueCase(descriptor.getDistinationClassName()); + // @PushReference���t�^���ꂽ�t�B�[���h��lj� + String pushrefCode = "@PushReference(\"" + dstValue + "\")" + System.getProperty("line.separator") + "@Pullable" + + System.getProperty("line.separator") + "String " + dstValue + "= " + "\"/" + dstValue + "\";"; + addStatement(srcUnitRewrite, srcUnit, srcDec[0], pushrefCode); + + // Handling �N���X�� updateAvailable() ���\�b�h�� updateRequest() ���\�b�h�̍Ō�ɁCShipping �N���X�� + // updateHandling() ���\�b�h���t�B�[���h value �������Ƃ��ēn���ČĂяo���s��lj�����D + + var dstDec = new TypeDeclaration[1]; + descriptor.getDistinationClass().accept(new ASTVisitor() { + @Override + public boolean visit(TypeDeclaration node) { + dstDec[0] = node; + return false; + } + }); + ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); + + // Shipping �N���X�̃t�B�[���h handling ���폜����D + deleteASTNode(dstUnitRewrite, dstUnit, searchFieldDeclaration(dstUnit, "PullReference", srcValue)); + + // 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}); + 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(dstUnit, dstUnitRewrite, pm); + + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + 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 + "})" + + System.getProperty("line.separator") + "public void update" + "(" + args + + ") throws JsonProcessingException {" + System.getProperty("line.separator"); + + //�^�錾 + for (int i = 0 ; i < paramTypes.length ; i++) { + + + 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]; + 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 += "}"+ System.getProperty("line.separator"); + + } + + { + var returnStatement = new ReturnStatement[1]; + 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 += "}" + 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()); + } + */ + return result; } /** @@ -506,9 +632,9 @@ TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); dstUnitRewriteList.insertFirst(statement, dstEditGroup); } - - private void addStatementBefore(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode nextNode, String statementCode, - ChildListPropertyDescriptor childListPropertyDescriptor) { + + private void addStatementBefore(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode nextNode, + String statementCode, ChildListPropertyDescriptor childListPropertyDescriptor) { ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, childListPropertyDescriptor); PackageDeclaration dstPackageDeclaration = cu.getPackage();