package no.uio.ifi.refaktor.handlers; import static no.uio.ifi.refaktor.RefaktorAssert.assertThat; import static org.hamcrest.CoreMatchers.instanceOf; import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodAnalysisResult; import no.uio.ifi.refaktor.analyze.analyzers.SearchBasedExtractAndMoveMethodAnalyzer; import no.uio.ifi.refaktor.analyze.comparators.FavorNoUnfixesAnalysisResultComparator; import no.uio.ifi.refaktor.analyze.exceptions.NoTargetFoundException; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jdt.core.IMethod; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ITreeSelection; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.handlers.HandlerUtil; public class SearchBasedExtractAndMoveMethodAnalysisHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection = HandlerUtil.getCurrentSelectionChecked(event); IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); assertThat(selection, instanceOf(ITreeSelection.class)); ITreeSelection treeSelection = (ITreeSelection) selection; Object o = treeSelection.getFirstElement(); assertThat(o, instanceOf(IMethod.class)); IMethod method = (IMethod) o; String message = ""; try { SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method, new FavorNoUnfixesAnalysisResultComparator()); analyzer.analyze(); ExtractAndMoveMethodAnalysisResult result = analyzer.getResult(); message = "Result:\n\nText selection:\n" + result.textSelection + "\n\nSelected text:\n" + result.textSelection.getText() + "\n\nNumber of text selections analyzed:\n" + result.numberOfTextSelectionsAnalyzed + "\n\nOriginal target:\n" + result.calculateOriginalTarget(); } catch (NoTargetFoundException e) { message = "No solution found for " + method; } MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", message); return null; } }