]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/SearchBasedExtractAndMoveMethodAnalysisHandler.java
skeleton done!
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / handlers / SearchBasedExtractAndMoveMethodAnalysisHandler.java
1 package no.uio.ifi.refaktor.handlers;
2
3 import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodCandidate;
4 import no.uio.ifi.refaktor.analyze.analyzers.SearchBasedExtractAndMoveMethodAnalyzer;
5 import no.uio.ifi.refaktor.analyze.comparators.FavorNoUnfixesExtractAndMoveCandidateComparator;
6 import no.uio.ifi.refaktor.analyze.exceptions.NoTargetFoundException;
7 import no.uio.ifi.refaktor.debugging.RefaktorDebug;
8 import no.uio.ifi.refaktor.statistics.StatisticsAspect;
9 import no.uio.ifi.refaktor.statistics.StatisticsAspect.Statistics;
10
11 import org.eclipse.core.commands.AbstractHandler;
12 import org.eclipse.core.commands.ExecutionEvent;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.jdt.core.IMethod;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ITreeSelection;
18 import org.eclipse.ui.IWorkbenchWindow;
19 import org.eclipse.ui.handlers.HandlerUtil;
20
21 public class SearchBasedExtractAndMoveMethodAnalysisHandler extends AbstractHandler {
22
23         @Override
24         public Object execute(ExecutionEvent event) throws ExecutionException {
25                 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
26                 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
27
28                 assert selection instanceof ITreeSelection;
29                 ITreeSelection treeSelection = (ITreeSelection) selection;
30                 Object o = treeSelection.getFirstElement();
31                 assert o instanceof IMethod;
32                 IMethod method = (IMethod) o;
33
34                 String message = "";
35                 try {
36                         StatisticsAspect.init();
37                         SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method, new FavorNoUnfixesExtractAndMoveCandidateComparator());
38                         analyzer.analyze();
39                         ExtractAndMoveMethodCandidate result = analyzer.getCandidate();
40                         message = "Result:\n\nText selection:\n" + result.textSelection 
41                                         + "\n\nNumber of text selections analyzed:\n" + result.numberOfTextSelectionsAnalyzed
42                                         + "\n\nOriginal target:\n" + result.calculateOriginalTarget()
43                                         + "\n\nSelected text:\n" + result.textSelection.getText();
44                 } catch (NoTargetFoundException e) {
45                         message = "No solution found for " + method;
46                 } finally {
47                         Statistics snapshot = StatisticsAspect.getSnapshot();
48                         RefaktorDebug.log(snapshot.toString());
49                 }
50
51                 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", message);
52                 return null;
53         }
54
55 }