]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/refaktor-before/src/no/uio/ifi/refaktor/analyze/analyzers/StatementListsCreator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / analyze / analyzers / StatementListsCreator.java
CommitLineData
1b2798f6
EK
1package no.uio.ifi.refaktor.analyze.analyzers;
2
3import java.util.Arrays;
4import java.util.Collection;
5import java.util.LinkedList;
6import java.util.List;
7
8import no.uio.ifi.refaktor.analyze.exceptions.GenericAnalyzerException;
9import no.uio.ifi.refaktor.utils.ParseUtils;
10import no.uio.ifi.refaktor.utils.RefaktorASTNodeSearchUtil;
11
12import org.eclipse.jdt.core.IMethod;
13import org.eclipse.jdt.core.JavaModelException;
14import org.eclipse.jdt.core.dom.ASTVisitor;
15import org.eclipse.jdt.core.dom.Block;
16import org.eclipse.jdt.core.dom.CompilationUnit;
17import org.eclipse.jdt.core.dom.DoStatement;
18import org.eclipse.jdt.core.dom.EnhancedForStatement;
19import org.eclipse.jdt.core.dom.ForStatement;
20import org.eclipse.jdt.core.dom.IfStatement;
21import org.eclipse.jdt.core.dom.LabeledStatement;
22import org.eclipse.jdt.core.dom.MethodDeclaration;
23import org.eclipse.jdt.core.dom.Statement;
24import org.eclipse.jdt.core.dom.SwitchCase;
25import org.eclipse.jdt.core.dom.SwitchStatement;
26import org.eclipse.jdt.core.dom.WhileStatement;
27
28public class StatementListsCreator extends ASTVisitor {
29
30 private final IMethod method;
31 private final Collection<List<Statement>> statementLists;
32 private MethodDeclaration methodDeclaration;
33
34 public StatementListsCreator(IMethod method) {
35 this.method = method;
36 statementLists = new LinkedList<List<Statement>>();
37 }
38
39 Collection<List<Statement>> getStatementLists() {
40 createStatements();
41 return statementLists;
42 }
43
44 private void createStatements() {
45 clearStatementLists();
46 createMethodDeclaration();
47 addStatementListsFromMethodDeclaration();
48 }
49
50 private void clearStatementLists() {
51 statementLists.clear();
52 }
53
54 private void createMethodDeclaration() {
55 try {
56 methodDeclaration = getMethodDeclarationNode(method);
57 assert methodDeclaration != null;
58 } catch (JavaModelException e) {
59 throw new GenericAnalyzerException(e);
60 }
61 }
62
63 public static MethodDeclaration getMethodDeclarationNode(IMethod method) throws JavaModelException {
64 CompilationUnit compilationUnit = ParseUtils.parse(method.getCompilationUnit());
65 return RefaktorASTNodeSearchUtil.getMethodDeclarationNode(method, compilationUnit);
66 }
67
68 private void addStatementListsFromMethodDeclaration() {
69 methodDeclaration.accept(this);
70 }
71
72 public int getNumberOfStatements() {
73 int size = 0;
74 for (List<Statement> list: statementLists)
75 size += list.size();
76 return size;
77 }
78
79 @Override
80 public boolean visit(Block node) {
81 statementLists.add(new LinkedList<Statement>(node.statements()));
82 return true;
83 }
84
85 @Override
86 public boolean visit(DoStatement node) {
87 addIfNotBlockOrNull(node.getBody());
88 return true;
89 }
90
91 @Override
92 public boolean visit(EnhancedForStatement node) {
93 addIfNotBlockOrNull(node.getBody());
94 return true;
95 }
96
97 @Override
98 public boolean visit(ForStatement node) {
99 addIfNotBlockOrNull(node.getBody());
100 return true;
101 }
102
103 @Override
104 public boolean visit(IfStatement node) {
105 addIfNotBlockOrNull(node.getThenStatement());
106 addIfNotBlockOrNull(node.getElseStatement());
107 return true;
108 }
109
110 @Override
111 public boolean visit(LabeledStatement node) {
112 addIfNotBlockOrNull(node.getBody());
113 return true;
114 }
115
116 @Override
117 public boolean visit(SwitchStatement node) {
118 List<Statement> statementList = new LinkedList<Statement>();
119
120 for (Object stmtObject: node.statements()) {
121 assert stmtObject instanceof Statement;
122 Statement statement = (Statement) stmtObject;
123
124 if (statement instanceof SwitchCase) {
125 if (!statementList.isEmpty()) {
126 statementLists.add(statementList);
127 statementList = new LinkedList<Statement>();
128 }
129 } else {
130 statementList.add(statement);
131 }
132 }
133
134 if (!statementList.isEmpty())
135 statementLists.add(statementList);
136
137 return true;
138 }
139
140 @Override
141 public boolean visit(WhileStatement node) {
142 addIfNotBlockOrNull(node.getBody());
143 return true;
144 }
145
146 private void addIfNotBlockOrNull(Statement body) {
147 if (!(body instanceof Block) && body != null)
148 statementLists.add(new LinkedList<Statement>(Arrays.asList(body)));
149 }
150
151}