Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: Add a unit test to show issue #1221. Propose a simple fix. #1224

Merged
merged 2 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtBlockImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,23 @@ private void ensureModifiableStatementsList() {
@Override
public void removeStatement(CtStatement statement) {
if (this.statements != CtElementImpl.<CtStatement>emptyList()) {
this.statements.remove(statement);

boolean hasBeenRemoved = false;
// we cannot use a remove(statement) as it uses the equals
// and a block can have twice exactly the same statement.
for (int i = 0; i < this.statements.size(); i++) {
if (this.statements.get(i) == statement) {
this.statements.remove(i);
hasBeenRemoved = true;
break;
}
}

// in case we use it with a statement manually built
if (!hasBeenRemoved) {
this.statements.remove(statement);
}

if (isImplicit() && statements.size() == 0) {
setImplicit(false);
}
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/spoon/test/ctBlock/TestCtBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package spoon.test.ctBlock;

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.visitor.filter.NameFilter;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Created by urli on 15/03/2017.
*/
public class TestCtBlock {

@Test
public void testRemoveStatement() {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
spoon.buildModel();

List<CtMethod> methods = spoon.getModel().getElements(new NameFilter<CtMethod>("foo"));

assertEquals(1, methods.size());

CtMethod foo = methods.get(0);

CtBlock block = foo.getBody();
CtStatement lastStatement = block.getLastStatement();

assertEquals("i++", lastStatement.toString());

block.removeStatement(lastStatement);

CtStatement newLastStatement = block.getLastStatement();

assertTrue(newLastStatement != lastStatement);
assertTrue(newLastStatement instanceof CtIf);
}
}
16 changes: 16 additions & 0 deletions src/test/java/spoon/test/ctBlock/testclasses/Toto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package spoon.test.ctBlock.testclasses;

/**
* Created by urli on 15/03/2017.
*/
public class Toto {

public void foo() {
int i = 1;
i++;
if (i > 0) {
java.lang.System.out.println("test");
}
i++;
}
}
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/snippets/SnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import spoon.Launcher;
import spoon.compiler.SpoonResource;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCodeSnippetExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtReturn;
Expand Down Expand Up @@ -104,5 +105,4 @@ public void testIssue981() throws Exception {
spoon.buildModel();
assertEquals("foo.bar", spoon.getFactory().Type().get("foo.bar.X").getPackage().getQualifiedName());
}

}