diff --git a/src/org/ntlab/pushPullRefactoring/FieldFindVisitor.java b/src/org/ntlab/pushPullRefactoring/FieldFindVisitor.java deleted file mode 100644 index 076ab51..0000000 --- a/src/org/ntlab/pushPullRefactoring/FieldFindVisitor.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.ntlab.pushPullRefactoring; - -import org.eclipse.jdt.core.dom.*; - -public class FieldFindVisitor extends ASTVisitor{ - private String fieldName; - - private FieldDeclaration field; - @Override - public boolean visit(FieldDeclaration node) { - String name = node.getClass().getCanonicalName(); - if (name.equals(this.fieldName)) { - this.field = node; - } - return false; - } -} diff --git a/src/org/ntlab/pushPullRefactoring/MethodFindVisitor.java b/src/org/ntlab/pushPullRefactoring/MethodFindVisitor.java deleted file mode 100644 index bf6afa0..0000000 --- a/src/org/ntlab/pushPullRefactoring/MethodFindVisitor.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.ntlab.pushPullRefactoring; - -import org.eclipse.jdt.core.dom.ASTVisitor; -import org.eclipse.jdt.core.dom.MethodDeclaration; - -public class MethodFindVisitor extends ASTVisitor { - private String methodName; - - private MethodDeclaration method; - @Override - public boolean visit(MethodDeclaration node) { - String name = node.getName().getIdentifier(); - if (name.equals(this.methodName)) { - this.method = node; - } - return false; - } - public MethodDeclaration getFoundMethod() { - return this.method; - } -} diff --git a/src/org/ntlab/pushPullRefactoring/Pull2PushRewriter.java b/src/org/ntlab/pushPullRefactoring/Pull2PushRewriter.java new file mode 100644 index 0000000..8ea10b8 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/Pull2PushRewriter.java @@ -0,0 +1,237 @@ +package org.ntlab.pushPullRefactoring; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.stream.Collectors; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.Assignment; +import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.ParameterizedType; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.Type; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; + +public class Pull2PushRewriter extends SourceRewriter { + + public Pull2PushRewriter(PushPullDescriptor descriptor) { + super(descriptor); + // TODO Auto-generated constructor stub + } + + @Override + public void rewrite(IProgressMonitor pm) { + var srcUnit = descriptor.getSourceClass(); + var dstUnit = descriptor.getDistinationClass(); + var srcDec = new TypeDeclaration[1]; + var messages = searchMethodDeclarations(srcUnit, "Message"); + 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(\"direct\")" + + System.getProperty("line.separator") + "String " + dstValue + "= " + "\"/" + dstValue + "\";"; + addStatement(srcUnitRewrite, srcUnit, srcDec[0], pushrefCode); + + String importPushRef = "import pushPullRefactor.PushReference;" + System.getProperty("line.separator") + + "import pushPullRefactor.Pullable;" + System.getProperty("line.separator") + + System.getProperty("line.separator") ; + addStatement(srcUnitRewrite, srcUnit, srcDec[0],importPushRef,TypeDeclaration.MODIFIERS2_PROPERTY); + + // �]�����N���X��update���\�b�h�̍Ō�ɁC�]����N���X��update���\�b�h�� + //�t�B�[���h value �������Ƃ��ēn���ČĂяo���s��lj�����D + 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