]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/analyzers/ExtractAndMoveMethodAnalyzer.java
Renaming no.uio.ifi.refaktor.changers to no.uio.ifi.refaktor.change
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / analyzers / ExtractAndMoveMethodAnalyzer.java
CommitLineData
33b364ef 1package no.uio.ifi.refaktor.analyze.analyzers;
64970e7f 2
e411ecf3
EK
3import java.util.Collections;
4import java.util.Comparator;
5import java.util.LinkedList;
2f82d251 6
33b364ef 7import no.uio.ifi.refaktor.analyze.CollectorManager;
fc35e1b0
EK
8import no.uio.ifi.refaktor.analyze.collectors.PrefixesCollector;
9import no.uio.ifi.refaktor.analyze.collectors.SelectionValidator;
10import no.uio.ifi.refaktor.analyze.collectors.UnfixesCollector;
ee0f23c0 11import no.uio.ifi.refaktor.analyze.exceptions.NoTargetFoundException;
e6db1ee7 12import no.uio.ifi.refaktor.change.ExtractAndMoveMethodChanger;
8a01ea52 13import no.uio.ifi.refaktor.changers.exceptions.RefaktorChangerException;
e411ecf3 14import no.uio.ifi.refaktor.prefix.Prefix;
e0fe6563 15import no.uio.ifi.refaktor.prefix.PrefixSet;
1d39ea7d 16import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
64970e7f 17
0b904053
EK
18import org.eclipse.jdt.core.dom.IVariableBinding;
19
fc35e1b0 20//TODO: rewrite docs
7a978020 21/**
520985c2
EK
22 * A property extractor that collects all the expression prefixes within a selection
23 * (see {@link PrefixesCollector}) that forms the base for calculating the
24 * candidates for the move refactoring, and also the unfixes
25 * that are non-candidates for the move refactoring.
26 *
27 * The set of prefixes that are not enclosing any unfixes is put in the set of safe prefixes.
28 * This set is used by an Extract and Move Method refactoring to find a suitable target
29 * for the Move Method.
30 *
31 * The class is typically used by the {@link ExtractAndMoveMethodChanger}.
7a978020 32 */
fc35e1b0 33public class ExtractAndMoveMethodAnalyzer implements Analyzer {
64970e7f 34
ee6a0b5b 35 private final CompilationUnitTextSelection selection;
05d2dee5
EK
36 private final PrefixesCollector prefixesCollector;
37 private final UnfixesCollector unfixesCollector;
64970e7f 38
fc35e1b0 39 public ExtractAndMoveMethodAnalyzer(CompilationUnitTextSelection selection) {
ee6a0b5b 40 this.selection = selection;
2f82d251
EK
41 prefixesCollector = new PrefixesCollector(selection);
42 unfixesCollector = new UnfixesCollector(selection);
64970e7f 43 }
74581229 44
95330826 45 public PrefixSet calculateSafePrefixes() {
2f82d251 46 return prefixesCollector.getPrefixes().minusEnclosedPrefixesFrom(unfixesCollector.getUnfixes());
087a79bf 47 }
c4d4c07b
EK
48
49 protected PrefixSet getPrefixes() {
50 return prefixesCollector.getPrefixes();
51 }
52
34328ca2 53 public PrefixSet getUnfixes() {
c4d4c07b
EK
54 return unfixesCollector.getUnfixes();
55 }
e411ecf3 56
e15b42b4
EK
57 public void checkPreconditions() throws RefaktorChangerException {
58 SelectionValidator.checkIfSelectionIsValid(selection);
59 analyze();
60 checkIfUsefulTargetFound();
61 }
62
63 private void checkIfUsefulTargetFound() {
64 if (!hasUsefulResults())
65 throw new NoTargetFoundException();
66 }
67
b0b82148 68 public boolean hasUsefulResults() {
e15b42b4
EK
69 return !calculateSafePrefixes().isEmpty();
70 }
71
72 @Override
73 public void analyze() {
74 CollectorManager.collectProperties(selection, prefixesCollector, unfixesCollector);
75 }
76
77 public IVariableBinding calculateOriginalTarget() {
78 return calculateMostFrequentPrefix().getVariableBindingOfFirstExpression();
79 }
80
34328ca2 81 public Prefix calculateMostFrequentPrefix() {
e15b42b4 82 LinkedList<Prefix> listOfSafePrefixes = calculateListOfSafePrefixes();
e411ecf3
EK
83 sortAscendingByCountAndLength(listOfSafePrefixes);
84 return listOfSafePrefixes.getLast();
85 }
86
e15b42b4 87 private LinkedList<Prefix> calculateListOfSafePrefixes() {
95330826 88 return calculateSafePrefixes().toList();
e411ecf3
EK
89 }
90
91 private void sortAscendingByCountAndLength(LinkedList<Prefix> values) {
92 Collections.sort(values, new Comparator<Prefix>() {
93 @Override
94 public int compare(Prefix p1, Prefix p2) {
95 if (p1.getCount() > p2.getCount()) {
96 return 1;
97 } else if (p1.getCount() < p2.getCount()) {
98 return -1;
99 } else if (p1.length() > p2.length()) {
100 return 1;
101 } else if (p1.length() < p2.length()) {
102 return -1;
103 }
104 return 0;
105 }
106 });
107 }
64970e7f 108}