diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index ab929fb..bed88d2 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.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -171,6 +172,7 @@ }); ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].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� @@ -209,49 +211,77 @@ 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(srcUnit, srcValue); - - // �V�K�t�@�C���쐬 + deletePutMethodInvocation(srcUnitRewrite, srcUnit, dstValue); + // @PushReference���t�^���ꂽ�t�B�[���h���폜 + deleteASTNode(srcUnitRewrite, srcUnit, searchFieldDeclaration(descriptor.getSourceClass(), "PushReference", dstValue)); try { - IPath path = dstUnit.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 = dstUnitRewrite.rewriteAST(document, null); - UndoEdit undo = edit.apply(document); - textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); - - } catch (Exception e) { - e.printStackTrace(); + applyRewrite(srcUnit, srcUnitRewrite, pm); + applyRewrite(dstUnit, dstUnitRewrite, pm); + + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); } +// // �V�K�t�@�C���쐬 +// try { +// IPath dstPath = dstUnit.getJavaElement().getPath(); +// ITextFileBufferManager dstBufferManager = FileBuffers.getTextFileBufferManager(); +// dstBufferManager.connect(dstPath, LocationKind.IFILE, pm); +// ITextFileBuffer dstTextFileBuffer = dstBufferManager.getTextFileBuffer(dstPath, LocationKind.IFILE); +// IDocument dstDocument = dstTextFileBuffer.getDocument(); +// TextEdit dstEdit = dstUnitRewrite.rewriteAST(dstDocument, null); +// UndoEdit dstUndo = dstEdit.apply(dstDocument); +// dstTextFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); +// } catch (Exception e) { +// e.printStackTrace(); +// } + } + private 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(pm /* ProgressMonitor */, true/* Overwrite */); } - private void deletePutMethodInvocation(ASTNode srcNode, String resourceName) { - var updateMethods = searchMethodDeclarations((CompilationUnit) srcNode, "Message"); + private void deletePutMethodInvocation(ASTRewrite astRewrite, CompilationUnit srcUnit, String resourceName) { + var updateMethods = searchMethodDeclarations(srcUnit, "Message"); for (MethodDeclaration md : updateMethods) { - List formStatements = new ArrayList<>(); - List entityStatements = new ArrayList<>(); - List resultStatements = new ArrayList<>(); - - List> invocations = new ArrayList<>(); + 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")) { - stopRegister(); + 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); @@ -263,8 +293,9 @@ startRegister(); } if (node.getLeftHandSide().toString().equals("result")) { + var right = node.getRightHandSide(); - stopRegister(); + stopRegister(""); } return super.visit(node); } @@ -273,54 +304,22 @@ isRegistering[0] = true; statements.clear(); } - private void stopRegister() { + + private void stopRegister(String resourceName) { statements.add(statement); - invocations.add(statements); + invocations.put(resourceName,statements); isRegistering[0] = false; } }); - if(isRegistering[0]) { + 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); + for(Statement st : invocations.get(resourceName)) { + deleteASTNode(astRewrite, srcUnit ,st ); + } } }