1 package no.uio.ifi.refaktor.analyze.checkers;
3 import no.uio.ifi.refaktor.analyze.CollectorManager;
4 import no.uio.ifi.refaktor.analyze.collectors.PropertyCollector;
5 import no.uio.ifi.refaktor.analyze.exceptions.IllegalExpressionFoundException;
6 import no.uio.ifi.refaktor.analyze.exceptions.IllegalStatementFoundException;
7 import no.uio.ifi.refaktor.analyze.matchers.InstanceOfMatcher;
8 import no.uio.ifi.refaktor.analyze.matchers.LoopStatementMatcher;
9 import no.uio.ifi.refaktor.analyze.matchers.OrInstanceOfMatcher;
10 import no.uio.ifi.refaktor.analyze.matchers.SwitchStatementMatcher;
11 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
13 import org.eclipse.jdt.core.dom.ASTNode;
14 import org.eclipse.jdt.core.dom.ArrayAccess;
15 import org.eclipse.jdt.core.dom.Assignment;
16 import org.eclipse.jdt.core.dom.BreakStatement;
17 import org.eclipse.jdt.core.dom.ContinueStatement;
18 import org.eclipse.jdt.core.dom.Expression;
19 import org.eclipse.jdt.core.dom.IBinding;
20 import org.eclipse.jdt.core.dom.LabeledStatement;
21 import org.eclipse.jdt.core.dom.Modifier;
22 import org.eclipse.jdt.core.dom.QualifiedName;
23 import org.eclipse.jdt.core.dom.SimpleName;
24 import org.eclipse.jdt.core.dom.Statement;
25 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
26 import org.eclipse.jdt.core.dom.SuperFieldAccess;
27 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
29 public class IllegalStatementsChecker extends PropertyCollector implements Checker {
31 public IllegalStatementsChecker(CompilationUnitTextSelection selection) {
36 public void check() throws IllegalStatementFoundException, IllegalExpressionFoundException {
37 CollectorManager.collectProperties(selection, this);
41 public void clearData() {
45 public boolean visit(SuperConstructorInvocation node) {
46 if (!nodeInSelection(node))
49 throw new IllegalStatementFoundException(node);
53 public boolean visit(SuperMethodInvocation node) {
54 if (!nodeInSelection(node))
57 throw new IllegalExpressionFoundException(node);
61 public boolean visit(SuperFieldAccess node) {
62 if (!nodeInSelection(node))
65 throw new IllegalExpressionFoundException(node);
69 public boolean visit(BreakStatement node) {
70 if (!nodeInSelection(node))
73 if (node.getLabel() != null) {
74 checkThatCorrespondingLabelStatementIsInSelection(node, node.getLabel());
76 checkThatInnermostMatchingStatementIsInSelection(node, new OrInstanceOfMatcher(new LoopStatementMatcher(), new SwitchStatementMatcher()));
83 public boolean visit(ContinueStatement node) {
84 if (!nodeInSelection(node))
87 if (node.getLabel() != null) {
88 checkThatCorrespondingLabelStatementIsInSelection(node, node.getLabel());
90 checkThatInnermostMatchingStatementIsInSelection(node, new LoopStatementMatcher());
96 private void checkThatCorrespondingLabelStatementIsInSelection(ASTNode node, SimpleName nodeLabel) {
97 ASTNode parent = node.getParent();
99 while (!parentMatchesLabel(parent, nodeLabel) && parent != node.getRoot())
100 parent = parent.getParent();
102 if (parent == node.getRoot() || !nodeInSelection(parent))
103 throw new IllegalStatementFoundException(node);
106 private boolean parentMatchesLabel(ASTNode parent, SimpleName nodeLabel) {
107 return parent instanceof LabeledStatement && labelsMatches((LabeledStatement) parent, nodeLabel);
110 private boolean labelsMatches(LabeledStatement parentLabelStatement, SimpleName nodeLabel) {
111 return parentLabelStatement.getLabel().getIdentifier().equals(nodeLabel.getIdentifier());
114 private void checkThatInnermostMatchingStatementIsInSelection(ASTNode node, InstanceOfMatcher matcher) {
115 ASTNode parent = node.getParent();
117 while (!matcher.matches(parent) && parent != node.getRoot())
118 parent = parent.getParent();
120 if (parent == node.getRoot())
121 throw new IllegalStatementFoundException(node);
123 assert parent instanceof Statement;
125 if (!nodeInSelection(parent))
126 throw new IllegalStatementFoundException(node);
130 public boolean visit(Assignment node) {
131 if (!nodeInSelection(node))
134 if (assignmentIsLegal(node))
137 throw new IllegalStatementFoundException(node);
140 private boolean assignmentIsLegal(Assignment node) {
141 Expression lhs = node.getLeftHandSide();
142 if (lhs instanceof SimpleName) {
143 SimpleName simpleName = (SimpleName) lhs;
144 IBinding binding = simpleName.resolveBinding();
145 assert binding != null;
147 return !Modifier.isFinal(binding.getModifiers());
148 } else if (lhs instanceof QualifiedName || lhs instanceof ArrayAccess) {