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
"; + 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(" + 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() { + @Override + public boolean visit(TypeDeclaration node) { + dstDec[0] = node; + return false; + } + }); + ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); + + //�]����N���X��PullReference���t�^���ꂽ�t�B�[���h���폜����D + deleteASTNode(dstUnitRewrite, dstUnit, searchFieldDeclaration(dstUnit, "PullReference", srcValue)); + + // �]����N���X�ɏ�Ԃ�ێ�����t�B�[���hvalue ��lj�����D + Type dstType = searchMethodDeclaration(dstUnit, "Getter").getReturnType2(); + String stateCode = "@State" + System.getProperty("line.separator") + dstType.toString() + " value;"; + addStatement(dstUnitRewrite, dstUnit, dstDec[0], stateCode); + + // �]����N���X��update���\�b�h��lj�����D + // �]����N���X��get���\�b�h���ōs���Ă����C�߂�l���v�Z���鏈���� + // update���\�b�h�Ɉړ����C�X�V��̒l��value�ɕۑ�����悤�ɂ���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); + + //�ύX��K�p���� + try { + applyRewrite(srcUnit, srcUnitRewrite, pm); + applyRewrite(dstUnit, dstUnitRewrite, pm); + + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + /** + * PUT���\�b�h���쐬����. + * @param �]���惊�\�[�X�̌^ + * @param �]�������\�[�X�̌^ + * @param �]�����̃��\�[�X�� + * @return + */ + public String generatePutMethod(Type updatedValueType, Type[] paramTypes, String[] srcNames) { + String paramType = "String"; + + if (paramTypes[0] instanceof ParameterizedType) { + //�]�����̌^�����X�g�������ꍇ, PUT���\�b�h���󂯂Ƃ�p�����[�^�[�̌^��List�ɂ��� + var ptype = (ParameterizedType) paramTypes[0]; + if (ptype.getType().toString().equals("List")) { + paramType = "List"; + } + } + + var args = "@FormParam(\"" + srcNames[0] + "\") "+paramType+" " + 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++) { + String initializer = ""; + if (paramTypes[i] instanceof ParameterizedType) { + var ptype = (ParameterizedType) paramTypes[i]; + if (ptype.getType().toString().equals("List")) { + initializer = " = new ArrayList<>()"; + } + } + result += paramTypes[i].toString() + " " + srcNames[i] + initializer + ";" + 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")) { + var argType = (Type)ptype.typeArguments().get(0); + String initializer = ""; + if(argType instanceof ParameterizedType) { + if (((ParameterizedType)argType).getType().toString().equals("Map.Entry")) { + initializer = " new ObjectMapper().readValue(str, HashMap.class);"; + } + //((ParameterizedType)argType).getType(); + }else { + if(argType.toString().equals("Integer")) { + initializer = " Integer.parseInt(str);"; + } + } + result += "for(String str: "+srcNames[i]+"_json ){ " + System.getProperty("line.separator"); + result += convertType(argType) +" i =" + initializer + System.getProperty("line.separator"); + result += srcNames[i].toString()+".add("; + // String value, String tmpValue, Type type + result += geterateParameterizedTypeStatements("i", "i", argType); + result += ");" + System.getProperty("line.separator"); + result += "}" + System.getProperty("line.separator"); + } + if (ptype.getType().toString().equals("Map.Entry")) { + result += convertType(ptype) + " i = new ObjectMapper().readValue(" + srcNames[i] + + "_json, HashMap.class);" + System.getProperty("line.separator") + "\t"; + var mapValue = "i"; + var typeArg = ptype.typeArguments().get(1); + if(typeArg instanceof ParameterizedType) { + if(( (ParameterizedType) ptype.typeArguments().get(1)).getType().toString().equals("Map.Entry")){ + mapValue += ".entrySet().iterator().next().getValue()"; + } + } + + result += srcNames[i] + " = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), " + + geterateParameterizedTypeStatements("i", mapValue, (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"); + + return result; + } + + +} diff --git a/src/org/ntlab/pushPullRefactoring/Push2PullRewriter.java b/src/org/ntlab/pushPullRefactoring/Push2PullRewriter.java new file mode 100644 index 0000000..3f3dcc3 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/Push2PullRewriter.java @@ -0,0 +1,273 @@ +package org.ntlab.pushPullRefactoring; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +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.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; +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.rewrite.ASTRewrite; + +public class Push2PullRewriter extends SourceRewriter{ + + public Push2PullRewriter(PushPullDescriptor descriptor) { + super(descriptor); + } + // TODO Auto-generated constructor stub + @Override + public void rewrite(IProgressMonitor pm) { + // TODO Auto-generated method stub + //�]�����̃N���X��CompilationUnit + var srcUnit = descriptor.getSourceClass(); + //�]����̃N���X + var dstUnit = descriptor.getDistinationClass(); + //�]����N���X�̐錾�� + var dstDec = new TypeDeclaration[1]; + var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName())); + descriptor.getDistinationClass().accept(new ASTVisitor() { + @Override + public boolean visit(TypeDeclaration node) { + dstDec[0] = node; + return false; + } + }); + ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); + //ASTRewrite dstUnitRewrite = ASTRewrite.create(dstUnit.getAST()); + + String srcValue = annotationValueCase(descriptor.getSourceClassName()); + String dstValue = annotationValueCase(descriptor.getDistinationClassName()); + String pullrefCode = "@PullReference(\"" + srcValue + "\")" + System.getProperty("line.separator") + "@Pushable" + + System.getProperty("line.separator") + "String " + srcValue + "= " + "\"/" + srcValue + "\";"; + // @PullReference���t�^���ꂽ�t�B�[���h��lj� + addStatement(dstUnitRewrite, dstUnit, dstDec[0], pullrefCode); + + String importPullRef = "import pushPullRefactor.PullReference;" + System.getProperty("line.separator") + + "import pushPullRefactor.Pushable;" + System.getProperty("line.separator") + + System.getProperty("line.separator") ; + addStatement(dstUnitRewrite, dstUnit, dstDec[0],importPullRef,TypeDeclaration.MODIFIERS2_PROPERTY); + + if(!hasClient(dstUnit)) { + addStatement(dstUnitRewrite, dstUnit, dstDec[0],"import javax.ws.rs.client.*;" + System.getProperty("line.separator"),TypeDeclaration.MODIFIERS2_PROPERTY); + addStatement(dstUnitRewrite, dstUnit, dstDec[0], "private Client client = ClientBuilder.newClient();"+ System.getProperty("line.separator")); + } + + // @State���t�^���ꂽ�t�B�[���h���폜 + deleteASTNode(dstUnitRewrite, dstUnit, searchFieldDeclaration(descriptor.getDistinationClass(), "State")); + + // @Message���t�^���ꂽ���\�b�h���폜 +// deleteASTNode(dstUnitRewrite, dstUnit, +// searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()))); + deleteASTNode(dstUnitRewrite, dstUnit, + searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()))); + // @Getter���t�^���ꂽ���\�b�h�̕Ԃ�l�����̏�Ōv�Z����悤�ɕύX + var srcState = searchFieldDeclaration(srcUnit, "State"); + Type srcType = srcState.getType(); + var getterStatements = generateGetStatement(srcValue, srcValue, srcType); + var transition = new Expression[1]; + md.accept(new ASTVisitor() { + @Override + public boolean visit(Assignment node) { + var left = node.getLeftHandSide(); + if(left.toString().equals("this.value")); + transition[0] = node.getRightHandSide(); + return true; + } + }); + var getter = searchMethodDeclaration(dstUnit, "Getter"); + var returnStatement = new ReturnStatement[1]; + getter.accept(new ASTVisitor() { + @Override + public boolean visit(ReturnStatement node) { + returnStatement[0] = node; + return true; + } + }); + + 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 + public boolean visit(TypeDeclaration node) { + srcDec[0] = node; + return false; + } + }); + ASTRewrite srcUnitRewrite = ASTRewrite.create(srcDec[0].getAST()); + // �]�����̕ύX + deletePutMethodInvocation(srcUnitRewrite, srcUnit, dstValue); + // @PushReference���t�^���ꂽ�t�B�[���h���폜 + deleteASTNode(srcUnitRewrite, srcUnit, + searchFieldDeclaration(descriptor.getSourceClass(), "PushReference", dstValue)); + + System.out.println(dstUnitRewrite.toString()); + try { + applyRewrite(srcUnit, srcUnitRewrite, pm); + applyRewrite(dstUnit, dstUnitRewrite, pm); + + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + protected void deletePutMethodInvocation(ASTRewrite astRewrite, CompilationUnit srcUnit, String resourceName) { + var updateMethods = searchMethodDeclarations(srcUnit, "Message"); + for (MethodDeclaration md : updateMethods) { + LinkedHashMap> invocations = getPutMethodInvocations(md); + 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); + var i = (invocationsIterator.next()).getKey().equals(resourceName); + if (invocations.size() > 1 && i) { + + for (Statement st : (List) invocationsIterator.next().getValue()) { + ASTNode[] n = new ASTNode[1]; + st.accept(new ASTVisitor() { + @Override + public boolean visit(Assignment node) { + if (node.getLeftHandSide().toString().equals("form")) { + n[0] = node; + addDecStatement(node, "Form "); + } + if (node.getLeftHandSide().toString().equals("entity")) { + n[0] = node; + addDecStatement(node, "Entity "); + } + if (node.getLeftHandSide().toString().equals("result")) { + MethodInvocation right = (MethodInvocation) node.getRightHandSide(); + n[0] = node; + addDecStatement(node, "String "); + } + 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); + } + }); + + } + } + + for (Statement st : invocations.get(resourceName)) { + deleteASTNode(astRewrite, srcUnit, st); + } + + } + + } + public String generatePutMethod(Type updatedValueType, Type[] paramTypes, String[] srcNames) { + String paramType = "String"; + + if (paramTypes[0] instanceof ParameterizedType) { + //�]�����̌^�����X�g�������ꍇ, PUT���\�b�h���󂯂Ƃ�p�����[�^�[�̌^��List�ɂ��� + var ptype = (ParameterizedType) paramTypes[0]; + if (ptype.getType().toString().equals("List")) { + paramType = "List"; + } + } + + var args = "@FormParam(\"" + srcNames[0] + "\") "+paramType+" " + 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++) { + String initializer = ""; + if (paramTypes[i] instanceof ParameterizedType) { + var ptype = (ParameterizedType) paramTypes[i]; + if (ptype.getType().toString().equals("List")) { + initializer = " = new ArrayList<>()"; + } + } + result += paramTypes[i].toString() + " " + srcNames[i] + initializer + ";" + 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")) { + var argType = (Type)ptype.typeArguments().get(0); + String initializer = ""; + if(argType instanceof ParameterizedType) { + if (((ParameterizedType)argType).getType().toString().equals("Map.Entry")) { + initializer = " new ObjectMapper().readValue(str, HashMap.class);"; + } + //((ParameterizedType)argType).getType(); + }else { + if(argType.toString().equals("Integer")) { + initializer = " Integer.parseInt(str);"; + } + } + result += "for(String str: "+srcNames[i]+"_json ){ " + System.getProperty("line.separator"); + result += convertType(argType) +" i =" + initializer + System.getProperty("line.separator"); + result += srcNames[i].toString()+".add("; + // String value, String tmpValue, Type type + result += geterateParameterizedTypeStatements("i", "i", argType); + result += ");" + System.getProperty("line.separator"); + result += "}" + System.getProperty("line.separator"); + } + if (ptype.getType().toString().equals("Map.Entry")) { + result += convertType(ptype) + " i = new ObjectMapper().readValue(" + srcNames[i] + + "_json, HashMap.class);" + System.getProperty("line.separator") + "\t"; + var mapValue = "i"; + var typeArg = ptype.typeArguments().get(1); + if(typeArg instanceof ParameterizedType) { + if(( (ParameterizedType) ptype.typeArguments().get(1)).getType().toString().equals("Map.Entry")){ + mapValue += ".entrySet().iterator().next().getValue()"; + } + } + + result += srcNames[i] + " = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), " + + geterateParameterizedTypeStatements("i", mapValue, (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"); + + return result; + } + +} diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 14a2c76..6c60978 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -1,115 +1,16 @@ package org.ntlab.pushPullRefactoring; -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; -import org.eclipse.core.filebuffers.ITextFileBufferManager; -import org.eclipse.core.filebuffers.LocationKind; -import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IResourceVisitor; -import org.eclipse.core.resources.IWorkspace; -import org.eclipse.core.resources.IWorkspaceRoot; -import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; -import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IPackageFragment; -import org.eclipse.jdt.core.IPackageFragmentRoot; -import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.dom.AST; -import org.eclipse.jdt.core.dom.ASTNode; -import org.eclipse.jdt.core.dom.ASTParser; -import org.eclipse.jdt.core.dom.ASTVisitor; -import org.eclipse.jdt.core.dom.Annotation; -import org.eclipse.jdt.core.dom.ArrayInitializer; -import org.eclipse.jdt.core.dom.Assignment; -import org.eclipse.jdt.core.dom.Block; -import org.eclipse.jdt.core.dom.BodyDeclaration; -import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; -import org.eclipse.jdt.core.dom.CompilationUnit; -import org.eclipse.jdt.core.dom.Expression; -import org.eclipse.jdt.core.dom.ExpressionStatement; -import org.eclipse.jdt.core.dom.FieldAccess; -import org.eclipse.jdt.core.dom.FieldDeclaration; -import org.eclipse.jdt.core.dom.InfixExpression; -import org.eclipse.jdt.core.dom.MarkerAnnotation; -import org.eclipse.jdt.core.dom.MethodDeclaration; -import org.eclipse.jdt.core.dom.MethodInvocation; -import org.eclipse.jdt.core.dom.Modifier; -import org.eclipse.jdt.core.dom.NormalAnnotation; -import org.eclipse.jdt.core.dom.PackageDeclaration; -import org.eclipse.jdt.core.dom.ParameterizedType; -import org.eclipse.jdt.core.dom.PrimitiveType; -import org.eclipse.jdt.core.dom.ReturnStatement; -import org.eclipse.jdt.core.dom.SingleMemberAnnotation; -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.VariableDeclaration; -import org.eclipse.jdt.core.dom.VariableDeclarationFragment; -import org.eclipse.jdt.core.dom.VariableDeclarationStatement; -import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; -import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; -import org.eclipse.jdt.core.dom.rewrite.ListRewrite; -import org.eclipse.jdt.core.search.IJavaSearchConstants; -import org.eclipse.jdt.core.search.IJavaSearchScope; -import org.eclipse.jdt.core.search.SearchEngine; -import org.eclipse.jdt.core.search.SearchMatch; -import org.eclipse.jdt.core.search.SearchParticipant; -import org.eclipse.jdt.core.search.SearchPattern; -import org.eclipse.jdt.core.search.SearchRequestor; -import org.eclipse.jdt.internal.core.SourceField; -import org.eclipse.jdt.internal.core.SourceType; -import org.eclipse.jdt.internal.core.builder.SourceFile; -import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange; -import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange; -import org.eclipse.jdt.ui.CodeStyleConfiguration; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.Document; -import org.eclipse.jface.text.IDocument; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; -import org.eclipse.text.edits.MalformedTreeException; -import org.eclipse.text.edits.TextEdit; -import org.eclipse.text.edits.TextEditGroup; -import org.eclipse.text.edits.UndoEdit; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.IWorkbench; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.editors.text.FileDocumentProvider; -import org.eclipse.ui.editors.text.TextEditor; -import org.eclipse.ui.ide.IDE; -import org.eclipse.ui.part.FileEditorInput; -import org.eclipse.ui.texteditor.AbstractTextEditor; -import org.eclipse.ui.texteditor.IDocumentProvider; -import org.eclipse.ui.texteditor.IDocumentProviderExtension; public class PushPullProcessor extends RefactoringProcessor { private PushPullDescriptor descriptor = null; @@ -165,878 +66,17 @@ ICompilationUnit[] icus = new ICompilationUnit[1]; if (descriptor.isPushToPull()) { - changePush2Pull(pm); + Push2PullRewriter rewriter = new Push2PullRewriter(descriptor); + rewriter.rewrite(pm); + //changePush2Pull(pm); } else { - changePull2Push(pm); + Pull2PushRewriter rewriter = new Pull2PushRewriter(descriptor); + rewriter.rewrite(pm); + //changePull2Push(pm); } Measurement.endTime(); return result; } - - private void changePush2Pull(IProgressMonitor pm) { - var srcUnit = descriptor.getSourceClass(); - var dstUnit = descriptor.getDistinationClass(); - var dstDec = new TypeDeclaration[1]; - var messages = searchMethodDeclarations(srcUnit, "Message"); - var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName())); - descriptor.getDistinationClass().accept(new ASTVisitor() { - @Override - public boolean visit(TypeDeclaration node) { - dstDec[0] = node; - return false; - } - }); - ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); - //ASTRewrite dstUnitRewrite = ASTRewrite.create(dstUnit.getAST()); - - String srcValue = annotationValueCase(descriptor.getSourceClassName()); - String dstValue = annotationValueCase(descriptor.getDistinationClassName()); - String pullrefCode = "@PullReference(\"" + srcValue + "\")" + System.getProperty("line.separator") + "@Pushable" - + System.getProperty("line.separator") + "String " + srcValue + "= " + "\"/" + srcValue + "\";"; - // @PullReference���t�^���ꂽ�t�B�[���h��lj� - addStatement(dstUnitRewrite, dstUnit, dstDec[0], pullrefCode); - - String importPullRef = "import pushPullRefactor.PullReference;" + System.getProperty("line.separator") - + "import pushPullRefactor.Pushable;" + System.getProperty("line.separator") - + System.getProperty("line.separator") ; - addStatement(dstUnitRewrite, dstUnit, dstDec[0],importPullRef,TypeDeclaration.MODIFIERS2_PROPERTY); - - if(!hasClient(dstUnit)) { - addStatement(dstUnitRewrite, dstUnit, dstDec[0],"import javax.ws.rs.client.*;" + System.getProperty("line.separator"),TypeDeclaration.MODIFIERS2_PROPERTY); - addStatement(dstUnitRewrite, dstUnit, dstDec[0], "private Client client = ClientBuilder.newClient();"+ System.getProperty("line.separator")); - } - - // @State���t�^���ꂽ�t�B�[���h���폜 - deleteASTNode(dstUnitRewrite, dstUnit, searchFieldDeclaration(descriptor.getDistinationClass(), "State")); - - // @Message���t�^���ꂽ���\�b�h���폜 -// deleteASTNode(dstUnitRewrite, dstUnit, -// searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()))); - deleteASTNode(dstUnitRewrite, dstUnit, - searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()))); - // @Getter���t�^���ꂽ���\�b�h�̕Ԃ�l�����̏�Ōv�Z����悤�ɕύX - var srcState = searchFieldDeclaration(srcUnit, "State"); - Type srcType = srcState.getType(); - var getterStatements = generateGetStatement(srcValue, srcValue, srcType); - var transition = new Expression[1]; - md.accept(new ASTVisitor() { -// @Override -// public boolean visit(InfixExpression node) { -// var left = node.getLeftOperand(); -// -// transition[0] = node; -// return true; -// } - @Override - public boolean visit(Assignment node) { - var left = node.getLeftHandSide(); - if(left.toString().equals("this.value")); - transition[0] = node.getRightHandSide(); - return true; - } - }); - var getter = searchMethodDeclaration(dstUnit, "Getter"); - var returnStatement = new ReturnStatement[1]; - getter.accept(new ASTVisitor() { - @Override - public boolean visit(ReturnStatement node) { - returnStatement[0] = node; - return true; - } - }); - - 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 - public boolean visit(TypeDeclaration node) { - srcDec[0] = node; - return false; - } - }); - ASTRewrite srcUnitRewrite = ASTRewrite.create(srcDec[0].getAST()); - // �]�����̕ύX - deletePutMethodInvocation(srcUnitRewrite, srcUnit, dstValue); - // @PushReference���t�^���ꂽ�t�B�[���h���폜 - deleteASTNode(srcUnitRewrite, srcUnit, - searchFieldDeclaration(descriptor.getSourceClass(), "PushReference", dstValue)); - - System.out.println(dstUnitRewrite.toString()); - try { - applyRewrite(srcUnit, srcUnitRewrite, pm); - applyRewrite(dstUnit, dstUnitRewrite, pm); - - } catch (Exception e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - } - private void addImport(CompilationUnit cu, String importString, IProgressMonitor pm) throws Exception { - ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(cu, true); - AST ast = cu.getAST(); - Type rtype = ast.newSimpleType(ast.newName(importRewrite.addImport(importString))); - TextEdit importEdit = importRewrite.rewriteImports(null); - IPath path = cu.getJavaElement().getPath(); - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, LocationKind.IFILE, pm); - ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); - IDocument document = textFileBuffer.getDocument(); - importEdit.apply(document); - } - private boolean hasClient(ASTNode astNode) { - boolean[] result = {false} ; - astNode.accept(new ASTVisitor() { - public boolean visit(FieldDeclaration node) { - if(node.getType().toString().equals("Client")) { - - result[0] = true; - } - return false; - } - }); - return result[0]; - } - private void applyRewrite(CompilationUnit unit, ASTRewrite unitRewrite, IProgressMonitor pm) throws Exception { - IPath path = unit.getJavaElement().getPath(); - IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); - IFile file = workspaceRoot.getFile(path); -// if(isOpened(file)) { -// IWorkbench workbench = PlatformUI.getWorkbench(); -// IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); -// IWorkbenchPage page = window.getActivePage(); -// IEditorPart editor = IDE.openEditor(page, file); -// } - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, LocationKind.IFILE, pm); - ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); - IDocument document = textFileBuffer.getDocument(); - TextEdit edit = unitRewrite.rewriteAST(document, null); - UndoEdit undo = edit.apply(document); - textFileBuffer.commit(null /* ProgressMonitor */, true/* Overwrite */); - - } - - private boolean isOpened(IFile targetFile) { - IEditorInput input = new FileEditorInput(targetFile); - IWorkbench workbench = PlatformUI.getWorkbench(); - IWorkbenchWindow[] windows = workbench.getWorkbenchWindows(); - for (int i = 0; i < windows.length; i++) { - IWorkbenchPage[] pages = windows[i].getPages(); - for (int j = 0; j < pages.length; j++) { - IEditorPart editor = pages[j].findEditor(input); - if (editor != null) { - // �G�f�B�^�ŊJ����Ă��� - return true; - } - } - } - // �G�f�B�^�ŊJ����Ă��Ȃ� - return false; - } - - private void deletePutMethodInvocation(ASTRewrite astRewrite, CompilationUnit srcUnit, String resourceName) { - var updateMethods = searchMethodDeclarations(srcUnit, "Message"); - for (MethodDeclaration md : updateMethods) { - LinkedHashMap> invocations = getPutMethodInvocations(md); - 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); - var i = (invocationsIterator.next()).getKey().equals(resourceName); - if (invocations.size() > 1 && i) { - - for (Statement st : (List) invocationsIterator.next().getValue()) { - ASTNode[] n = new ASTNode[1]; - st.accept(new ASTVisitor() { - @Override - public boolean visit(Assignment node) { - if (node.getLeftHandSide().toString().equals("form")) { - n[0] = node; - addDecStatement(node, "Form "); - } - if (node.getLeftHandSide().toString().equals("entity")) { - n[0] = node; - addDecStatement(node, "Entity "); - } - if (node.getLeftHandSide().toString().equals("result")) { - MethodInvocation right = (MethodInvocation) node.getRightHandSide(); - n[0] = node; - addDecStatement(node, "String "); - } - 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); - } - }); - - } - } - - for (Statement st : invocations.get(resourceName)) { - deleteASTNode(astRewrite, srcUnit, st); - } - - } - - } - - /** - * �����Ƃ��ēn���ꂽMethodDeclaration�̃��\�b�h�{�f�B���ɂ���PUT���\�b�h�̌Ăяo�����擾���郁�\�b�h - * - * @param md - * @return - */ - private LinkedHashMap> getPutMethodInvocations(MethodDeclaration md) { - 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); - } - } - return invocations; - } - - public MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { - List result = new ArrayList(); - cu.accept(new ASTVisitor() { - @Override - public boolean visit(MethodDeclaration node) { - if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { - result.add(node); - } - return super.visit(node); - } - }); - return result.toArray(new MethodDeclaration[] {}); - } - - private String generateGetStatement(String assignedValue, String path, Type type) { - // �Ԃ��{�f�B�S�̂̃\�[�X�R�[�h - String result = ""; - // �]�����̒l��������ϐ��� - String tmp = assignedValue; - // 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(this."+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 += tmp + " = client.target(\"http://localhost:8080\").path(this."+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)); - - } - } - - return result; - } - - /** - * Map.Entry��Map�ɕϊ����郁�\�b�h - * - * @param type - * @return - */ - private String convertType(Type type) { - - if ((type instanceof ParameterizedType - && ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) { - - return "Map"; - } - return type.toString(); - } - - private String geterateParameterizedTypeStatements(String value, String tmpValue, Type type) { - if (type instanceof ParameterizedType) { - ParameterizedType ptype = (ParameterizedType) type; - if (ptype.getType().toString().equals("List")) { - String str = "for (" + convertType((Type) ptype.typeArguments().get(0)) + " i: " + tmpValue + ") {" - + System.getProperty("line.separator"); - var argment = ((Type) ptype.typeArguments().get(0)); - str += value + ".add(" + geterateParameterizedTypeStatements(value, "i", argment) + ");"; - str += System.getProperty("line.separator") + "}"; - 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 str = "new AbstractMap.SimpleEntry<>(" + parsedKey + ", " + tmpValue - + ".entrySet().iterator().next().getValue())"; - return str; - } - for (var arg : ((ParameterizedType) type).typeArguments()) { - - } - } - return ""; - } - - 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(\"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); - - // Handling �N���X�� updateAvailable() ���\�b�h�� updateRequest() ���\�b�h�̍Ō�ɁCShipping �N���X�� - // updateHandling() ���\�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 "; - 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(" + 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() { - @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(); - - 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) { - String paramType = "String"; - - if (paramTypes[0] instanceof ParameterizedType) { - //�]�����̌^�����X�g�������ꍇ, PUT���\�b�h���󂯂Ƃ�p�����[�^�[�̌^��List�ɂ��� - var ptype = (ParameterizedType) paramTypes[0]; - if (ptype.getType().toString().equals("List")) { - paramType = "List"; - } - } - - var args = "@FormParam(\"" + srcNames[0] + "\") "+paramType+" " + 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++) { - String initializer = ""; - if (paramTypes[i] instanceof ParameterizedType) { - var ptype = (ParameterizedType) paramTypes[i]; - if (ptype.getType().toString().equals("List")) { - initializer = " = new ArrayList<>()"; - } - } - result += paramTypes[i].toString() + " " + srcNames[i] + initializer + ";" + 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")) { - var argType = (Type)ptype.typeArguments().get(0); - String initializer = ""; - if(argType instanceof ParameterizedType) { - if (((ParameterizedType)argType).getType().toString().equals("Map.Entry")) { - initializer = " new ObjectMapper().readValue(str, HashMap.class);"; - } - //((ParameterizedType)argType).getType(); - }else { - if(argType.toString().equals("Integer")) { - initializer = " Integer.parseInt(str);"; - } - } - result += "for(String str: "+srcNames[i]+"_json ){ " + System.getProperty("line.separator"); - result += convertType(argType) +" i =" + initializer + System.getProperty("line.separator"); - result += srcNames[i].toString()+".add("; - // String value, String tmpValue, Type type - result += geterateParameterizedTypeStatements("i", "i", argType); - result += ");" + System.getProperty("line.separator"); - result += "}" + System.getProperty("line.separator"); - } - if (ptype.getType().toString().equals("Map.Entry")) { - result += convertType(ptype) + " i = new ObjectMapper().readValue(" + srcNames[i] - + "_json, HashMap.class);" + System.getProperty("line.separator") + "\t"; - var mapValue = "i"; - var typeArg = ptype.typeArguments().get(1); - if(typeArg instanceof ParameterizedType) { - if(( (ParameterizedType) ptype.typeArguments().get(1)).getType().toString().equals("Map.Entry")){ - mapValue += ".entrySet().iterator().next().getValue()"; - } - } - - result += srcNames[i] + " = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), " - + geterateParameterizedTypeStatements("i", mapValue, (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"); - - return result; - } - - /** - * - * @param astRewrite - * @param cu - * @param astNode - * @param replasement - */ - private void replaceASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode replasement) { - TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); - astRewrite.replace(astNode, replasement, dstEditGroup); - } - - private void addStatement(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode) { - ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); - 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.insertFirst(statement, dstEditGroup); - } - - private void addStatement(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.insertFirst(statement, dstEditGroup); - } - - 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(); - - 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.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); - } - - /** - * ������̐�[�Ɩ����ɂ���_�u���N�H�[�e�[�V�������폜���郁�\�b�h - * - * @param str - * @return - */ - - private String annotationValueCase(String str) { - return Character.toLowerCase(str.charAt(0)) + (str.length() > 1 ? str.substring(1) : ""); - } - - private void changeGetter(IProgressMonitor pm) { - - CompilationUnit unit = descriptor.getDistinationClass(); - var getter = searchMethodDeclaration(unit, "Getter"); - ASTNode[] changed = new ASTNode[1]; - - getter.accept(new ASTVisitor() { - @Override - public boolean visit(MethodInvocation node) { - - changed[0] = node; - return false; - } - }); - ASTRewrite rewrite = ASTRewrite.create(changed[0].getAST()); - ListRewrite[] rewrites = new ListRewrite[1]; - FieldDeclaration fd = searchFieldDeclaration(unit, "State"); - Statement pushable = (Statement) rewrite.createStringPlaceholder(fd.fragments().get(0).toString(), - ASTNode.EMPTY_STATEMENT); - PackageDeclaration packageDeclaration = unit.getPackage(); - rewrite.set(unit, CompilationUnit.PACKAGE_PROPERTY, packageDeclaration, null); - TextEditGroup editGroup = new TextEditGroup("new class edit group"); - rewrites[0] = rewrite.getListRewrite(changed[0], MethodInvocation.TYPE_ARGUMENTS_PROPERTY); - rewrites[0].insertFirst(pushable, editGroup); - IPath path = unit.getJavaElement().getPath(); - // �V�K�t�@�C���쐬 - try { - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, LocationKind.IFILE, pm); - ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); - IDocument document = textFileBuffer.getDocument(); - TextEdit edit = rewrite.rewriteAST(document, null); - edit.apply(document); - textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); - IDocumentProvider provider; - } catch (Exception e) { - e.printStackTrace(); - } - } - - private MethodDeclaration createGetCtargetMethod(AST ast) { - - MethodDeclaration newDeclaration = ast.newMethodDeclaration(); - Block body = ast.newBlock(); - - // ���\�b�h����ݒ� - newDeclaration.setName(ast.newSimpleName("test")); - - // �߂�l�̌^��Ctarget�ɐݒ� - newDeclaration.setReturnType2(ast.newSimpleType(ast.newName("int"))); - - // �߂�l�̒l��ݒ� - ReturnStatement returnStatement = ast.newReturnStatement(); - FieldAccess access = ast.newFieldAccess(); - access.setName(ast.newSimpleName("result")); - access.setExpression(ast.newThisExpression()); - - returnStatement.setExpression(access); - - body.statements().add(returnStatement); - newDeclaration.setBody(body); - newDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); - return newDeclaration; - } - - /** - * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� - * - * @param cu �Ώۂ̃N���X�t�@�C�� - * @param annotationName �A�m�e�[�V������ - * @param value �A�m�e�[�V�����̒l - * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� - */ - private FieldDeclaration searchFieldDeclaration(CompilationUnit cu, String annotationName, String... values) { - FieldDeclaration[] result = new FieldDeclaration[1]; - cu.accept(new ASTVisitor() { - @Override - public boolean visit(FieldDeclaration node) { - if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { - result[0] = node; - } - return super.visit(node); - } - - }); - return result[0]; - } - - /** - * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� - * - * @param cu �Ώۂ̃N���X�t�@�C�� - * @param annotationName �A�m�e�[�V������ - * @param value �A�m�e�[�V�����̒l - * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� - */ - private MethodDeclaration searchMethodDeclaration(CompilationUnit cu, String annotationName, String... values) { - MethodDeclaration[] result = new MethodDeclaration[1]; - cu.accept(new ASTVisitor() { - @Override - public boolean visit(MethodDeclaration node) { - if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { - result[0] = node; - } - return super.visit(node); - } - }); - return result[0]; - } - - private boolean hasMatchAnnotation(ASTNode node, List modifiers, String annotationName, String... values) { - - for (var annotation : modifiers) { - // annotation���A�m�e�[�V��������Ȃ���,���̃��[�v�� - if (!(annotation instanceof Annotation)) - continue; - String annoName = ((Annotation) annotation).getTypeName().toString(); - // node�ɕt�^���ꂽ�A�m�e�[�V��������annotationName����v����ꍇ - if (annotationName.equals(annoName)) { - // annotation���l�̖����A�m�e�[�V�����������ꍇ - if (annotation instanceof MarkerAnnotation) { - return true; - } - - // annotation���l��1�‚̃A�m�e�[�V�����������ꍇ - if (annotation instanceof SingleMemberAnnotation) { - //�����Œl�̎w�肪���ɂ���Ă��Ȃ��Ƃ��̓A�m�e�[�V����������v�����i�K��true��Ԃ� - if (values.length == 0) - return true; - - SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; - Expression tmp = singleMemberAnnotation.getValue(); - - IType itype = (IType) tmp.resolveTypeBinding().getJavaElement(); - String typename = itype.getTypeQualifiedName(); - if (tmp instanceof ArrayInitializer) { - - String value = singleMemberAnnotation.getValue().toString().substring(1, - singleMemberAnnotation.getValue().toString().length() - 1); - String[] arr = value.split(","); - for(int i=0; i) listA) { - - } - return false; - } - - /** - * AST�𕶎���̃R�[�h�ɖ߂����\�b�h - * - * @param code ���̃R�[�h - * @param unit ASTVisitor�ő�����s�������c - * @return �\�[�X�R�[�h - */ - private static String getCode(String code, CompilationUnit unit) { - IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); - IFile file = workspaceRoot.getFile(unit.getTypeRoot().getPath()); - IPath path = file.getFullPath(); - ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); - ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE); - IDocument eDoc = buffer.getDocument(); - - TextEdit edit = unit.rewrite(eDoc, null); - try { - edit.apply(eDoc); - return eDoc.get(); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } - } - @Override public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException { diff --git a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java index 14957d4..6d0efdf 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java @@ -198,20 +198,7 @@ ASTNode node = parser.createAST(new NullProgressMonitor()); return node; } - private CompilationUnit getSourceCompilationUnit(ExecutionEvent event) throws JavaModelException { - - IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); - ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput); - ICompilationUnit sourceClass = (ICompilationUnit)typeRoot; - - ASTParser parser = ASTParser.newParser(AST.JLS8); - parser.setKind(ASTParser.K_COMPILATION_UNIT); - parser.setResolveBindings(true); - parser.setSource(sourceClass); - CompilationUnit result = (CompilationUnit) parser.createAST(null); - result.recordModifications(); - return result; - } + private CompilationUnit getCompilationUnitByEvent(ExecutionEvent event) throws JavaModelException { IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); diff --git a/src/org/ntlab/pushPullRefactoring/SourceRewriter.java b/src/org/ntlab/pushPullRefactoring/SourceRewriter.java new file mode 100644 index 0000000..df6a420 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/SourceRewriter.java @@ -0,0 +1,438 @@ +package org.ntlab.pushPullRefactoring; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.filebuffers.LocationKind; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.Assignment; +import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.FieldDeclaration; +import org.eclipse.jdt.core.dom.MarkerAnnotation; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.NormalAnnotation; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jdt.core.dom.ParameterizedType; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.SingleMemberAnnotation; +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; +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; +import org.eclipse.jface.text.IDocument; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.text.edits.TextEditGroup; +import org.eclipse.text.edits.UndoEdit; + +public abstract class SourceRewriter { + protected PushPullDescriptor descriptor = null; + public SourceRewriter(PushPullDescriptor descriptor){ + this.descriptor = descriptor; + } + abstract public void rewrite(IProgressMonitor pm) ; + + protected void applyRewrite(CompilationUnit unit, ASTRewrite unitRewrite, IProgressMonitor pm) throws Exception { + IPath path = unit.getJavaElement().getPath(); + ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); + bufferManager.connect(path, LocationKind.IFILE, pm); + ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); + IDocument document = textFileBuffer.getDocument(); + TextEdit edit = unitRewrite.rewriteAST(document, null); + UndoEdit undo = edit.apply(document); + textFileBuffer.commit(null /* ProgressMonitor */, true/* Overwrite */); + } + public MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { + List result = new ArrayList(); + cu.accept(new ASTVisitor() { + @Override + public boolean visit(MethodDeclaration node) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { + result.add(node); + } + return super.visit(node); + } + }); + return result.toArray(new MethodDeclaration[] {}); + } + + protected String generateGetStatement(String assignedValue, String path, Type type) { + // �Ԃ��{�f�B�S�̂̃\�[�X�R�[�h + String result = ""; + // �]�����̒l��������ϐ��� + String tmp = assignedValue; + // 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(this."+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 += tmp + " = client.target(\"http://localhost:8080\").path(this."+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)); + + } + } + + return result; + } + + + /** + * + * @param astRewrite + * @param cu + * @param astNode + * @param replasement + */ + protected void replaceASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode replasement) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.replace(astNode, replasement, dstEditGroup); + } + + protected void addStatement(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode) { + ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); + 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.insertFirst(statement, dstEditGroup); + } + /** + * + * @param astRewrite ���������Ώۂ�ASTRewrite + * @param cu ���������Ώۂ�CompilationUnit + * @param astNode cu���ɑ��݂���, ���������Ώۂ�AST + * @param statementCode astNode�ɒlj�����X�e�[�g�����g�� + * @param childListPropertyDescriptor astNode��ChildListPropertyDescriptor + */ + protected void addStatement(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.insertFirst(statement, dstEditGroup); + } + + protected void addStatementBefore(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode nextNode, + 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.insertBefore(statement, nextNode, dstEditGroup); + } + + protected 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); + } + + protected void deleteASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.remove(astNode, dstEditGroup); + } + protected boolean hasClient(ASTNode astNode) { + boolean[] result = {false} ; + astNode.accept(new ASTVisitor() { + public boolean visit(FieldDeclaration node) { + if(node.getType().toString().equals("Client")) { + + result[0] = true; + } + return false; + } + }); + return result[0]; + } + /** + * ������̐�[�Ɩ����ɂ���_�u���N�H�[�e�[�V�������폜���郁�\�b�h + * + * @param str + * @return + */ + + protected String annotationValueCase(String str) { + return Character.toLowerCase(str.charAt(0)) + (str.length() > 1 ? str.substring(1) : ""); + } + /** + * Map.Entry��Map�ɕϊ����郁�\�b�h + * + * @param type + * @return + */ + protected String convertType(Type type) { + + if ((type instanceof ParameterizedType + && ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) { + + return "Map"; + } + return type.toString(); + } + /** + * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� + * + * @param cu �Ώۂ̃N���X�t�@�C�� + * @param annotationName �A�m�e�[�V������ + * @param value �A�m�e�[�V�����̒l + * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� + */ + protected FieldDeclaration searchFieldDeclaration(CompilationUnit cu, String annotationName, String... values) { + FieldDeclaration[] result = new FieldDeclaration[1]; + cu.accept(new ASTVisitor() { + @Override + public boolean visit(FieldDeclaration node) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { + result[0] = node; + } + return super.visit(node); + } + + }); + return result[0]; + } + + /** + * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� + * + * @param cu �Ώۂ̃N���X�t�@�C�� + * @param annotationName �A�m�e�[�V������ + * @param value �A�m�e�[�V�����̒l + * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� + */ + protected MethodDeclaration searchMethodDeclaration(CompilationUnit cu, String annotationName, String... values) { + MethodDeclaration[] result = new MethodDeclaration[1]; + cu.accept(new ASTVisitor() { + @Override + public boolean visit(MethodDeclaration node) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { + result[0] = node; + } + return super.visit(node); + } + }); + return result[0]; + } + + protected String geterateParameterizedTypeStatements(String value, String tmpValue, Type type) { + if (type instanceof ParameterizedType) { + ParameterizedType ptype = (ParameterizedType) type; + if (ptype.getType().toString().equals("List")) { + String str = "for (" + convertType((Type) ptype.typeArguments().get(0)) + " i: " + tmpValue + ") {" + + System.getProperty("line.separator"); + var argment = ((Type) ptype.typeArguments().get(0)); + str += value + ".add(" + geterateParameterizedTypeStatements(value, "i", argment) + ");"; + str += System.getProperty("line.separator") + "}"; + 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 str = "new AbstractMap.SimpleEntry<>(" + parsedKey + ", " + tmpValue + + ".entrySet().iterator().next().getValue())"; + return str; + } + for (var arg : ((ParameterizedType) type).typeArguments()) { + + } + } + return ""; + } + /** + * �����Ƃ��ēn���ꂽMethodDeclaration�̃��\�b�h�{�f�B���ɂ���PUT���\�b�h�̌Ăяo�����擾���郁�\�b�h + * + * @param md + * @return + */ + protected LinkedHashMap> getPutMethodInvocations(MethodDeclaration md) { + 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); + } + } + return invocations; + } + protected boolean hasMatchAnnotation(ASTNode node, List modifiers, String annotationName, String... values) { + + for (var annotation : modifiers) { + // annotation���A�m�e�[�V��������Ȃ���,���̃��[�v�� + if (!(annotation instanceof Annotation)) + continue; + String annoName = ((Annotation) annotation).getTypeName().toString(); + // node�ɕt�^���ꂽ�A�m�e�[�V��������annotationName����v����ꍇ + if (annotationName.equals(annoName)) { + // annotation���l�̖����A�m�e�[�V�����������ꍇ + if (annotation instanceof MarkerAnnotation) { + return true; + } + + // annotation���l��1�‚̃A�m�e�[�V�����������ꍇ + if (annotation instanceof SingleMemberAnnotation) { + //�����Œl�̎w�肪���ɂ���Ă��Ȃ��Ƃ��̓A�m�e�[�V����������v�����i�K��true��Ԃ� + if (values.length == 0) + return true; + + SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; + Expression tmp = singleMemberAnnotation.getValue(); + + IType itype = (IType) tmp.resolveTypeBinding().getJavaElement(); + String typename = itype.getTypeQualifiedName(); + if (tmp instanceof ArrayInitializer) { + + String value = singleMemberAnnotation.getValue().toString().substring(1, + singleMemberAnnotation.getValue().toString().length() - 1); + String[] arr = value.split(","); + for(int i=0; i