]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | package no.uio.ifi.refaktor.analyze.analyzers; |
2 | ||
3 | import java.util.Arrays; | |
4 | import java.util.Collection; | |
5 | import java.util.LinkedList; | |
6 | import java.util.List; | |
7 | ||
8 | import no.uio.ifi.refaktor.analyze.exceptions.GenericAnalyzerException; | |
9 | import no.uio.ifi.refaktor.utils.ParseUtils; | |
10 | import no.uio.ifi.refaktor.utils.RefaktorASTNodeSearchUtil; | |
11 | ||
12 | import org.eclipse.jdt.core.IMethod; | |
13 | import org.eclipse.jdt.core.JavaModelException; | |
14 | import org.eclipse.jdt.core.dom.ASTVisitor; | |
15 | import org.eclipse.jdt.core.dom.Block; | |
16 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
17 | import org.eclipse.jdt.core.dom.DoStatement; | |
18 | import org.eclipse.jdt.core.dom.EnhancedForStatement; | |
19 | import org.eclipse.jdt.core.dom.ForStatement; | |
20 | import org.eclipse.jdt.core.dom.IfStatement; | |
21 | import org.eclipse.jdt.core.dom.LabeledStatement; | |
22 | import org.eclipse.jdt.core.dom.MethodDeclaration; | |
23 | import org.eclipse.jdt.core.dom.Statement; | |
24 | import org.eclipse.jdt.core.dom.SwitchCase; | |
25 | import org.eclipse.jdt.core.dom.SwitchStatement; | |
26 | import org.eclipse.jdt.core.dom.WhileStatement; | |
27 | ||
28 | public 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 | } |