1 package no.uio.ifi.refaktor.analyze;
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.LinkedList;
7 import no.uio.ifi.refaktor.analyze.collectors.PrefixesCollector;
8 import no.uio.ifi.refaktor.analyze.collectors.SelectionValidator;
9 import no.uio.ifi.refaktor.analyze.collectors.UnfixesCollector;
10 import no.uio.ifi.refaktor.changers.ExtractAndMoveMethodChanger;
11 import no.uio.ifi.refaktor.changers.RefaktorChangerException;
12 import no.uio.ifi.refaktor.prefix.Prefix;
13 import no.uio.ifi.refaktor.prefix.PrefixSet;
14 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
18 * A property extractor that collects all the expression prefixes within a selection
19 * (see {@link PrefixesCollector}) that forms the base for calculating the
20 * candidates for the move refactoring, and also the unfixes
21 * that are non-candidates for the move refactoring.
23 * The set of prefixes that are not enclosing any unfixes is put in the set of safe prefixes.
24 * This set is used by an Extract and Move Method refactoring to find a suitable target
25 * for the Move Method.
27 * The class is typically used by the {@link ExtractAndMoveMethodChanger}.
29 public class ExtractAndMoveMethodAnalyzer implements Analyzer {
31 private final CompilationUnitTextSelection selection;
32 private final PrefixesCollector prefixesCollector;
33 private final UnfixesCollector unfixesCollector;
35 public ExtractAndMoveMethodAnalyzer(CompilationUnitTextSelection selection) {
36 this.selection = selection;
37 prefixesCollector = new PrefixesCollector(selection);
38 unfixesCollector = new UnfixesCollector(selection);
41 public PrefixSet calculateSafePrefixes() {
42 return prefixesCollector.getPrefixes().minusEnclosedPrefixesFrom(unfixesCollector.getUnfixes());
45 protected PrefixSet getPrefixes() {
46 return prefixesCollector.getPrefixes();
49 protected PrefixSet getUnfixes() {
50 return unfixesCollector.getUnfixes();
53 public Prefix getMostFrequentPrefix() {
54 LinkedList<Prefix> listOfSafePrefixes = getListOfSafePrefixes();
55 sortAscendingByCountAndLength(listOfSafePrefixes);
56 return listOfSafePrefixes.getLast();
59 private LinkedList<Prefix> getListOfSafePrefixes() {
60 return calculateSafePrefixes().toList();
63 private void sortAscendingByCountAndLength(LinkedList<Prefix> values) {
64 Collections.sort(values, new Comparator<Prefix>() {
66 public int compare(Prefix p1, Prefix p2) {
67 if (p1.getCount() > p2.getCount()) {
69 } else if (p1.getCount() < p2.getCount()) {
71 } else if (p1.length() > p2.length()) {
73 } else if (p1.length() < p2.length()) {
81 public void checkPreconditions() throws RefaktorChangerException {
82 SelectionValidator.checkIfSelectionIsValid(selection);
84 checkIfUsefulTargetFound();
87 private void checkIfUsefulTargetFound() {
88 if (!hasUsefulResults())
89 throw new NoTargetFoundException();
92 private boolean hasUsefulResults() {
93 return !calculateSafePrefixes().isEmpty();
97 public void analyze() {
98 CollectorManager.collectProperties(selection, prefixesCollector, unfixesCollector);