]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/refaktor-before/src/no/uio/ifi/refaktor/analyze/collectors/AbstractPrefixCollector.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / analyze / collectors / AbstractPrefixCollector.java
1 package no.uio.ifi.refaktor.analyze.collectors;
2
3 import no.uio.ifi.refaktor.prefix.Prefix;
4 import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection;
5
6 import org.eclipse.jdt.core.dom.IVariableBinding;
7 import org.eclipse.jdt.core.dom.QualifiedName;
8 import org.eclipse.jdt.core.dom.SimpleName;
9
10 /**
11  * An abstract property collector responsible for finding
12  * prefixes within a selection.
13  * 
14  * Subclasses are responsible for implementing the 
15  * {@link #registerPrefix(Prefix)} method. I.e. they
16  * decide how to handle the encountered prefixes.
17  */
18 public abstract class AbstractPrefixCollector extends PropertyCollector {
19
20         public AbstractPrefixCollector(CompilationUnitTextSelection selection) {
21                 super(selection);
22         }
23
24         /**
25          * Register a prefix encountered by the collector.
26          * @param prefix The prefix to register.
27          */
28         protected abstract void registerPrefix(Prefix prefix);
29
30         @Override
31         public boolean visit(QualifiedName node) {
32                 if (!nodeInSelection(node))
33                         return false;
34
35                 Prefix prefix = new Prefix(node);
36                 if (prefix.firstExpressionIsBoundToVariable())
37                         registerPrefix(prefix);
38
39                 // Must be false. Important to prevent double count for simple names.
40                 return false;
41         }
42
43         @Override
44         public boolean visit(SimpleName node) {
45                 if (!nodeInSelection(node))
46                         return false;
47
48                 if (nodeIsUsageOfVariable(node))
49                         registerPrefix(new Prefix(node));
50
51                 return false;
52         }
53
54         private boolean nodeIsUsageOfVariable(SimpleName node) {
55                 return !node.isDeclaration() && node.resolveBinding() instanceof IVariableBinding;
56         }
57 }