Skip to content

Commit

Permalink
fix: fix incorrect model for empty for loop (#3108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor18 authored and monperrus committed Sep 19, 2019
1 parent 408d76d commit eddd948
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/ParentExiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public void visitCtFor(CtFor forLoop) {
} else if (isContainedInForUpdate() && child instanceof CtStatement) {
forLoop.addForUpdate((CtStatement) child);
return;
} else if (forLoop.getExpression() == null && child instanceof CtExpression) {
} else if (isContainedInForCondition() && child instanceof CtExpression) {
forLoop.setExpression((CtExpression<Boolean>) child);
return;
}
Expand Down Expand Up @@ -582,6 +582,14 @@ private boolean isContainedInForUpdate() {
return false;
}

private boolean isContainedInForCondition() {
if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ForStatement)) {
return false;
}
final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().stack.peek().node;
return parent.condition != null && parent.condition.equals(childJDT);
}

@Override
public void visitCtForEach(CtForEach foreach) {
if (foreach.getVariable() == null && child instanceof CtVariable) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/spoon/test/loop/LoopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package spoon.test.loop;

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtFor;
import spoon.reflect.code.CtForEach;
Expand All @@ -32,6 +34,7 @@
import java.io.File;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

Expand Down Expand Up @@ -70,4 +73,16 @@ public void testForeachShouldHaveAlwaysABlockInItsBody() throws Exception {

assertEquals(expectedDebug, ctLoop.toString());
}

@Test
public void testEmptyForLoopExpression() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/loop/testclasses/EmptyLoops.java");
CtModel model = launcher.buildModel();
CtFor ctFor = model.getElements(new TypeFilter<>(CtFor.class)).get(0);
assertTrue(ctFor.getForInit().isEmpty());
assertNull(ctFor.getExpression());
assertTrue(ctFor.getForUpdate().isEmpty());
assertEquals("x = 5", ((CtBlock)ctFor.getBody()).getStatement(0).toString().trim());
}
}
8 changes: 8 additions & 0 deletions src/test/java/spoon/test/loop/testclasses/EmptyLoops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package spoon.test.loop.testclasses;

public class EmptyLoops {
void m1() {
int x = 0;
for (;;) x = 5;
}
}

0 comments on commit eddd948

Please sign in to comment.