]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/refaktor-before/src/no/uio/ifi/refaktor/change/executors/MoveMethodRefactoringExecutor.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / change / executors / MoveMethodRefactoringExecutor.java
CommitLineData
1b2798f6
EK
1package no.uio.ifi.refaktor.change.executors;
2
3import no.uio.ifi.refaktor.change.performers.SimpleRefactoringPerformer;
4import no.uio.ifi.refaktor.change.processors.ModifiedMoveInstanceMethodProcessor;
5import no.uio.ifi.refaktor.debugging.RefaktorDebug;
6
7import org.eclipse.core.runtime.CoreException;
8import org.eclipse.core.runtime.IProgressMonitor;
9import org.eclipse.jdt.core.JavaModelException;
10import org.eclipse.jdt.core.dom.IVariableBinding;
11import org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring;
12import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
13import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
14
15/**
16 * An {@link Executor} that is respnsible for executing a Move Method refactoring.
17 *
18 * The executed refactoring is a {@link MoveRefactoring} that is
19 * fed a {@link MoveInstanceMethodProcessor}.
20 *
21 * Input to the executor is resources gathered after the Execution
22 * of an {@link ExtractMethodRefactoring}.
23 */
24@SuppressWarnings("restriction")
25class MoveMethodRefactoringExecutor implements Executor {
26
27 private final ExtractMethodPostExecutionResources resources;
28 private final IVariableBinding originalTarget;
29
30 public MoveMethodRefactoringExecutor(ExtractMethodPostExecutionResources resources, IVariableBinding originalTarget) {
31 this.resources = resources;
32 this.originalTarget = originalTarget;
33 }
34
35 @Override
36 public void execute(IProgressMonitor monitor) throws CoreException {
37 getRefactoringPerformer(monitor).performRefactoring(createMoveRefactoring());
38 }
39
40 private SimpleRefactoringPerformer getRefactoringPerformer(IProgressMonitor monitor) {
41 return new SimpleRefactoringPerformer(monitor);
42 }
43
44 private MoveRefactoring createMoveRefactoring() throws JavaModelException {
45 // Here, the method we'd like to move must already exist since we need to look up an IMethod handle:
46 ModifiedMoveInstanceMethodProcessor refactoringProcessor = new ModifiedMoveInstanceMethodProcessor(resources.getExtractedMethod(), null);
47 if (refactoringProcessor.canEnableDelegateUpdating())
48 refactoringProcessor.setDelegateUpdating(false);
49 refactoringProcessor.setDeprecateDelegates(true);
50 refactoringProcessor.setUseGetters(true);
51 refactoringProcessor.setUseSetters(true);
52
53 // Set the name of the parameter that takes an object of
54 // the type of the class the method is moved from.
55 refactoringProcessor.setTargetName(resources.getDeclaringTypeName().toLowerCase());
56
57 IVariableBinding target = findTarget();
58 refactoringProcessor.setTarget(target);
59
60 RefaktorDebug.println(this, "Trying to move method " + resources.getExtractedMethod().getElementName() + " to " + target.getName());
61
62 return new MoveRefactoring(refactoringProcessor);
63 }
64
65 private IVariableBinding findTarget() throws JavaModelException {
66 return createMoveMethodRefactoringTargetFinder().findTarget();
67 }
68
69 private MoveMethodRefactoringTargetFinder createMoveMethodRefactoringTargetFinder() throws JavaModelException {
70 return new MoveMethodRefactoringTargetFinder(resources.getExtractedMethod(), originalTarget, resources.getParameterInfos());
71 }
72}