1 package no.uio.ifi.refaktor.change.executors;
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;
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;
13 * This {@link Executor} composes the refactorings known as 'Extract Method' and 'Move Method'.
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}.
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;
28 public ExtractAndMoveMethodExecutor(ExtractAndMoveMethodCandidate analysisResult) {
29 this(analysisResult.textSelection, analysisResult.getNewMethodName(), analysisResult.calculateOriginalTarget());
30 this.analysisResult = analysisResult;
33 public ExtractAndMoveMethodExecutor(CompilationUnitTextSelection textSelection, String newMethodName, IVariableBinding originalTarget) {
34 this.textSelection = textSelection;
35 setNewMethodName(newMethodName);
36 this.originalTarget = originalTarget;
39 public void setNewMethodName(String newMethodName) {
40 this.newMethodName = newMethodName;
44 public void execute(IProgressMonitor monitor) throws CoreException {
45 this.monitor = monitor;
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);
55 private void performExtractMethodRefactoringAndCollectPostExecutionResources() throws CoreException {
56 ExtractMethodRefactoringExecutor executor = new ExtractMethodRefactoringExecutor(textSelection, newMethodName);
57 executor.execute(monitor);
58 postExecutionResources = executor.getPostExecutionResources();
61 private void performMoveMethodRefactoring() throws JavaModelException, CoreException {
62 MoveMethodRefactoringExecutor executor = new MoveMethodRefactoringExecutor(postExecutionResources, originalTarget);
63 executor.execute(monitor);