]>
Commit | Line | Data |
---|---|---|
14dc9ff0 EK |
1 | package no.uio.ifi.refaktor.analyze; |
2 | ||
46cc235b EK |
3 | import static no.uio.ifi.refaktor.RefaktorAssert.assertThat; |
4 | import static org.hamcrest.CoreMatchers.notNullValue; | |
37be5000 | 5 | import no.uio.ifi.refaktor.changers.ExtractAndMoveMethodChanger; |
14dc9ff0 EK |
6 | import no.uio.ifi.refaktor.prefix.Prefix; |
7 | import no.uio.ifi.refaktor.prefix.PrefixSet; | |
8 | import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection; | |
6d081f7a | 9 | import no.uio.ifi.refaktor.utils.MethodRelativeCompilationUnitTextSelection; |
14dc9ff0 | 10 | |
4f577e24 | 11 | import org.eclipse.jdt.core.IMethod; |
14dc9ff0 | 12 | import org.eclipse.jdt.core.dom.IBinding; |
46cc235b | 13 | import org.eclipse.jdt.core.dom.IVariableBinding; |
14dc9ff0 | 14 | |
4f577e24 | 15 | public class ExtractAndMoveMethodAnalysisResult { |
46cc235b EK |
16 | private class ResultDataCalculator { |
17 | private final ExtractAndMoveMethodAnalyzer analyzer; | |
18 | ||
19 | public ResultDataCalculator(ExtractAndMoveMethodAnalyzer analyzer) { | |
20 | this.analyzer = analyzer; | |
21 | } | |
22 | ||
23 | public boolean hasMoreThanOnePossibleTarget() { | |
24 | PrefixSet safePrefixes = analyzer.calculateSafePrefixes(); | |
25 | IBinding binding = safePrefixes.toList().getFirst().getVariableBindingOfFirstExpression(); | |
26 | for (Prefix prefix: safePrefixes) { | |
27 | if (!prefix.getVariableBindingOfFirstExpression().isEqualTo(binding)) | |
28 | return true; | |
29 | } | |
30 | return false; | |
31 | } | |
32 | ||
33 | public int getTargetFrequency() { | |
34 | if (hasMoreThanOnePossibleTarget()) | |
35 | ; // TODO: throw exception? | |
36 | return analyzer.calculateMostFrequentPrefix().getCount(); | |
37 | } | |
38 | ||
39 | public boolean hasNoUnfixes() { | |
40 | return analyzer.getUnfixes().isEmpty(); | |
41 | } | |
42 | ||
43 | public String calculateOriginalTargetKey() { | |
44 | return analyzer.calculateOriginalTarget().getKey(); | |
45 | } | |
46 | } | |
47 | ||
14dc9ff0 | 48 | public final CompilationUnitTextSelection textSelection; |
4f577e24 EK |
49 | public final int numberOfStatementsAnalyzed; |
50 | public final int numberOfTextSelectionsAnalyzed; | |
51 | public final IMethod method; | |
14dc9ff0 | 52 | |
46cc235b EK |
53 | private final boolean hasMoreThanOnePossibleTarget; |
54 | private final int targetFrequency; | |
55 | private final boolean hasNoUnfixes; | |
56 | private final String originalTargetKey; | |
4f577e24 EK |
57 | |
58 | public ExtractAndMoveMethodAnalysisResult(IMethod method, ExtractAndMoveMethodAnalyzer analyzer, CompilationUnitTextSelection textSelection, int numberOfStatementsAnalyzed, int numberOfTextSelectionsAnalyzed) { | |
59 | this.method = method; | |
6d081f7a | 60 | this.textSelection = new MethodRelativeCompilationUnitTextSelection(method, textSelection); |
4f577e24 EK |
61 | this.numberOfStatementsAnalyzed = numberOfStatementsAnalyzed; |
62 | this.numberOfTextSelectionsAnalyzed = numberOfTextSelectionsAnalyzed; | |
46cc235b EK |
63 | |
64 | ResultDataCalculator calculator = new ResultDataCalculator(analyzer); | |
65 | hasMoreThanOnePossibleTarget = calculator.hasMoreThanOnePossibleTarget(); | |
66 | targetFrequency = calculator.getTargetFrequency(); | |
67 | hasNoUnfixes = calculator.hasNoUnfixes(); | |
68 | originalTargetKey = calculator.calculateOriginalTargetKey(); | |
69 | ||
70 | dropExpensiveCachedData(); | |
71 | } | |
72 | ||
73 | private void dropExpensiveCachedData() { | |
74 | textSelection.dropCachedData(); | |
14dc9ff0 EK |
75 | } |
76 | ||
77 | public boolean hasMoreThanOnePossibleTarget() { | |
46cc235b | 78 | return hasMoreThanOnePossibleTarget; |
14dc9ff0 EK |
79 | } |
80 | ||
81 | public boolean hasOnlyOnePossibleTarget() { | |
82 | return !hasMoreThanOnePossibleTarget(); | |
83 | } | |
84 | ||
85 | public int getTargetFrequency() { | |
46cc235b | 86 | return targetFrequency; |
14dc9ff0 EK |
87 | } |
88 | ||
89 | public boolean hasNoUnfixes() { | |
46cc235b | 90 | return hasNoUnfixes; |
14dc9ff0 EK |
91 | } |
92 | ||
93 | public boolean hasUnfixes() { | |
94 | return !hasNoUnfixes(); | |
95 | } | |
4f577e24 | 96 | |
46cc235b EK |
97 | public IVariableBinding calculateOriginalTarget() { |
98 | return restoreOriginalTarget(); | |
99 | } | |
100 | ||
101 | private IVariableBinding restoreOriginalTarget() { | |
102 | VariableBindingFinder variableBindingFinder = new VariableBindingFinder(originalTargetKey); | |
103 | textSelection.getCoveringNode().accept(variableBindingFinder); | |
104 | assertThat(variableBindingFinder.getVariableBinding(), notNullValue()); | |
105 | return variableBindingFinder.getVariableBinding(); | |
4f577e24 EK |
106 | } |
107 | ||
108 | @Override | |
109 | public String toString() { | |
06adde11 | 110 | return "Result for '" + method.getDeclaringType().getElementName() + "." + method.getElementName() + "' (" + numberOfStatementsAnalyzed + " statements analyzed): < " + textSelection + " >"; |
4f577e24 | 111 | } |
37be5000 EK |
112 | |
113 | public String getNewMethodName() { | |
114 | // TODO: do better | |
115 | return ExtractAndMoveMethodChanger.generateName(); | |
116 | } | |
14dc9ff0 | 117 | } |