]>
Commit | Line | Data |
---|---|---|
33b364ef | 1 | package no.uio.ifi.refaktor.analyze.analyzers; |
4f577e24 EK |
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 | ||
33b364ef | 9 | import no.uio.ifi.refaktor.analyze.GenericAnalyzerException; |
4f577e24 EK |
10 | import no.uio.ifi.refaktor.utils.ParseUtils; |
11 | ||
12 | import org.eclipse.jdt.core.IMethod; | |
13 | import org.eclipse.jdt.core.JavaModelException; | |
14 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
15 | import org.eclipse.jdt.core.dom.MethodDeclaration; | |
16 | import org.eclipse.jdt.core.dom.Statement; | |
17 | import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil; | |
18 | ||
19 | @SuppressWarnings("restriction") | |
20 | class 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 | } |