diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 55e843a..ab929fb 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -35,11 +35,13 @@ 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; @@ -58,6 +60,7 @@ 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.ListRewrite; import org.eclipse.jdt.core.search.IJavaSearchConstants; @@ -157,7 +160,7 @@ var srcUnit = descriptor.getSourceClass(); var dstUnit = descriptor.getDistinationClass(); var dstDec = new TypeDeclaration[1]; - var messages = searchMethodDeclarations(srcUnit, "Message" ); + var messages = searchMethodDeclarations(srcUnit, "Message"); var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName())); descriptor.getDistinationClass().accept(new ASTVisitor() { @Override @@ -183,7 +186,7 @@ // @Getter���t�^���ꂽ���\�b�h�̕Ԃ�l�����̏�Ōv�Z����悤�ɕύX var srcState = searchFieldDeclaration(srcUnit, "State"); Type srcType = srcState.getType(); - var getterStatements = generateGetStatement(srcValue, srcValue , srcType); + var getterStatements = generateGetStatement(srcValue, srcValue, srcType); var transition = new Expression[1]; md.accept(new ASTVisitor() { @Override @@ -203,14 +206,13 @@ return true; } }); - - addStatement(dstUnitRewrite, dstUnit, getter.getBody() ,getterStatements, Block.STATEMENTS_PROPERTY); + + addStatement(dstUnitRewrite, dstUnit, getter.getBody(), getterStatements, Block.STATEMENTS_PROPERTY); replaceASTNode(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); - - //�]�����̕ύX - deletePutMethodInvocation(srcUnit); - + // �]�����̕ύX + deletePutMethodInvocation(srcUnit, srcValue); + // �V�K�t�@�C���쐬 try { IPath path = dstUnit.getJavaElement().getPath(); @@ -226,9 +228,103 @@ e.printStackTrace(); } } - private void deletePutMethodInvocation(ASTNode srcNode) { - + + private void deletePutMethodInvocation(ASTNode srcNode, String resourceName) { + var updateMethods = searchMethodDeclarations((CompilationUnit) srcNode, "Message"); + for (MethodDeclaration md : updateMethods) { + List formStatements = new ArrayList<>(); + List entityStatements = new ArrayList<>(); + List resultStatements = new ArrayList<>(); + + List> invocations = new ArrayList<>(); + 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")) { + stopRegister(); + } + } + return super.visit(node); + } + + @Override + public boolean visit(Assignment node) { + if (node.getLeftHandSide().toString().equals("form")) { + startRegister(); + } + if (node.getLeftHandSide().toString().equals("result")) { + + stopRegister(); + } + return super.visit(node); + } + + private void startRegister() { + isRegistering[0] = true; + statements.clear(); + } + private void stopRegister() { + statements.add(statement); + invocations.add(statements); + isRegistering[0] = false; + } + }); + if(isRegistering[0]) { + statements.add(statement); + } + /* +// statement.accept(new ASTVisitor() { +// @Override +// public boolean visit(VariableDeclarationStatement node) { +// if (node.getType().toString().equals("Form")) { +// formStatements.add(node); +// } +// +// if (node.getType() +// .toString().equals("Entity
")) { +// entityStatements.add(node); +// } +// if (node.getType().toString().equals("String")) { +// if (((VariableDeclarationFragment) node.fragments().get(0)).getName().toString() +// .equals("result")) +// resultStatements.add(node); +// } +// +// return super.visit(node); +// } +// +// @Override +// public boolean visit(Assignment node) { +// +// if (node.getLeftHandSide().toString().equals("form")) { +// formStatements.add((Statement) node.getParent().getParent()); +// } +// if (node.getLeftHandSide().toString().equals("entity")) { +// entityStatements.add((Statement) node.getParent().getParent()); +// } +// if (node.getLeftHandSide().toString().equals("result")) { +// resultStatements.add((Statement) node.getParent().getParent()); +// } +// +// return super.visit(node); +// } +// }); */ + } + System.out.println(invocations); + } + } + private MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { List result = new ArrayList(); cu.accept(new ASTVisitor() { @@ -242,31 +338,34 @@ }); return result.toArray(new MethodDeclaration[] {}); } + private String generateGetStatement(String assignedValue, String path, Type type) { String result = ""; String tmp = assignedValue; - String simpleType=type.toString(); + String simpleType = type.toString(); if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; if (ptype.getType().toString().equals("List")) { - result += "List<"+ptype.typeArguments().get(0)+"> "+assignedValue+" = new ArrayList<>();"+System.getProperty("line.separator"); - tmp=assignedValue+"_json"; - result += "List<"+convertType((Type)ptype.typeArguments().get(0))+"> "; - simpleType="ArrayList"; + result += "List<" + ptype.typeArguments().get(0) + "> " + assignedValue + " = new ArrayList<>();" + + System.getProperty("line.separator"); + tmp = assignedValue + "_json"; + result += "List<" + convertType((Type) ptype.typeArguments().get(0)) + "> "; + simpleType = "ArrayList"; } } - result += tmp + " = client.target(\"http://localhost:8080\").path(\"/"+ path + "\").request().get(" + result += tmp + " = client.target(\"http://localhost:8080\").path(\"/" + path + "\").request().get(" + simpleType + ".class);" + System.getProperty("line.separator"); - + result += geterateParameterizedTypeStatements(assignedValue, tmp, type); return result; } + private String convertType(Type type) { - + if ((type instanceof ParameterizedType - &&((ParameterizedType)type).getType().toString().equals("Map.Entry"))) { - - return "Map"; + && ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) { + + return "Map"; } return type.toString(); } @@ -275,21 +374,22 @@ 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") + "}" ; + 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")) { - //parseInt���܂��ł��ĂȂ� - String str = "new AbstractMap.SimpleEntry<>(" - +ptype.typeArguments().get(0)+".parseInt("+tmpValue+".entrySet().iterator().next().getKey()), " - +tmpValue+".entrySet().iterator().next().getValue())"; + // parseInt���܂��ł��ĂȂ� + String str = "new AbstractMap.SimpleEntry<>(" + ptype.typeArguments().get(0) + ".parseInt(" + tmpValue + + ".entrySet().iterator().next().getKey()), " + tmpValue + + ".entrySet().iterator().next().getValue())"; return str; } for (var arg : ((ParameterizedType) type).typeArguments()) { - + } } return ""; @@ -322,7 +422,9 @@ 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) { + + private void addStatement(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode, + ChildListPropertyDescriptor childListPropertyDescriptor) { ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, childListPropertyDescriptor); PackageDeclaration dstPackageDeclaration = cu.getPackage(); @@ -333,6 +435,7 @@ TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); dstUnitRewriteList.insertFirst(statement, dstEditGroup); } + private void deleteASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode) { TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); astRewrite.remove(astNode, dstEditGroup); @@ -474,18 +577,19 @@ // annotation���l��1�‚̃A�m�e�[�V�����������ꍇ if (annotation instanceof SingleMemberAnnotation) { - if(values.length == 0)return true; + if (values.length == 0) + return true; SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; - Expression tmp =singleMemberAnnotation.getValue(); - - IType itype = (IType)tmp.resolveTypeBinding().getJavaElement(); + Expression tmp = singleMemberAnnotation.getValue(); + + IType itype = (IType) tmp.resolveTypeBinding().getJavaElement(); String typename = itype.getTypeQualifiedName(); - if(tmp instanceof ArrayInitializer) { - + if (tmp instanceof ArrayInitializer) { + } - //if(tmp instanceof )) { - - //} + // if(tmp instanceof )) { + + // } // �A�m�e�[�V��������擾�����l����擪�Ɩ����̃_�u���N�H�[�e�[�V�������폜���������� String value = singleMemberAnnotation.getValue().toString().substring(1, singleMemberAnnotation.getValue().toString().length() - 1);