]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/refaktor-before/src/no/uio/ifi/refaktor/analyze/collectors/LastStatementCollector.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / analyze / collectors / LastStatementCollector.java
CommitLineData
1b2798f6
EK
1package no.uio.ifi.refaktor.analyze.collectors;
2
3import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection;
4
5import org.eclipse.jdt.core.dom.ASTNode;
6import org.eclipse.jdt.core.dom.Statement;
7
8public class LastStatementCollector extends PropertyCollector {
9 private final ASTNode coveringNode;
10 private Statement lastStatement;
11
12 public LastStatementCollector(CompilationUnitTextSelection selection, ASTNode coveringNode) {
13 super(selection);
14 this.coveringNode = coveringNode;
15 }
16
17 @Override
18 public boolean preVisit2(ASTNode node) {
19 if (node == coveringNode)
20 return true;
21
22 if (!nodeInSelection(node))
23 return false;
24
25 if (node instanceof Statement && isPlacedAfterLastStatement(node)) {
26 lastStatement = (Statement) node;
27 }
28
29 // only visit the statements on the top level
30 return false;
31 }
32
33 private boolean isPlacedAfterLastStatement(ASTNode node) {
34 if (lastStatement == null)
35 return true;
36
37 int nodeEndPosition = node.getStartPosition() + node.getLength();
38 int lastStatementEndPosition = lastStatement.getStartPosition() + lastStatement.getLength();
39 return nodeEndPosition > lastStatementEndPosition;
40 }
41
42 public Statement getLastStatement() {
43 return lastStatement;
44 }
45
46 @Override
47 public void clearData() {
48 lastStatement = null;
49 }
50}