1 package no.uio.ifi.refaktor.analyze.analyzers;
3 import java.util.Collections;
4 import java.util.LinkedList;
7 import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodCandidate;
8 import no.uio.ifi.refaktor.analyze.comparators.ExtractAndMoveMethodCandidateComparator;
9 import no.uio.ifi.refaktor.analyze.comparators.IgnorantCandidateComparator;
10 import no.uio.ifi.refaktor.analyze.exceptions.GenericAnalyzerException;
11 import no.uio.ifi.refaktor.analyze.exceptions.NoTargetFoundException;
12 import no.uio.ifi.refaktor.analyze.exceptions.RefaktorAnalyzerException;
13 import no.uio.ifi.refaktor.debugging.RefaktorDebug;
14 import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection;
16 import org.eclipse.jdt.core.Flags;
17 import org.eclipse.jdt.core.IMethod;
18 import org.eclipse.jdt.core.JavaModelException;
21 * A class for searching through a method body to find the best
22 * candidate for the Extract and Move Method refactoring.
24 public class SearchBasedExtractAndMoveMethodAnalyzer implements Analyzer {
27 private final ExtractAndMoveMethodCandidateComparator candidateComparator;
28 final LinkedList<ExtractAndMoveMethodCandidate> candidates;
29 final StatementListsCreator statementsCreator;
31 Set<CompilationUnitTextSelection> textSelections;
32 private ExtractAndMoveMethodCandidate chosenCandidate;
34 public SearchBasedExtractAndMoveMethodAnalyzer(IMethod method) {
35 this(method, new IgnorantCandidateComparator());
39 * @param method The handle of the method to analyze.
40 * @param candidateComparator A candidate comparator to compare two analysis candidates when deciding
41 * which of them that is the best candidate for the refactoring.
43 public SearchBasedExtractAndMoveMethodAnalyzer(IMethod method, ExtractAndMoveMethodCandidateComparator candidateComparator) {
45 this.candidateComparator = candidateComparator;
46 candidates = new LinkedList<ExtractAndMoveMethodCandidate>();
47 statementsCreator = new StatementListsCreator(method);
50 public ExtractAndMoveMethodCandidate getCandidate() {
51 return chosenCandidate;
55 public void analyze() throws RefaktorAnalyzerException {
56 // TODO: make the Extract and Move Method Refactoring work with static methods
57 abortIfStaticMethod();
60 generateTextSelections();
61 findPotentialCandidates();
62 } catch (RefaktorAnalyzerException e) {
63 throw new NoTargetFoundException(e);
66 if (candidates.isEmpty())
67 throw new NoTargetFoundException();
72 private void abortIfStaticMethod() {
74 if (Flags.isStatic(method.getFlags()))
75 throw new NoTargetFoundException(method.getElementName() + " is static!");
76 } catch (JavaModelException e) {
78 throw new GenericAnalyzerException(e);
82 private void generateTextSelections() {
83 textSelections = TextSelectionsGenerator.generateTextSelections(statementsCreator, method.getCompilationUnit());
86 private void findPotentialCandidates() {
87 for (CompilationUnitTextSelection textSelection: textSelections) {
88 ExtractAndMoveMethodAnalyzer analyzer = new ExtractAndMoveMethodAnalyzer(textSelection);
89 analyzer.generated_2107180729092345330(textSelection, this);
93 private void chooseCandidate() {
95 chosenCandidate = candidates.getLast();
98 private void sortCandidates() {
99 Collections.sort(candidates, candidateComparator);
102 public void generated_8976769988341837625(
103 TypeWideExtractAndMoveMethodAnalyzer typewideextractandmovemethodanalyzer) {
105 typewideextractandmovemethodanalyzer.candidates.add(getCandidate());