]>
Commit | Line | Data |
---|---|---|
5748b416 EK |
1 | package no.uio.ifi.refaktor.utils; |
2 | ||
3 | import org.eclipse.jdt.core.dom.ASTNode; | |
4 | import org.eclipse.jdt.core.dom.ASTVisitor; | |
5 | import org.eclipse.jdt.core.dom.Statement; | |
6 | ||
7 | public class LastStatementFinder extends ASTVisitor { | |
8 | private final ASTNode methodDeclarationBlock; | |
9 | private Statement lastStatement; | |
10 | ||
11 | public LastStatementFinder(ASTNode methodDeclarationBlock) { | |
12 | this.methodDeclarationBlock = methodDeclarationBlock; | |
13 | } | |
14 | ||
15 | @Override | |
16 | public boolean preVisit2(ASTNode node) { | |
17 | if (node == methodDeclarationBlock) | |
18 | return true; | |
19 | ||
20 | if (node instanceof Statement && isPlacedAfterLastStatement(node)) { | |
21 | lastStatement = (Statement) node; | |
22 | } | |
23 | ||
24 | return false; | |
25 | } | |
26 | ||
27 | private boolean isPlacedAfterLastStatement(ASTNode node) { | |
28 | if (lastStatement == null) | |
29 | return true; | |
30 | ||
31 | int nodeEndPosition = node.getStartPosition() + node.getLength(); | |
32 | int lastStatementEndPosition = lastStatement.getStartPosition() + lastStatement.getLength(); | |
33 | return nodeEndPosition > lastStatementEndPosition; | |
34 | } | |
35 | ||
36 | public Statement getLastStatement() { | |
37 | return lastStatement; | |
38 | } | |
39 | } |