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: fix regression due to #2009 #2035

Merged
merged 1 commit into from
Jun 6, 2018
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
5 changes: 4 additions & 1 deletion src/main/java/spoon/template/BlockTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public BlockTemplate() {

public CtBlock<?> apply(CtType<?> targetType) {
CtClass<? extends BlockTemplate> c = Substitution.getTemplateCtClass(targetType, this);
return Substitution.substitute(targetType, this, getBlock(c));
CtBlock<?> result = Substitution.substitute(targetType, this, getBlock(c));
// removing it from its parent
result.delete();
return result;
}

public Void S() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spoon/template/ExpressionTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public ExpressionTemplate() {
public CtExpression<T> apply(CtType<?> targetType) {
CtClass<? extends ExpressionTemplate<?>> c = Substitution.getTemplateCtClass(targetType, this);
CtBlock<?> b = Substitution.substitute(targetType, this, getExpressionBlock(c));
return ((CtReturn<T>) b.getStatements().get(0)).getReturnedExpression();
CtExpression<T> returnedExpression = ((CtReturn<T>) b.getStatements().get(0)).getReturnedExpression();
returnedExpression.setParent(null);
return returnedExpression;
}

public T S() {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/spoon/template/StatementTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public CtStatement apply(CtType<?> targetType) {
if (statements.size() > 1) {
throw new SpoonException("StatementTemplate cannot return more then one statement");
}
return statements.isEmpty() ? null : statements.get(0);

if (statements.size() == 1) {
statements.get(0).setParent(null);
return statements.get(0);
}

return null;
}

public Void S() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spoon.test.template;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import org.junit.Test;

Expand Down Expand Up @@ -43,8 +45,12 @@ public void testInvocationSubstitutionByExpression() throws Exception {

CtClass<?> resultKlass = factory.Class().create("Result");
CtBlock<?> result = new InvocationSubstitutionByExpressionTemplate(factory.createLiteral("abc")).apply(resultKlass);

assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(0).toString());
assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(1).toString());

// contract: the result of the template has no parent, and can be put anywhere in an AST
assertFalse(result.isParentInitialized());
}

@Test
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ public void testExpressionTemplate() throws Exception {

CtClass<?> resultKlass = factory.Class().create("Result");
CtExpression result = new AnExpressionTemplate(factory.createCodeSnippetExpression("\"Spoon is cool!\"")).apply(resultKlass);
assertFalse(result.isParentInitialized());
assertEquals("new java.lang.String(\"Spoon is cool!\")", result.toString());
}

Expand Down