]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/StatementsCreator.java
Adding command + handler for analyzing projects.
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / StatementsCreator.java
1 package no.uio.ifi.refaktor.analyze;
2
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.notNullValue;
5
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import no.uio.ifi.refaktor.utils.ParseUtils;
10
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;
17
18 @SuppressWarnings("restriction")
19 class StatementsCreator {
20
21         private IMethod method;
22         private List<Statement> statements;
23
24         public StatementsCreator(IMethod method) {
25                 this.method = method;
26         }
27
28         List<Statement> getStatements() {
29                 if (statements == null) 
30                         createStatements();
31
32                 return statements;
33         }
34
35         private void createStatements() {
36                 statements = new LinkedList<Statement>();
37                 MethodDeclaration methodDeclaration = createMethodDeclaration();
38                 if (hasBody(methodDeclaration))
39                         addStatementsFrom(methodDeclaration);
40         }
41
42         private MethodDeclaration createMethodDeclaration() {
43                 try {
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);
50                 }
51         }
52
53         private boolean hasBody(MethodDeclaration methodDecl) {
54                 return methodDecl.getBody() != null;
55         }
56
57         private void addStatementsFrom(MethodDeclaration methodDecl) {
58                 for (Object element: methodDecl.getBody().statements())
59                         statements.add((Statement) element);
60         }
61
62         public int getNumberOfStatements() {
63                 if (statements == null)
64                         return -1;
65
66                 return statements.size();
67         }
68 }