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