]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/checkers/AmbiguousReturnValueChecker.java
0f966cb8fa8a0e50810b4a356fbd34c77d510112
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / checkers / AmbiguousReturnValueChecker.java
1 package no.uio.ifi.refaktor.analyze.checkers;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import no.uio.ifi.refaktor.analyze.CollectorManager;
7 import no.uio.ifi.refaktor.analyze.collectors.PropertyCollector;
8 import no.uio.ifi.refaktor.analyze.exceptions.IllegalExpressionFoundException;
9 import no.uio.ifi.refaktor.analyze.exceptions.IllegalStatementFoundException;
10 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
11
12 import org.eclipse.jdt.core.dom.ASTNode;
13 import org.eclipse.jdt.core.dom.Assignment;
14 import org.eclipse.jdt.core.dom.IBinding;
15 import org.eclipse.jdt.core.dom.IVariableBinding;
16 import org.eclipse.jdt.core.dom.ReturnStatement;
17 import org.eclipse.jdt.core.dom.SimpleName;
18 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
19
20 public class AmbiguousReturnValueChecker extends PropertyCollector implements Checker {
21
22         private class AssigneesUsedOutsideSelectionChecker extends PropertyCollector {
23                 
24                 private final Set<String> referrals;
25
26                 public AssigneesUsedOutsideSelectionChecker(CompilationUnitTextSelection selection) {
27                         super(selection);
28                         referrals = new HashSet<String>();
29                 }
30
31                 @Override
32                 public void clearData() {
33                         referrals.clear();
34                 }
35
36                 @Override
37                 public boolean visit(SimpleName node) {
38                         if (nodeInSelectionOrBefore(node))
39                                 return false;
40
41                         IBinding binding = node.resolveBinding();
42
43                         if (binding instanceof IVariableBinding && assigneesBindingKeys.contains(binding.getKey())) {
44                                 referrals.add(binding.getKey());
45                                 if (selectionContainsReturn || referrals.size() > 1)
46                                         throw new IllegalStatementFoundException(SimpleName.class);
47                         }
48                         
49                         return true;
50                 }
51
52                 private boolean nodeInSelectionOrBefore(ASTNode node) {
53                         return nodeInSelection(node) || nodeBeforeSelection(node);
54                 }
55                 
56                 private boolean nodeBeforeSelection(ASTNode node) {
57                         int nodeEndPosition = node.getStartPosition() + node.getLength();
58                         return node.getStartPosition() < selection.getOffset() && nodeEndPosition < selection.getOffset();
59                 }
60
61         }
62
63         private final Set<String> assigneesBindingKeys;
64         private boolean selectionContainsReturn;
65
66         public AmbiguousReturnValueChecker(CompilationUnitTextSelection selection) {
67                 super(selection);
68                 assigneesBindingKeys = new HashSet<String>();
69                 selectionContainsReturn = false;
70         }
71
72         @Override
73         public void check() throws IllegalStatementFoundException, IllegalExpressionFoundException {
74                 CollectorManager.collectProperties(selection, this);
75                 CollectorManager.collectPropertiesFromEnclosingMethod(selection, new AssigneesUsedOutsideSelectionChecker(selection));
76         }
77
78         @Override
79         public void clearData() {
80                 assigneesBindingKeys.clear();
81                 selectionContainsReturn = false;
82         }
83
84         @Override
85         public boolean visit(Assignment node) {
86                 if (!nodeInSelection(node))
87                         return false;
88
89                 if (node.getLeftHandSide() instanceof SimpleName) {
90                         SimpleName name = (SimpleName) node.getLeftHandSide();
91                         IBinding binding = name.resolveBinding();
92                         assert binding instanceof IVariableBinding;
93                         if (!((IVariableBinding) binding).isField())
94                                 assigneesBindingKeys.add(binding.getKey());
95                 }
96
97                 return true;
98         }
99
100         @Override
101         public boolean visit(VariableDeclarationFragment node) {
102                 if (!nodeInSelection(node))
103                         return false;
104
105                 IBinding binding = node.getName().resolveBinding();
106                 assert binding instanceof IVariableBinding;
107                 assigneesBindingKeys.add(binding.getKey());
108
109                 return true;
110         }
111         
112         @Override
113         public boolean visit(ReturnStatement node) {
114                 if (!nodeInSelection(node))
115                         return false;
116
117                 selectionContainsReturn = true;
118                 return true;
119         }
120 }
121