]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/refaktor-after/src/no/uio/ifi/refaktor/analyze/analyzers/SearchBasedExtractAndMoveMethodAnalyzer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-after / src / no / uio / ifi / refaktor / analyze / analyzers / SearchBasedExtractAndMoveMethodAnalyzer.java
1 package no.uio.ifi.refaktor.analyze.analyzers;
2
3 import java.util.Collections;
4 import java.util.LinkedList;
5 import java.util.Set;
6
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;
15
16 import org.eclipse.jdt.core.Flags;
17 import org.eclipse.jdt.core.IMethod;
18 import org.eclipse.jdt.core.JavaModelException;
19
20 /**
21  * A class for searching through a method body to find the best
22  * candidate for the Extract and Move Method refactoring.
23  */
24 public class SearchBasedExtractAndMoveMethodAnalyzer implements Analyzer {
25
26         final IMethod method;
27         private final ExtractAndMoveMethodCandidateComparator candidateComparator;
28         final LinkedList<ExtractAndMoveMethodCandidate> candidates;
29         final StatementListsCreator statementsCreator;
30
31         Set<CompilationUnitTextSelection> textSelections;
32         private ExtractAndMoveMethodCandidate chosenCandidate;
33
34         public SearchBasedExtractAndMoveMethodAnalyzer(IMethod method) {
35                 this(method, new IgnorantCandidateComparator());
36         }
37
38         /**
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.
42          */
43         public SearchBasedExtractAndMoveMethodAnalyzer(IMethod method, ExtractAndMoveMethodCandidateComparator candidateComparator) {
44                 this.method = method;
45                 this.candidateComparator = candidateComparator;
46                 candidates = new LinkedList<ExtractAndMoveMethodCandidate>();
47                 statementsCreator = new StatementListsCreator(method);
48         }
49
50         public ExtractAndMoveMethodCandidate getCandidate() {
51                 return chosenCandidate;
52         }
53
54         @Override
55         public void analyze() throws RefaktorAnalyzerException {
56                 // TODO: make the Extract and Move Method Refactoring work with static methods
57                 abortIfStaticMethod();
58
59                 try {
60                         generateTextSelections();
61                         findPotentialCandidates();
62                 } catch (RefaktorAnalyzerException e) {
63                         throw new NoTargetFoundException(e);
64                 }
65
66                 if (candidates.isEmpty())
67                         throw new NoTargetFoundException();
68
69                 chooseCandidate();
70         }
71
72         private void abortIfStaticMethod() {
73                 try {
74                         if (Flags.isStatic(method.getFlags()))
75                                 throw new NoTargetFoundException(method.getElementName() + " is static!");
76                 } catch (JavaModelException e) {
77                         RefaktorDebug.log(e);
78                         throw new GenericAnalyzerException(e);
79                 }
80         }
81
82         private void generateTextSelections() {
83                 textSelections = TextSelectionsGenerator.generateTextSelections(statementsCreator, method.getCompilationUnit());
84         }
85
86         private void findPotentialCandidates() {
87                 for (CompilationUnitTextSelection textSelection: textSelections) {
88                         ExtractAndMoveMethodAnalyzer analyzer = new ExtractAndMoveMethodAnalyzer(textSelection);
89                         analyzer.generated_2107180729092345330(textSelection, this);
90                 }
91         }
92
93         private void chooseCandidate() {
94                 sortCandidates();
95                 chosenCandidate = candidates.getLast();
96         }
97
98         private void sortCandidates() {
99                 Collections.sort(candidates, candidateComparator);
100         }
101
102         public void generated_8976769988341837625(
103                         TypeWideExtractAndMoveMethodAnalyzer typewideextractandmovemethodanalyzer) {
104                 analyze();
105                 typewideextractandmovemethodanalyzer.candidates.add(getCandidate());
106         }
107 }