diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index da463a5..c5366dd 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -2,6 +2,7 @@ import java.io.File; import java.util.List; +import java.util.Map; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBuffer; @@ -34,6 +35,7 @@ import org.eclipse.jdt.core.dom.Annotation; 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.FieldAccess; @@ -45,6 +47,7 @@ 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; @@ -153,7 +156,13 @@ var dstUnit = descriptor.getDistinationClass(); 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()); String srcValue = annotationValueCase(descriptor.getSourceClassName()); String pullrefCode = "@PullReference(\"" + srcValue + "\")" + System.getProperty("line.separator") + "@Pushable" @@ -169,6 +178,9 @@ 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 @@ -188,14 +200,8 @@ return true; } }); - descriptor.getDistinationClass().accept(new ASTVisitor() { - @Override - public boolean visit(TypeDeclaration node) { - dstDec[0] = node; - return false; - } - }); - // addStatement(dstUnitRewrite, dstUnit, dstDec[0],pullrefCode); + + addStatement(dstUnitRewrite, dstUnit, getter.getBody() ,getterStatements, Block.STATEMENTS_PROPERTY); replaceASTNode(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); // �V�K�t�@�C���쐬 @@ -214,12 +220,65 @@ } } - private String generateGetStatement(String assignedValue, String path, Object type) { - String result = assignedValue + " = client.target(\"http://localhost:8080\").path(" + path + ").request().get(" - + type.getClass().toString() + ");"; - + private String generateGetStatement(String assignedValue, String path, Type type) { + String result = ""; + String tmp = assignedValue; + 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 += 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"; + } + 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")) { + //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()) { + + } + } +// List> handsA_json = client.target("http://localhost:8080").path("/handsA").request().get(ArrayList.class); + // List> handsA = new ArrayList<>(); + // for (Map i: handsA_json) { + // handsA.add(new + // AbstractMap.SimpleEntry<>(Integer.parseInt(i.entrySet().iterator().next().getKey()), + // i.entrySet().iterator().next().getValue())); + // } + return ""; + } private void changePull2Push(IProgressMonitor pm) { @@ -248,7 +307,17 @@ 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 deleteASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode) { TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); astRewrite.remove(astNode, dstEditGroup);