1 package no.uio.ifi.refaktor.analyze.checkers;
3 import java.util.HashSet;
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.textselection.CompilationUnitTextSelection;
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;
20 class AmbiguousReturnValueChecker extends PropertyCollector implements Checker {
22 private class AssigneesUsedOutsideSelectionChecker extends PropertyCollector {
24 private final Set<String> referrals;
26 public AssigneesUsedOutsideSelectionChecker(CompilationUnitTextSelection selection) {
28 referrals = new HashSet<String>();
32 public void clearData() {
37 public boolean visit(SimpleName node) {
38 if (nodeInSelectionOrBefore(node))
41 IBinding binding = node.resolveBinding();
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);
52 private boolean nodeInSelectionOrBefore(ASTNode node) {
53 return nodeInSelection(node) || nodeBeforeSelection(node);
56 private boolean nodeBeforeSelection(ASTNode node) {
57 int nodeEndPosition = node.getStartPosition() + node.getLength();
58 return node.getStartPosition() < selection.getOffset() && nodeEndPosition < selection.getOffset();
63 private final Set<String> assigneesBindingKeys;
64 private boolean selectionContainsReturn;
66 public AmbiguousReturnValueChecker(CompilationUnitTextSelection selection) {
68 assigneesBindingKeys = new HashSet<String>();
69 selectionContainsReturn = false;
73 public void check() throws IllegalStatementFoundException, IllegalExpressionFoundException {
74 CollectorManager.collectProperties(selection, this);
75 CollectorManager.collectPropertiesFromEnclosingMethod(selection, new AssigneesUsedOutsideSelectionChecker(selection));
79 public void clearData() {
80 assigneesBindingKeys.clear();
81 selectionContainsReturn = false;
85 public boolean visit(Assignment node) {
86 if (!nodeInSelection(node))
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());
101 public boolean visit(VariableDeclarationFragment node) {
102 if (!nodeInSelection(node))
105 IBinding binding = node.getName().resolveBinding();
106 assert binding instanceof IVariableBinding;
107 assigneesBindingKeys.add(binding.getKey());
113 public boolean visit(ReturnStatement node) {
114 if (!nodeInSelection(node))
117 selectionContainsReturn = true;