]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/collectors/EveryConditionalBranchEndsInReturnCheckerCollector.java
LegalStatementsChecker: refactoring
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / collectors / EveryConditionalBranchEndsInReturnCheckerCollector.java
1 package no.uio.ifi.refaktor.analyze.collectors;
2
3 import no.uio.ifi.refaktor.analyze.CollectorManager;
4 import no.uio.ifi.refaktor.analyze.ReturnStatementFinder;
5 import no.uio.ifi.refaktor.analyze.analyzers.Checker;
6 import no.uio.ifi.refaktor.analyze.analyzers.IllegalExpressionFoundException;
7 import no.uio.ifi.refaktor.analyze.analyzers.IllegalStatementFoundException;
8 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
9
10 import org.eclipse.jdt.core.dom.IfStatement;
11 import org.eclipse.jdt.core.dom.ReturnStatement;
12 import org.eclipse.jdt.core.dom.Statement;
13 import org.eclipse.jdt.core.dom.SwitchStatement;
14
15 public class EveryConditionalBranchEndsInReturnCheckerCollector extends PropertyCollector implements Checker {
16
17         private CompilationUnitTextSelection privateSelection;
18
19         public EveryConditionalBranchEndsInReturnCheckerCollector(CompilationUnitTextSelection selection) {
20                 super(selection);
21                 privateSelection = selection;
22         }
23
24         @Override
25         public void clearData() {
26         }
27
28         @Override
29         public boolean visit(IfStatement node) {
30                 if (!nodeInSelection(node))
31                         return false;
32                 
33                 ReturnStatementFinder returnStatementFinder = new ReturnStatementFinder();
34                 node.getThenStatement().accept(returnStatementFinder);
35
36                 if (returnStatementFinder.hasFoundReturnStatement()) {
37                         Statement elseStatement = node.getElseStatement();
38
39                         if (elseStatement == null) {
40                                 throw new IllegalStatementFoundException(ReturnStatement.class);
41                         }
42
43                         returnStatementFinder.reset();
44                         elseStatement.accept(returnStatementFinder);
45
46                         if (!returnStatementFinder.hasFoundReturnStatement()) {
47                                 throw new IllegalStatementFoundException(ReturnStatement.class);
48                         }
49                 }
50
51                 return true;
52         }
53         
54         @Override
55         public boolean visit(SwitchStatement node) {
56                 // TODO: Handle switch statements in another way?
57                 if (!nodeInSelection(node))
58                         return false;
59                 
60                 node.accept(new IllegalReturnStatementChecker());
61                 
62                 return false;
63         }
64
65         @Override
66         public void check() throws IllegalStatementFoundException, IllegalExpressionFoundException {
67                 CollectorManager.collectProperties(privateSelection, this);
68         }
69 }