1 package no.uio.ifi.refaktor.analyze;
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.notNullValue;
6 import java.util.LinkedList;
9 import no.uio.ifi.refaktor.utils.ParseUtils;
11 import org.eclipse.jdt.core.IMethod;
12 import org.eclipse.jdt.core.JavaModelException;
13 import org.eclipse.jdt.core.dom.CompilationUnit;
14 import org.eclipse.jdt.core.dom.MethodDeclaration;
15 import org.eclipse.jdt.core.dom.Statement;
16 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
18 @SuppressWarnings("restriction")
19 class StatementsCreator {
21 private IMethod method;
22 private List<Statement> statements;
24 public StatementsCreator(IMethod method) {
28 List<Statement> getStatements() {
29 if (statements == null)
35 private void createStatements() {
36 statements = new LinkedList<Statement>();
37 MethodDeclaration methodDeclaration = createMethodDeclaration();
38 if (hasBody(methodDeclaration))
39 addStatementsFrom(methodDeclaration);
42 private MethodDeclaration createMethodDeclaration() {
44 CompilationUnit compilationUnit = ParseUtils.parse(method.getCompilationUnit());
45 MethodDeclaration methodDeclaration = ASTNodeSearchUtil.getMethodDeclarationNode(method, compilationUnit);
46 assertThat(methodDeclaration, notNullValue());
47 return methodDeclaration;
48 } catch (JavaModelException e) {
49 throw new GenericAnalyzerException(e);
53 private boolean hasBody(MethodDeclaration methodDecl) {
54 return methodDecl.getBody() != null;
57 private void addStatementsFrom(MethodDeclaration methodDecl) {
58 for (Object element: methodDecl.getBody().statements())
59 statements.add((Statement) element);
62 public int getNumberOfStatements() {
63 if (statements == null)
66 return statements.size();