]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/collectors/UnfixesCollector.java
83538c43310e1f06cd6b5493f4638fe1bb26ee54
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / collectors / UnfixesCollector.java
1 package no.uio.ifi.refaktor.analyze.collectors;
2
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
5 import no.uio.ifi.refaktor.prefix.Prefix;
6 import no.uio.ifi.refaktor.prefix.PrefixSet;
7 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
8 import no.uio.ifi.refaktor.utils.RefaktorDebug;
9
10 import org.eclipse.jdt.core.dom.ArrayAccess;
11 import org.eclipse.jdt.core.dom.Assignment;
12 import org.eclipse.jdt.core.dom.Expression;
13 import org.eclipse.jdt.core.dom.FieldAccess;
14 import org.eclipse.jdt.core.dom.Name;
15 import org.eclipse.jdt.core.dom.SimpleName;
16 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
17
18 public class UnfixesCollector extends PropertyCollector {
19         private final PrefixSet unfixes;
20
21         public UnfixesCollector(CompilationUnitTextSelection selection) {
22                 super(selection);
23                 this.unfixes = new PrefixSet();
24         }
25
26         public PrefixSet getUnfixes() {
27                 return unfixes;
28         }
29
30         @Override
31         public boolean visit(VariableDeclarationFragment node) {
32                 if (!nodeInSelection(node))
33                         return false;
34
35                 addSimpleNameToUnfixes(node.getName());
36                 return false;
37         }
38
39         @Override
40         public boolean visit(Assignment node) {
41                 if (!nodeInSelection(node))
42                         return false;
43
44                 addLeftHandSideToUnfixesIfSimpleName(node);
45                 return true;
46         }
47
48         private void addLeftHandSideToUnfixesIfSimpleName(Assignment node) {
49                 Expression lhs = node.getLeftHandSide();
50                 if (lhs instanceof ArrayAccess)
51                         return;
52
53                 if (lhs instanceof FieldAccess) {
54                         addSimpleNameToUnfixes(((FieldAccess) lhs).getName());
55                 } else {
56                         assertThat(lhs, instanceOf(Name.class));
57                         Name name = (Name) lhs;
58                         if (name.isSimpleName())
59                                 addSimpleNameToUnfixes((SimpleName) name);
60                 }
61         }
62
63         private void addSimpleNameToUnfixes(SimpleName simpleName) {
64                 Prefix p = new Prefix(simpleName);
65                 RefaktorDebug.println("Unfix string: " + p.toString());
66                 unfixes.add(p);
67         }
68
69         @Override
70         public void clearData() {
71                 unfixes.clear();
72         }
73 }