]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/analyzers/StatementsCreator.java
Moving analyzers to no.uio.ifi.refaktor.analyze.analyzers
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / analyzers / StatementsCreator.java
CommitLineData
33b364ef 1package no.uio.ifi.refaktor.analyze.analyzers;
4f577e24
EK
2
3import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4import static org.hamcrest.CoreMatchers.notNullValue;
5
6import java.util.LinkedList;
7import java.util.List;
8
33b364ef 9import no.uio.ifi.refaktor.analyze.GenericAnalyzerException;
4f577e24
EK
10import no.uio.ifi.refaktor.utils.ParseUtils;
11
12import org.eclipse.jdt.core.IMethod;
13import org.eclipse.jdt.core.JavaModelException;
14import org.eclipse.jdt.core.dom.CompilationUnit;
15import org.eclipse.jdt.core.dom.MethodDeclaration;
16import org.eclipse.jdt.core.dom.Statement;
17import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
18
19@SuppressWarnings("restriction")
20class StatementsCreator {
21
22 private IMethod method;
23 private List<Statement> statements;
24
25 public StatementsCreator(IMethod method) {
26 this.method = method;
27 }
28
29 List<Statement> getStatements() {
30 if (statements == null)
31 createStatements();
32
33 return statements;
34 }
35
36 private void createStatements() {
37 statements = new LinkedList<Statement>();
38 MethodDeclaration methodDeclaration = createMethodDeclaration();
39 if (hasBody(methodDeclaration))
40 addStatementsFrom(methodDeclaration);
41 }
42
43 private MethodDeclaration createMethodDeclaration() {
44 try {
45 CompilationUnit compilationUnit = ParseUtils.parse(method.getCompilationUnit());
46 MethodDeclaration methodDeclaration = ASTNodeSearchUtil.getMethodDeclarationNode(method, compilationUnit);
47 assertThat(methodDeclaration, notNullValue());
48 return methodDeclaration;
49 } catch (JavaModelException e) {
50 throw new GenericAnalyzerException(e);
51 }
52 }
53
54 private boolean hasBody(MethodDeclaration methodDecl) {
55 return methodDecl.getBody() != null;
56 }
57
58 private void addStatementsFrom(MethodDeclaration methodDecl) {
59 for (Object element: methodDecl.getBody().statements())
60 statements.add((Statement) element);
61 }
62
63 public int getNumberOfStatements() {
64 if (statements == null)
65 return -1;
66
67 return statements.size();
68 }
69}