Skip to content

Commit a56f2fd

Browse files
authored
Merge pull request #733 from kusumotolab/fix-null-conditio
For文の条件式が空の時に落ちるバグを修正
2 parents 3566087 + 8774671 commit a56f2fd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/main/java/jp/kusumotolab/kgenprog/project/jdt/StatementAndConditionVisitor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.eclipse.jdt.core.dom.DoStatement;
99
import org.eclipse.jdt.core.dom.EmptyStatement;
1010
import org.eclipse.jdt.core.dom.EnhancedForStatement;
11+
import org.eclipse.jdt.core.dom.Expression;
1112
import org.eclipse.jdt.core.dom.ExpressionStatement;
1213
import org.eclipse.jdt.core.dom.ForStatement;
1314
import org.eclipse.jdt.core.dom.IfStatement;
@@ -91,7 +92,10 @@ public boolean visit(ForStatement node) {
9192

9293
@Override
9394
public void endVisit(final ForStatement node) {
94-
consumeElement(node.getExpression()); // Blockよりも後に入れるためにendVisitで行う
95+
final Expression expression = node.getExpression();
96+
if (null != expression) {
97+
consumeElement(expression); // Blockよりも後に入れるためにendVisitで行う
98+
}
9599
super.endVisit(node);
96100
}
97101

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package jp.kusumotolab.kgenprog.project.jdt;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
import org.eclipse.jdt.core.dom.ASTNode;
7+
import org.eclipse.jdt.core.dom.Block;
8+
import org.junit.Test;
9+
import jp.kusumotolab.kgenprog.project.ProductSourcePath;
10+
11+
public class StatementAndConditionVisitorTest {
12+
13+
private static final String FILE_NAME_FOR = "For.java";
14+
private static final String SOURCE_FOR = new StringBuilder()
15+
// Line breaks must be included to execute GeneratedJDTAST#inferLocation.
16+
.append("public class For {\n")
17+
.append(" public int method() {\n")
18+
.append(" for (int i = 0; ; i++) {\n")
19+
.append(" if(i == 10) {\n")
20+
.append(" break;\n")
21+
.append(" }\n")
22+
.append(" }\n")
23+
.append(" return i;\n")
24+
.append(" }\n")
25+
.append("}\n")
26+
.toString();
27+
28+
@Test
29+
public void test_consumeStatement01() {
30+
31+
// generating an AST
32+
// ASTを作成
33+
final ProductSourcePath productSourcePath =
34+
new ProductSourcePath(Paths.get("."), Paths.get(FILE_NAME_FOR));
35+
final JDTASTConstruction constructor = new JDTASTConstruction();
36+
final GeneratedJDTAST<ProductSourcePath> ast = constructor.constructAST(
37+
productSourcePath, SOURCE_FOR);
38+
39+
// assuming that any instance of Block is not included in statements
40+
// Blockが含まれていないはず
41+
final StatementAndConditionVisitor visitor = new StatementAndConditionVisitor();
42+
visitor.analyzeElements(ast.getRoot());
43+
final List<ASTNode> statements = visitor.getElements();
44+
assertThat(statements).noneMatch(s -> Block.class == s.getClass());
45+
}
46+
}
47+

0 commit comments

Comments
 (0)