1 package no.uio.ifi.refaktor.handlers;
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
5 import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodAnalysisResult;
6 import no.uio.ifi.refaktor.analyze.NoTargetFoundException;
7 import no.uio.ifi.refaktor.analyze.SearchBasedExtractAndMoveMethodAnalyzer;
8 import no.uio.ifi.refaktor.analyze.comparators.FavorNoUnfixesAnalysisResultComparator;
10 import org.eclipse.core.commands.AbstractHandler;
11 import org.eclipse.core.commands.ExecutionEvent;
12 import org.eclipse.core.commands.ExecutionException;
13 import org.eclipse.jdt.core.IMethod;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ITreeSelection;
17 import org.eclipse.ui.IWorkbenchWindow;
18 import org.eclipse.ui.handlers.HandlerUtil;
20 public class SearchBasedExtractAndMoveMethodAnalysisHandler extends AbstractHandler {
23 public Object execute(ExecutionEvent event) throws ExecutionException {
24 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
25 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
27 assertThat(selection, instanceOf(ITreeSelection.class));
28 ITreeSelection treeSelection = (ITreeSelection) selection;
29 Object o = treeSelection.getFirstElement();
30 assertThat(o, instanceOf(IMethod.class));
31 IMethod method = (IMethod) o;
35 SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method, new FavorNoUnfixesAnalysisResultComparator());
37 ExtractAndMoveMethodAnalysisResult result = analyzer.getResult();
38 message = "Result:\n\nText selection:\n" + result.textSelection
39 + "\n\nSelected text:\n" + result.textSelection.getText()
40 + "\n\nNumber of text selections analyzed:\n" + result.numberOfTextSelectionsAnalyzed
41 + "\n\nOriginal target:\n" + result.calculateOriginalTarget();
42 } catch (NoTargetFoundException e) {
43 message = "No solution found for " + method;
46 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", message);