]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/change/executors/ExtractAndMoveMethodExecutor.java
ExtractAndMoveMethodAnalysisResult: renaming to ExtractAndMoveMethodCandidate
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / change / executors / ExtractAndMoveMethodExecutor.java
1 package no.uio.ifi.refaktor.change.executors;
2
3 import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodCandidate;
4 import no.uio.ifi.refaktor.change.exceptions.RefactoringNotExecutedException;
5 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
6
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.jdt.core.JavaModelException;
10 import org.eclipse.jdt.core.dom.IVariableBinding;
11
12 /**
13  * This {@link Executor} composes the refactorings known as 'Extract Method' and 'Move Method'.
14  * 
15  * It takes the result of an analysis (see {@link ExtractAndMoveMethodCandidate})
16  * and uses its textSelection, new method name and original target. First it executes 
17  * an {@link ExtractMethodRefactoringExecutor} and then a 
18  * {@link MoveMethodRefactoringExecutor}.
19  */
20 public class ExtractAndMoveMethodExecutor implements Executor {
21         private final CompilationUnitTextSelection textSelection;
22         private final IVariableBinding originalTarget;
23         private String newMethodName;
24         private ExtractAndMoveMethodCandidate analysisResult;
25         private ExtractMethodPostExecutionResources postExecutionResources;
26         private IProgressMonitor monitor;
27
28         public ExtractAndMoveMethodExecutor(ExtractAndMoveMethodCandidate analysisResult) {
29                 this(analysisResult.textSelection, analysisResult.getNewMethodName(), analysisResult.calculateOriginalTarget());
30                 this.analysisResult = analysisResult;
31         }
32
33         public ExtractAndMoveMethodExecutor(CompilationUnitTextSelection textSelection, String newMethodName, IVariableBinding originalTarget) {
34                 this.textSelection = textSelection;
35                 setNewMethodName(newMethodName);
36                 this.originalTarget = originalTarget;
37         }
38
39         public void setNewMethodName(String newMethodName) {
40                 this.newMethodName = newMethodName;
41         }
42
43         @Override
44         public void execute(IProgressMonitor monitor) throws CoreException {
45                 this.monitor = monitor;
46                 try {
47                         performExtractMethodRefactoringAndCollectPostExecutionResources();
48                         performMoveMethodRefactoring();
49                 } catch (RefactoringNotExecutedException e) {
50                         throw new RefactoringNotExecutedException(e.getMessage() + " - originating from " + analysisResult.getOriginatingMethodName() 
51                                         + " with target " + originalTarget.getName() + " and text selection " + analysisResult.textSelection);
52                 }
53         }
54
55         private void performExtractMethodRefactoringAndCollectPostExecutionResources() throws CoreException {
56                 ExtractMethodRefactoringExecutor executor = new ExtractMethodRefactoringExecutor(textSelection, newMethodName);
57                 executor.execute(monitor);
58                 postExecutionResources = executor.getPostExecutionResources();
59         }
60
61         private void performMoveMethodRefactoring() throws JavaModelException, CoreException {
62                 MoveMethodRefactoringExecutor executor = new MoveMethodRefactoringExecutor(postExecutionResources, originalTarget); 
63                 executor.execute(monitor);
64         }
65 }