diff --git a/org.ntlab.refactoring.decouplingClasses/META-INF/MANIFEST.MF b/org.ntlab.refactoring.decouplingClasses/META-INF/MANIFEST.MF index ea34033..5362c18 100644 --- a/org.ntlab.refactoring.decouplingClasses/META-INF/MANIFEST.MF +++ b/org.ntlab.refactoring.decouplingClasses/META-INF/MANIFEST.MF @@ -12,7 +12,8 @@ org.eclipse.ui.workbench, org.eclipse.jdt.ui, org.eclipse.ltk.core.refactoring, - org.eclipse.jdt.core.manipulation + org.eclipse.jdt.core.manipulation, + org.eclipse.core.filebuffers;bundle-version="3.6.400" Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Bundle-ActivationPolicy: lazy Import-Package: org.eclipse.core.filesystem, diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java index afa2492..7a3833e 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java @@ -78,13 +78,15 @@ import org.eclipse.text.edits.TextEdit; import org.eclipse.ui.ide.ResourceUtil; import org.ntlab.refactoring.decouplingClasses.descriptors.DecoupleClassesDescriptor; +import org.ntlab.refactoring.decouplingClasses.refactoringApplication.RefactoringApplication; import test.pseudocode.core.DepthFirstTopologicalSorter; import test.pseudocode.core.Node; public class DecoupleClassesRefactoringProcessor extends RefactoringProcessor { private DecoupleClassesDescriptor descriptor = null; - + private List refactoringTasks; + public DecoupleClassesRefactoringProcessor(DecoupleClassesDescriptor descriptor) { this.descriptor = descriptor; } @@ -127,7 +129,7 @@ RefactoringTaskGenerator taskGenerator = new RefactoringTaskGenerator(); taskGenerator.setCfrom(descriptor.getClients()); taskGenerator.setTargetClass(descriptor.getTargetType()); - List refactoringTasks = taskGenerator.generateTasks(); + refactoringTasks = taskGenerator.generateTasks(); new DepthFirstTopologicalSorter().sort(refactoringTasks); @@ -215,11 +217,20 @@ String newSource = document.get(); workingCopy.getBuffer().setContents(newSource); - - change.add(new CreateNewClassChange(cu, document.get())); + + // TODO: �͋Z�Ȃ̂����Ƃ����� + String source = ""; + source += "package " + packageFragment.getElementName() + "\n"; + System.out.println(source); +// change.add(new CreateNewClassChange(cu, document.get())); workingCopy.discardWorkingCopy(); - + + // add refactor + for(Node refactor : refactoringTasks) { + change.add((RefactoringApplication)refactor); + } + return change; } @@ -307,6 +318,7 @@ CompilationUnit unit = (CompilationUnit) parser.createAST(null); final List results = new ArrayList<>(); unit.accept(new ASTVisitor() { + AST ast; /** * �N���X�錾�����‚���ƌĂ΂�郁�\�b�h */ @@ -315,12 +327,14 @@ ITypeBinding typeBinding = node.resolveBinding();// �ڍׂȏ���ITypeBinding�C���X�^���X���g���Ď擾������ ITypeBinding superClass = typeBinding.getSuperclass();// �e�N���X�̎擾 ITypeBinding[] interfaces = typeBinding.getInterfaces();// �C���^�[�t�F�[�X�̎擾 - String className = typeBinding.getBinaryName();// �N���X���̎擾 +// String className = typeBinding.getBinaryName();// �N���X���̎擾 + String className = typeBinding.getName();// �N���X���̎擾 int modifiers = typeBinding.getModifiers();// "public static"�Ƃ��̎��ʎq System.out.println("ClassName: " + className); System.out.println("Modifiers: " + modifiers); System.out.println("SuperClass: " + superClass.getBinaryName()); // System.out.println("Interfaces: " + interfaces); + results.add(ast.newSimpleType(ast.newSimpleName(typeBinding.getName()))); return super.visit(node); } /** @@ -351,7 +365,12 @@ System.out.println("Parameters: "+ node.parameters().toString()); return super.visit(node); } - }); + + public ASTVisitor setAst(AST ast) { + this.ast = ast; + return this; + } + }.setAst(ast)); fieldDeclaration.setType(results.get(0)); return fieldDeclaration; diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecouplingClassesRefactoringHelper.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecouplingClassesRefactoringHelper.java new file mode 100644 index 0000000..856865e --- /dev/null +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecouplingClassesRefactoringHelper.java @@ -0,0 +1,18 @@ +package org.ntlab.refactoring.decouplingClasses; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.swt.widgets.Display; +import org.eclipse.text.edits.MalformedTreeException; +import org.eclipse.text.edits.TextEdit; + +public class DecouplingClassesRefactoringHelper { +} diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/MyInvocationVisitorForReturnWholeObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/MyInvocationVisitorForReturnWholeObject.java new file mode 100644 index 0000000..bb98a24 --- /dev/null +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/MyInvocationVisitorForReturnWholeObject.java @@ -0,0 +1,33 @@ +package org.ntlab.refactoring.decouplingClasses; + +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; + +public class MyInvocationVisitorForReturnWholeObject extends ASTVisitor { + private boolean isReplaced = false; + private ASTNode removedInvocation; + private ASTRewrite rewrite; + + public MyInvocationVisitorForReturnWholeObject(ASTRewrite rewrite) { + super(); + this.rewrite = rewrite; + } + + public boolean visit(MethodInvocation node) { + if (!isReplaced) { + System.out.println("\n\n\nname: " + node.getName()); + removedInvocation = ASTNode.copySubtree(node.getAST(), node.getName()); +// removedInvocation = rewrite.createCopyTarget(node); + rewrite.replace(node, node.getExpression(), null); + System.out.println("MethodInvocation: " + node.getExpression()); + isReplaced = true; + } + return super.visit(node); + } + + public ASTNode getRemovedInvocation() { + return removedInvocation; + } +} diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/MyReturnStatementVisitorForReturnWholeObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/MyReturnStatementVisitorForReturnWholeObject.java new file mode 100644 index 0000000..b468902 --- /dev/null +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/MyReturnStatementVisitorForReturnWholeObject.java @@ -0,0 +1,35 @@ +package org.ntlab.refactoring.decouplingClasses; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; + +public class MyReturnStatementVisitorForReturnWholeObject extends ASTVisitor { + private ASTRewrite rewrite; + private ASTNode astNode; + private boolean isReplaced = false; + private ASTNode removedInvocation = null; + + public MyReturnStatementVisitorForReturnWholeObject(ASTRewrite rewrite, ASTNode astNode) { + super(); + this.rewrite = rewrite; + this.astNode = astNode; + } + public boolean visit(ReturnStatement node) { + if (!isReplaced && astNode.getClass().equals(node.getClass())) { + AST ast = node.getAST(); + MyInvocationVisitorForReturnWholeObject visitor = new MyInvocationVisitorForReturnWholeObject(rewrite); + node.accept(visitor); + removedInvocation = visitor.getRemovedInvocation(); + isReplaced = true; + } + return super.visit(node); + } + + public ASTNode getRemovedInvocation() { + return removedInvocation; + } +} diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java index 6face45..c35c762 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java @@ -56,17 +56,19 @@ private void exploreClasses() { ASTParser parser = ASTParser.newParser(AST.JLS8); - parser.setResolveBindings(true); - parser.setBindingsRecovery(true); parser.setKind(ASTParser.K_COMPILATION_UNIT); - + for (IType clientType: cFrom) { parser.setSource(clientType.getCompilationUnit()); + parser.setResolveBindings(true); + parser.setBindingsRecovery(true); CompilationUnit cuNode = (CompilationUnit) parser.createAST(null); System.out.println("--- " + cuNode.getJavaElement().getElementName() + " ---"); // �^�[�Q�b�g�N���X�̏o���ʒu�ɉ����Ċ�{���t�@�N�^�����O��lj�����B cuNode.accept(new ASTVisitor() { + private CompilationUnit cu; + @Override public boolean visit(MethodDeclaration node) { for (Object obj: node.parameters()) { @@ -203,10 +205,11 @@ public boolean visit(ReturnStatement node) { System.out.println("Return Statement: " + node.toString()); MethodDeclaration mDec = m(node); + String returnTypeName = mDec.getReturnType2().toString(); String targetTypeName = targetClass.getElementName(); if (returnTypeName.equals(targetTypeName)) { - addRefactoringTask(mDec, new ReturnWholeObject(node)); + addRefactoringTask(mDec, new ReturnWholeObject(node, mDec, cu)); } return super.visit(node); @@ -234,7 +237,12 @@ } list.add(refactoring); } - }); + + public ASTVisitor setCompilationUnit(CompilationUnit cu) { + this.cu = cu; + return this; + } + }.setCompilationUnit(cuNode)); System.out.println(); } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/TargetOccurenceSearchEngine.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/TargetOccurenceSearchEngine.java index 97bb9ec..38d292c 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/TargetOccurenceSearchEngine.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/TargetOccurenceSearchEngine.java @@ -1,13 +1,24 @@ package org.ntlab.refactoring.decouplingClasses; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.IMethodBinding; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.core.search.SearchEngine; @@ -15,6 +26,7 @@ import org.eclipse.jdt.core.search.SearchParticipant; import org.eclipse.jdt.core.search.SearchPattern; import org.eclipse.jdt.core.search.SearchRequestor; +import org.ntlab.refactoring.decouplingClasses.refactoringApplication.IntroduceParameterObject; public class TargetOccurenceSearchEngine { private final SearchEngine defaultSearchEngine = new SearchEngine(); @@ -109,6 +121,48 @@ return; } } + + public HashMap> searchMethodInvocationAsts(final IType target, final IMethod method) { + HashMap> result = new HashMap>(); + List clients = findClients(target); + for(IJavaElement elem : clients) { + IType m = (IType)elem; + ASTParser parser = ASTParser.newParser(AST.JLS8); + parser.setSource(m.getCompilationUnit()); + parser.setResolveBindings(true); + parser.setBindingsRecovery(true); + final CompilationUnit cuNode = (CompilationUnit) parser.createAST(null); + + System.out.println("---�Ăяo���������J�n---"); + System.out.println("Element: " + elem.toString()); + + cuNode.accept(new ASTVisitor() { + private HashMap> result; + public boolean visit(MethodInvocation node) { + IMethodBinding methodBinding = node.resolveMethodBinding(); + IMethod calledObjectMethod = (IMethod) methodBinding.getJavaElement(); + if (calledObjectMethod.equals(method)) { + if (!result.containsKey(cuNode)) { + result.put(cuNode, new ArrayList()); + } + result.get(cuNode).add(node); + System.out.println("invoke: " + node.toString()); + System.out.println("same method: " + method.getElementName()); + } + + return super.visit(node); + } + public ASTVisitor setResultArray(HashMap> result) { + this.result = result; + return this; + } + }.setResultArray(result)); +// + System.out.println("---�Ăяo���������I��---"); + } + + return result; + } private class CollectingRequestor extends SearchRequestor { protected List results = new ArrayList<>(); diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java index 6a8d8be..515ce1f 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java @@ -3,7 +3,12 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; import test.pseudocode.core.Node; @@ -94,4 +99,28 @@ } return result; } + + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java index ef5aaf8..2a3534d 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java @@ -1,7 +1,12 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class HideDelegate extends PrimitiveRefactoring { @@ -12,4 +17,28 @@ public MethodInvocation getApplicationPoint() { return (MethodInvocation) super.getApplicationPoint(); } + + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + System.out.println("perform: " + getName()); + return null; + } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java index b593365..3356243 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java @@ -1,7 +1,12 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class IntroduceParameterObject extends PrimitiveRefactoring { @@ -14,4 +19,28 @@ return (MethodDeclaration) super.getApplicationPoint(); } + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + System.out.println("perform: " + getName()); + return null; + } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java index 46eb728..2074de4 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java @@ -1,6 +1,11 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class IntroduceReturnValueObject extends PrimitiveRefactoring { @@ -8,4 +13,28 @@ super("Introduce Return Value Object", applicationPoint); } + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + System.out.println("perform: " + getName()); + return null; + } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java index bb01358..016110a 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java @@ -1,7 +1,12 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class PreserveWholeObject extends PrimitiveRefactoring { @@ -13,4 +18,28 @@ public MethodInvocation getApplicationPoint() { return (MethodInvocation) super.getApplicationPoint(); } + + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + System.out.println("perform: " + getName()); + return null; + } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java index ab6932d..1aa74a0 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java @@ -57,4 +57,8 @@ } return result; } + + public ASTNode getNode() { + return applicationPoint; + } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java index 395b53c..06ab324 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java @@ -1,10 +1,11 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.ltk.core.refactoring.Change; import test.pseudocode.core.Node; -public abstract class RefactoringApplication implements Node { +public abstract class RefactoringApplication extends Change implements Node { protected String name = ""; private boolean visited = false; private RefactoringApplication parent = null; diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java index ec7b442..aae02be 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java @@ -1,10 +1,39 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class ReplaceTypeByHiding extends PrimitiveRefactoring { public ReplaceTypeByHiding(ASTNode applicationPoint) { super("Replace Type by Hiding", applicationPoint); } + + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + System.out.println("perform: " + getName()); + return null; + } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java index b8b0582..b2dc54f 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java @@ -1,16 +1,194 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import javax.imageio.ImageTypeSpecifier; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.FieldAccess; +import org.eclipse.jdt.core.dom.IMethodBinding; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.MethodRef; import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.Type; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +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.text.edits.MalformedTreeException; +import org.eclipse.text.edits.TextEdit; +import org.ntlab.refactoring.decouplingClasses.DecouplingClassesRefactoringHelper; +import org.ntlab.refactoring.decouplingClasses.MyInvocationVisitorForReturnWholeObject; +import org.ntlab.refactoring.decouplingClasses.MyReturnStatementVisitorForReturnWholeObject; +import org.ntlab.refactoring.decouplingClasses.TargetOccurenceSearchEngine; public class ReturnWholeObject extends PrimitiveRefactoring { - - public ReturnWholeObject(ASTNode applicationPoint) { + MethodDeclaration methodDecl; + CompilationUnit cu; + + public ReturnWholeObject(ASTNode applicationPoint, MethodDeclaration m, CompilationUnit cu) { super("Return Whole Object", applicationPoint); + methodDecl = m; + this.cu = cu; } @Override public ReturnStatement getApplicationPoint() { return (ReturnStatement) super.getApplicationPoint(); } + + @Override + public void initializeValidationData(IProgressMonitor pm) { + // TODO Auto-generated method stub + + } + + @Override + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { + // TODO Auto-generated method stub + return new RefactoringStatus(); + } + + @Override + public Change perform(IProgressMonitor pm) throws CoreException { + System.out.println("\n-----ReturnWholeObject ���t�@�N�^�����O�J�n-----"); + System.out.println("perform: " + getName()); + System.out.println("node: " + getNode().toString()); + System.out.println("return type: " + methodDecl.getReturnType2()); + IPath path = cu.getJavaElement().getPath(); +// Document document = new Document(cu.toString()); + ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); +// cu.recordModifications(); + // results.add(ast.newSimpleType(ast.newSimpleName(typeBinding.getName()))); // Type�ݒ�T���v�� + // fieldDeclaration.setType(results.get(0)); + ASTNode node = getNode(); + AST ast = methodDecl.getAST(); + + Type t = ast.newSimpleType(ast.newSimpleName("CNew")); + rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, t, null); + + IMethodBinding methodBinding = methodDecl.resolveBinding(); + IMethod iMethod = (IMethod) methodBinding.getJavaElement(); + IType tt = iMethod.getDeclaringType(); + + TargetOccurenceSearchEngine searchEngine = new TargetOccurenceSearchEngine(); + HashMap> clients = searchEngine.searchMethodInvocationAsts(tt, iMethod); + +// Type t = ast.newSimpleType(ast.newSimpleName("String")); // Type�ݒ�T���v�� +// methodDecl.setReturnType2(t); + + MyReturnStatementVisitorForReturnWholeObject visitor = new MyReturnStatementVisitorForReturnWholeObject(rewrite, getNode()); + // return�̏C�� + cu.accept(visitor); + ASTNode removedInvocation = visitor.getRemovedInvocation(); + +// applyRewrite(path, rewrite); + try { + ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); + bufferManager.connect(path, null); + ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path); + IDocument document = textFileBuffer.getDocument(); + TextEdit edit = rewrite.rewriteAST(document, null); + edit.apply(document); + textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); + } catch (MalformedTreeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (BadLocationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + // CompilationUnit���Ƃ�getCTarget�̕t�^�����Ă��� + for(CompilationUnit cu : clients.keySet()) { + IPath cupath = cu.getJavaElement().getPath(); + ArrayList nodes = clients.get(cu); + ASTRewrite rewrite2 = ASTRewrite.create(cu.getAST()); + for(MethodInvocation invocation : nodes) { + MethodInvocation copiedNode = (MethodInvocation) ASTNode.copySubtree(invocation.getAST(), invocation); +// MethodInvocation copiedNode = (MethodInvocation)rewrite2.createCopyTarget(invocation); + AST ast2 = copiedNode.getAST(); + MethodInvocation newInvocation = ast2.newMethodInvocation(); + newInvocation.setExpression(copiedNode); + newInvocation.setName(ast2.newSimpleName(removedInvocation.toString())); + rewrite2.replace(invocation, newInvocation, null); + } +// applyRewrite(cupath, rewrite2); + try { + ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); + bufferManager.connect(cupath, null); + ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(cupath); + IDocument document = textFileBuffer.getDocument(); + TextEdit edit = rewrite2.rewriteAST(document, null); + edit.apply(document); + textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); + } catch (MalformedTreeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (BadLocationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return null; + } + +// public boolean applyRewrite(final IPath path, final ASTRewrite rewrite) { +// try { +// ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); +// bufferManager.connect(path, null); +// ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path); +// IDocument document = textFileBuffer.getDocument(); +// TextEdit edit = rewrite.rewriteAST(document, null); +// edit.apply(document); +// textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); +// } catch (MalformedTreeException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// return false; +// } catch (BadLocationException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// return false; +// } catch (CoreException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// return false; +// } +// return true; +// } + + @Override + public Object getModifiedElement() { + // TODO Auto-generated method stub + return null; + } }