diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 62d4720..f60e2f5 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -35,8 +35,10 @@ import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.BodyDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; 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; @@ -78,6 +80,7 @@ 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.editors.text.FileDocumentProvider; import org.eclipse.ui.editors.text.TextEditor; import org.eclipse.ui.part.FileEditorInput; @@ -180,21 +183,21 @@ // // �ύX�ӏ��𐶐����� // - - String code=""; - try { - String initialContent = descriptor.getDistinationClass().getTypeRoot().getSource(); - // �ϊ����ʂ𕶎���Ŏ擾 - code = getCode(initialContent, descriptor.getDistinationClass()); - } catch (JavaModelException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - System.out.println(code); - - if (icus[0] != null) { - - } +// +// String code=""; +// try { +// String initialContent = descriptor.getDistinationClass().getTypeRoot().getSource(); +// // �ϊ����ʂ𕶎���Ŏ擾 +// code = getCode(initialContent, descriptor.getDistinationClass()); +// } catch (JavaModelException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// System.out.println(code); +// +// if (icus[0] != null) { +// +// } return result; } @@ -202,6 +205,30 @@ var srcUnit = descriptor.getSourceClass(); var dstUnit = descriptor.getDistinationClass(); var dstDec = new TypeDeclaration[1]; + // searchFieldDeclaration(descriptor.getDistinationClass(), "State").delete(); + // var ucu = updateCompilationUnit(descriptor.getDistinationClass()); + var md = searchMethodDeclaration(dstUnit, "Message",annotationValueCase(descriptor.getSourceClassName())); + var transition = new Expression[1]; + md.accept(new ASTVisitor() { + @Override + public boolean visit(InfixExpression node) { + var left = node.getLeftOperand(); + + transition[0] = node; + 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; + } + }); + + //var ucu2 = updateCompilationUnit(descriptor.getDistinationClass()); descriptor.getDistinationClass().accept(new ASTVisitor() { @Override public boolean visit(TypeDeclaration node) { @@ -210,18 +237,31 @@ } }); + //PullReference����̃t�B�[���h��lj� ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); - ListRewrite dstUnitRewriteList = dstUnitRewrite.getListRewrite(dstDec[0], TypeDeclaration.BODY_DECLARATIONS_PROPERTY); - PackageDeclaration dstPackageDeclaration =dstUnit.getPackage(); - String srcValue = annotationValueCase(descriptor.getSourceClassName()); - String pullrefCode = "@PullReference(\""+srcValue+"\")"+System.getProperty("line.separator")+ - "@Pushable"+System.getProperty("line.separator")+ - "String "+srcValue+"= "+"\"/"+srcValue+"\";"; - Statement pullRef = (Statement) dstUnitRewrite.createStringPlaceholder(pullrefCode, ASTNode.VARIABLE_DECLARATION_STATEMENT); + String srcValue = annotationValueCase(descriptor.getSourceClassName()); + String pullrefCode = "@PullReference(\""+srcValue+"\")"+System.getProperty("line.separator")+ + "@Pushable"+System.getProperty("line.separator")+ + "String "+srcValue+"= "+"\"/"+srcValue+"\";"; + deleteDeclaration(dstUnitRewrite, dstUnit, searchFieldDeclaration(descriptor.getDistinationClass(), "State")); + addStatement(dstUnitRewrite, dstUnit, dstDec[0],pullrefCode); + deleteDeclaration(dstUnitRewrite, dstUnit, searchMethodDeclaration(dstUnit, "Message",annotationValueCase(descriptor.getSourceClassName()))); + replaceStatement(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); + //addStatement(dstUnitRewrite, dstUnit, dstDec[0],pullrefCode); +// ListRewrite dstUnitRewriteList = dstUnitRewrite.getListRewrite(dstDec[0], TypeDeclaration.BODY_DECLARATIONS_PROPERTY); +// PackageDeclaration dstPackageDeclaration =dstUnit.getPackage(); +// String srcValue = annotationValueCase(descriptor.getSourceClassName()); +// String pullrefCode = "@PullReference(\""+srcValue+"\")"+System.getProperty("line.separator")+ +// "@Pushable"+System.getProperty("line.separator")+ +// "String "+srcValue+"= "+"\"/"+srcValue+"\";"; +// Statement pullRef = (Statement) dstUnitRewrite.createStringPlaceholder(pullrefCode, ASTNode.VARIABLE_DECLARATION_STATEMENT); +// +// dstUnitRewrite.set(dstUnit, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null); +// TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); +// dstUnitRewriteList.insertFirst(pullRef, dstEditGroup); - dstUnitRewrite.set(dstUnit, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null); - TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); - dstUnitRewriteList.insertFirst(pullRef, dstEditGroup); + + //�V�K�t�@�C���쐬 try { @@ -231,13 +271,13 @@ ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); IDocument document = textFileBuffer.getDocument(); TextEdit edit = dstUnitRewrite.rewriteAST(document, null); - edit.apply(document); + UndoEdit undo = edit.apply(document); textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); } catch(Exception e) { e.printStackTrace(); } - + // final FieldDeclaration typeDeclaration = searchFieldDeclaration(dstUnit, "State"); // ASTRewrite rewrite = ASTRewrite.create(typeDeclaration.getAST()); // ListRewrite rewrites = rewrite.getListRewrite(typeDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY); @@ -270,6 +310,31 @@ // } } + private void replaceStatement(ASTRewrite astRewrite,CompilationUnit cu, ASTNode astNode, ASTNode replasement) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.replace(astNode, replasement, dstEditGroup); + } + private String generateGetStatement(String assignedValue, String path, Object type) { + String result = assignedValue +" = client.target(\"http://localhost:8080\").path(" + path +").request().get("+type.getClass()+");"; + + return result; + } + 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 pullRef = (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(pullRef, dstEditGroup); + } + private void deleteDeclaration(ASTRewrite astRewrite,CompilationUnit cu, ASTNode astNode) { + + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.remove(astNode, dstEditGroup); + } private void changePull2Push(IProgressMonitor pm) { } @@ -369,30 +434,6 @@ return fieldDeclaration; } - private ASTNode searchFieldNode(String annotationName,String... value) { - ASTNode[] result = new ASTNode[1]; - final String a = "@"+annotationName; - descriptor.getDistinationClass().accept(new ASTVisitor() { - @Override - public boolean visit(FieldDeclaration node) { - var modifiers = node.modifiers(); - for(var annotation : modifiers) { - String annoName = annotation.toString(); - if(a.equals(annoName)) { - var r = node; - // result[0] = (SourceField)node; - List fragments = node.fragments(); - VariableDeclarationFragment fragment = fragments.get(0); - //IJavaElement fieldElement = fragment.resolveBinding().getJavaElement(); - result[0] = fragments.get(0); - } - } - return false; - } - - }); - return result[0]; - } /** * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. * �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� @@ -401,38 +442,13 @@ * @param value �A�m�e�[�V�����̒l * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� */ - private FieldDeclaration searchFieldDeclaration(CompilationUnit cu, String annotationName, String... value) { + private FieldDeclaration searchFieldDeclaration(CompilationUnit cu, String annotationName, String... values) { FieldDeclaration[] result = new FieldDeclaration[1]; - cu.accept(new ASTVisitor() { @Override public boolean visit(FieldDeclaration node) { - var modifiers = node.modifiers(); - for(var annotation : modifiers) { - //annotation���A�m�e�[�V��������Ȃ���,���̃��[�v�� - if(!(annotation instanceof Annotation))continue; - String annoName = ((Annotation)annotation).getTypeName().toString(); - if(annotationName.equals(annoName)) { - - //annotation���l�̖����A�m�e�[�V�����������ꍇ - if(annotation instanceof MarkerAnnotation) { - - result[0] = node; - } - - //annotation���l��1�‚̃A�m�e�[�V�����������ꍇ - if(annotation instanceof SingleMemberAnnotation) { - var singleMemberAnnotation = ( SingleMemberAnnotation)annotation; - if(singleMemberAnnotation.getValue().equals(value[0]))result[0] = node; - } - //annotation���l�𕡐����ƒA�m�e�[�V�����������ꍇ - if(annotation instanceof NormalAnnotation) { - NormalAnnotation normalAnnotation = (NormalAnnotation)annotation; - - // if(normalAnnotation.values())result[0] = node; - } - - } + if(hasMatchAnnotation(node,node.modifiers(), annotationName, values)) { + result[0] = node; } return super.visit(node); } @@ -449,45 +465,90 @@ * @param value �A�m�e�[�V�����̒l * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� */ - private MethodDeclaration searchMethodDeclaration(CompilationUnit cu, String annotationName, String... value) { + private MethodDeclaration searchMethodDeclaration(CompilationUnit cu, String annotationName, String... values) { MethodDeclaration[] result = new MethodDeclaration[1]; - cu.accept(new ASTVisitor() { @Override public boolean visit(MethodDeclaration node) { - var modifiers = node.modifiers(); - for(var annotation : modifiers) { - //annotation���A�m�e�[�V��������Ȃ���,���̃��[�v�� - if(!(annotation instanceof Annotation))continue; - String annoName = ((Annotation)annotation).getTypeName().toString(); - if(annotationName.equals(annoName)) { - - //annotation���l�̖����A�m�e�[�V�����������ꍇ - if(annotation instanceof MarkerAnnotation) { - - result[0] = node; - } - - //annotation���l��1�‚̃A�m�e�[�V�����������ꍇ - if(annotation instanceof SingleMemberAnnotation) { - var singleMemberAnnotation = ( SingleMemberAnnotation)annotation; - if(singleMemberAnnotation.getValue().equals(value[0]))result[0] = node; - } - //annotation���l�𕡐����ƒA�m�e�[�V�����������ꍇ - if(annotation instanceof NormalAnnotation) { - NormalAnnotation normalAnnotation = (NormalAnnotation)annotation; - - // if(normalAnnotation.values())result[0] = 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) { + SingleMemberAnnotation singleMemberAnnotation = ( SingleMemberAnnotation)annotation; + //�A�m�e�[�V��������擾�����l����擪�Ɩ����̃_�u���N�H�[�e�[�V�������폜���������� + String value = singleMemberAnnotation.getValue().toString().substring(1, singleMemberAnnotation.getValue().toString().length()-1); + if(value.equals(values[0]))return true; + } + + //annotation���l�𕡐����ƒA�m�e�[�V�����������ꍇ + if(annotation instanceof NormalAnnotation) { + NormalAnnotation normalAnnotation = (NormalAnnotation)annotation; + //������(�܂��l�̔��肪�ł��Ă��Ȃ�) + return true; + // if(normalAnnotation.values())result[0] = node; + } + + } + } + return false; + } + CompilationUnit updateCompilationUnit(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 { +// UndoEdit undo = edit.apply(eDoc); +// //edit.apply(eDoc); +// //return eDoc.get(); +// } catch (Exception ex) { +// ex.printStackTrace(); +// return null; +// } +// ICompilationUnit icu = (ICompilationUnit)unit.getJavaElement(); +// ASTParser parser = ASTParser.newParser(AST.JLS4); +// parser.setKind(ASTParser.K_COMPILATION_UNIT); +// parser.setResolveBindings(true); +// parser.setBindingsRecovery(true); +// parser.setSource(icu); +// unit = (CompilationUnit)parser.createAST(new NullProgressMonitor()); +// +// // get the ast rewriter +// final ASTRewrite rewriter = ASTRewrite.create(unit.getAST()); +// // get the current document source +// final Document document = new Document(unit.getSource()); +// // compute the edits you have made to the compilation unit +// final TextEdit edits = rewriter.rewriteAST(); +// // apply the edits to the document +// edits.apply(document); +// // get the new source +// String newSource = document.get(); +// // now write this source to some other file. + return unit; + } //2�‚̃��X�g�����—v�f�����������ǂ����𔻒肷�郁�\�b�h private boolean isEqualSets(List listA, List listB) {