]> git.uio.no Git - ifi-stolz-refaktor.git/blobdiff - case-study/refaktor-before/src/no/uio/ifi/refaktor/change/executors/ExtractMethodRefactoringExecutor.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / change / executors / ExtractMethodRefactoringExecutor.java
diff --git a/case-study/refaktor-before/src/no/uio/ifi/refaktor/change/executors/ExtractMethodRefactoringExecutor.java b/case-study/refaktor-before/src/no/uio/ifi/refaktor/change/executors/ExtractMethodRefactoringExecutor.java
new file mode 100644 (file)
index 0000000..79a628d
--- /dev/null
@@ -0,0 +1,82 @@
+package no.uio.ifi.refaktor.change.executors;
+
+import no.uio.ifi.refaktor.change.performers.ExtractMethodRefactoringPerformer;
+import no.uio.ifi.refaktor.change.performers.RefactoringPerformer;
+import no.uio.ifi.refaktor.debugging.RefaktorDebug;
+import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.core.dom.Modifier;
+import org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring;
+
+/**
+ * An {@link Executor} that is responsible for executing an {@link ExtractMethodRefactoring}.
+ * 
+ * Input is a {@link CompilationUnitTextSelection} and a name for the new method.
+ */
+@SuppressWarnings("restriction")
+class ExtractMethodRefactoringExecutor implements Executor {
+
+       private final CompilationUnitTextSelection compilationUnitTextSelection;
+       private final String newMethodName;
+       private ExtractMethodRefactoring extractMethodRefactoring;
+       private boolean replaceDuplicates;
+
+       public ExtractMethodRefactoringExecutor(CompilationUnitTextSelection compilationUnitTextSelection, String newMethodName) {
+               this.compilationUnitTextSelection = compilationUnitTextSelection;
+               this.newMethodName = newMethodName;
+               replaceDuplicates = false;
+       }
+
+       @Override
+       public void execute(IProgressMonitor monitor) throws CoreException {
+               extractMethodRefactoring = createExtractMethodRefactoring();
+               RefaktorDebug.println(this, "Trying to extract method to " + extractMethodRefactoring.getMethodName());
+               getRefactoringPerformer(monitor).performRefactoring(extractMethodRefactoring);
+               assertThatNewMethodNameWasUsedWhenPerformingRefactoring();
+       }
+
+       private RefactoringPerformer getRefactoringPerformer(IProgressMonitor monitor) {
+               return new ExtractMethodRefactoringPerformer(monitor, replaceDuplicates);
+       }
+
+       private ExtractMethodRefactoring createExtractMethodRefactoring() throws CoreException {
+               ExtractMethodRefactoring extractMethodRefactoring = new ExtractMethodRefactoring(
+                               compilationUnitTextSelection.getCompilationUnit(), 
+                               compilationUnitTextSelection.getOffset(), 
+                               compilationUnitTextSelection.getLength()
+                               );
+
+               extractMethodRefactoring.setMethodName(newMethodName);
+               extractMethodRefactoring.setValidationContext(null);
+               // This seems to have no effect! Hence the use of ExtractMethodRefactoringPerformer.
+               extractMethodRefactoring.setReplaceDuplicates(replaceDuplicates); 
+               extractMethodRefactoring.setVisibility(Modifier.PUBLIC);
+               extractMethodRefactoring.setLinkedProposalModel(null);
+               return extractMethodRefactoring;
+       }
+       
+       public void setReplaceDuplicates() {
+               this.replaceDuplicates = true;
+       }
+       
+       /**
+        * This is the default behavior.
+        */
+       public void setDoNotReplaceDuplicates() {
+               this.replaceDuplicates = false;
+       }
+
+       private void assertThatNewMethodNameWasUsedWhenPerformingRefactoring() {
+               assert extractMethodRefactoring.getMethodName().equals(newMethodName);
+       }
+
+       public ExtractMethodRefactoring getExtractMethodRefactoring() {
+               return extractMethodRefactoring;
+       }
+
+       public ExtractMethodPostExecutionResources getPostExecutionResources() {
+               return new ExtractMethodPostExecutionResources(extractMethodRefactoring, compilationUnitTextSelection);
+       }
+}