Skip to content

Commit

Permalink
Make changes to handle Block statements appropriately in case of method
Browse files Browse the repository at this point in the history
body and as child element of another block. Add test methods.
Fixes INRIA#2013
  • Loading branch information
LakshyAAAgrawal committed Jul 15, 2020
1 parent 79f8176 commit ab07c50
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/main/java/spoon/reflect/code/CtBlock.java
Expand Up @@ -27,4 +27,12 @@ public interface CtBlock<R> extends CtStatement, CtStatementList, TemplateParame

@Override
CtBlock<R> clone();

/**
* If the block is a method body, then all the statements are
* replaced by corresponding comments within the block. If not
* then the block itself is replaced by a Block comment.
*/
@Override
public void comment();
}
16 changes: 16 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtBlockImpl.java
Expand Up @@ -10,11 +10,13 @@
import spoon.reflect.ModelElementContainerDefaultCapacities;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;
Expand Down Expand Up @@ -196,4 +198,18 @@ public R S() {
public CtBlock<R> clone() {
return (CtBlock<R>) super.clone();
}

@Override
public void comment() {
if (!isParentInitialized()) {
// already not in a tree, commenting wouldn't make a difference
return;
}

if(getParent() instanceof CtMethod) {
this.getStatements().forEach(stmt -> stmt.comment());
}else{
super.comment();
}
}
}
Expand Up @@ -20,6 +20,7 @@

import spoon.Launcher;
import spoon.reflect.code.CtAssignment;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtIf;
Expand All @@ -32,10 +33,10 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.CtBFSIterator;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.statementComment.testclasses.Adobada;
import spoon.test.statementComment.testclasses.AllStmtExtensions;

import java.lang.annotation.Annotation;
Expand All @@ -44,22 +45,102 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

public class StatementCommentTest {
@Test
public void testAssertStatement(){

String EOL;

public StatementCommentTest() {
EOL = System.getProperty("line.separator");
}

@Test
public void testAssignmentStatement(){

private Factory getSpoonFactory() {
final Launcher launcher = new Launcher();
launcher.run(new String[]{
"-i", "./src/test/java/spoon/test/statementComment/testclasses",
"-o", "./target/spooned/",
"-c"
});
return launcher.getFactory();
}

private Launcher setUpTest() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/statementComment/testclasses/AllStmtExtensions.java");
launcher.getEnvironment().setCommentEnabled(true);
launcher.run();
return launcher;
}

@Test
public void testBlockStatement(){

public void testAssertStatement(){
Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m1 = allstmt.getMethod("m1");
m1.getBody().getStatement(0).comment();
assertTrue(m1.getBody().getStatement(0) instanceof CtComment);
CtComment assertAsComment = ((CtComment) m1.getBody().getStatement(0));
assertEquals(assertAsComment.getContent(), "assert 1 == 5;");
}

@Test
public void testAssignmentStatement(){
Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m1 = allstmt.getMethod("m1");
assertTrue(m1.getBody().getStatement(2) instanceof CtAssignment);
CtAssignment<?, ?> assignmentStatement = (CtAssignment<?, ?>) m1.getBody().getStatement(2);
assignmentStatement.comment();
assertTrue(m1.getBody().getStatement(2) instanceof CtComment);
CtComment assignmentAsComment = ((CtComment) m1.getBody().getStatement(2));
assertEquals(assignmentAsComment.getContent(), "r = 20;");
}

@Test
public void testBlockStatementWithinBody(){
Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m1 = allstmt.getMethod("m1");
assertTrue(m1.getBody().getStatement(4) instanceof CtBlock);
CtBlock blockWithinBody = m1.getBody().getStatement(4);
blockWithinBody.comment();
assertTrue(m1.getBody().getStatement(4) instanceof CtComment);
CtComment blockAsComment = (CtComment) m1.getBody().getStatement(4);
assertEquals("{" + EOL +
"int j = 10;" + EOL +
"}", blockAsComment.getContent());
}

@Test
public void testMethodBodyEmptyStatement(){
Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m2 = allstmt.getMethod("m2");
m2.getBody().comment();
assertEquals("{" + EOL +
"}", m2.getBody().prettyprint());
}

@Test
public void testMethodBodyNonEmptyStatement(){
Launcher launcher = setUpTest();
CtClass<?> allstmt = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.statementComment.testclasses.AllStmtExtensions");
CtMethod<?> m1 = allstmt.getMethod("m1");
m1.getBody().comment();
for(CtStatement stmt: m1.getBody().getStatements()) {
assertTrue(stmt instanceof CtComment);
}
assertEquals("{" + EOL +
" // assert 1 == 5;" + EOL +
" // int r = 10;" + EOL +
" // r = 20;" + EOL +
" // java.lang.String s = \"This is a new String!\";" + EOL +
" /* {" + EOL +
" int j = 10;" + EOL +
" }" + EOL +
" */" + EOL +
"}", m1.getBody().prettyprint());
}

@Test
Expand Down
Expand Up @@ -2,8 +2,13 @@
public class AllStmtExtensions{
public AllStmtExtensions() {}
void m1() {
int i = 10;
assert 1 == 5;
int r = 10;
r = 20;
String s = "This is a new String!";
{
int j = 10;
}
}
void m2() {

Expand Down

0 comments on commit ab07c50

Please sign in to comment.