-
-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e01f87
commit fe0689c
Showing
10 changed files
with
327 additions
and
5 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/test/java/spoon/test/template/TemplateInvocationSubstitutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package spoon.test.template; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
import spoon.Launcher; | ||
import spoon.reflect.code.CtBlock; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.factory.Factory; | ||
import spoon.support.compiler.FileSystemFile; | ||
import spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate; | ||
import spoon.test.template.testclasses.InvocationSubstitutionByStatementTemplate; | ||
|
||
public class TemplateInvocationSubstitutionTest { | ||
|
||
@Test | ||
public void testInvocationSubstitutionByStatement() throws Exception { | ||
//contract: the template engine supports substitution of any method invocation | ||
Launcher spoon = new Launcher(); | ||
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByStatementTemplate.java")); | ||
|
||
spoon.buildModel(); | ||
Factory factory = spoon.getFactory(); | ||
|
||
CtBlock<?> model = factory.Class().get(InvocationSubstitutionByStatementTemplate.class).getMethod("sample").getBody(); | ||
|
||
CtClass<?> resultKlass = factory.Class().create("Result"); | ||
CtStatement result = new InvocationSubstitutionByStatementTemplate(model).apply(resultKlass); | ||
assertEquals("throw new java.lang.RuntimeException(\"Failed\")", result.toString()); | ||
} | ||
|
||
@Test | ||
public void testInvocationSubstitutionByExpression() throws Exception { | ||
//contract: the template engine supports substitution of any method invocation | ||
Launcher spoon = new Launcher(); | ||
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByExpressionTemplate.java")); | ||
|
||
spoon.buildModel(); | ||
Factory factory = spoon.getFactory(); | ||
|
||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...test/java/spoon/test/template/testclasses/InvocationSubstitutionByExpressionTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package spoon.test.template.testclasses; | ||
|
||
import spoon.reflect.code.CtExpression; | ||
import spoon.template.BlockTemplate; | ||
import spoon.template.Local; | ||
import spoon.template.Parameter; | ||
|
||
public class InvocationSubstitutionByExpressionTemplate extends BlockTemplate { | ||
|
||
@Override | ||
public void block() throws Throwable { | ||
System.out.println(_expression_().substring(1)); | ||
System.out.println(_expression_.S().substring(1)); | ||
} | ||
|
||
@Parameter | ||
CtExpression<String> _expression_; | ||
|
||
@Local | ||
public InvocationSubstitutionByExpressionTemplate(CtExpression<String> expr) { | ||
this._expression_ = expr; | ||
} | ||
|
||
@Local | ||
String _expression_() { | ||
return null; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByStatementTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package spoon.test.template.testclasses; | ||
|
||
import spoon.reflect.code.CtStatement; | ||
import spoon.template.Local; | ||
import spoon.template.Parameter; | ||
import spoon.template.StatementTemplate; | ||
|
||
public class InvocationSubstitutionByStatementTemplate extends StatementTemplate { | ||
|
||
@Override | ||
public void statement() throws Throwable { | ||
_statement_(); | ||
} | ||
|
||
@Parameter("_statement_") | ||
CtStatement statement; | ||
|
||
@Local | ||
public InvocationSubstitutionByStatementTemplate(CtStatement statement) { | ||
this.statement = statement; | ||
} | ||
|
||
@Local | ||
void _statement_() { | ||
} | ||
|
||
@Local | ||
void sample() { | ||
throw new RuntimeException("Failed"); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/spoon/test/template/testclasses/LoggerModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2006-2015 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
|
||
package spoon.test.template.testclasses; | ||
|
||
import spoon.reflect.code.CtBlock; | ||
import spoon.test.template.testclasses.logger.Logger; | ||
|
||
public class LoggerModel { | ||
private String _classname_; | ||
private String _methodName_; | ||
private CtBlock<?> _block_; | ||
|
||
public void block() throws Throwable { | ||
try { | ||
Logger.enter(_classname_, _methodName_); | ||
_block_.S(); | ||
} finally { | ||
Logger.exit(_methodName_); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/spoon/test/template/testclasses/SubstituteRootTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package spoon.test.template.testclasses; | ||
|
||
import spoon.reflect.code.CtBlock; | ||
import spoon.template.Local; | ||
import spoon.template.Parameter; | ||
import spoon.template.StatementTemplate; | ||
import spoon.template.TemplateParameter; | ||
|
||
public class SubstituteRootTemplate extends StatementTemplate { | ||
|
||
@Override | ||
public void statement() throws Throwable { | ||
block.S(); | ||
} | ||
|
||
@Parameter | ||
TemplateParameter<Void> block; | ||
|
||
@Local | ||
public SubstituteRootTemplate(CtBlock<Void> block) { | ||
this.block = block; | ||
} | ||
|
||
@Local | ||
void sampleBlock() { | ||
String s="Spoon is cool!"; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/spoon/test/template/testclasses/types/AClassModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package spoon.test.template.testclasses.types; | ||
|
||
import java.util.AbstractList; | ||
|
||
public class AClassModel<E> extends AbstractList<E> implements AnIfaceModel { | ||
|
||
public AClassModel() { | ||
} | ||
|
||
@Override | ||
public E get(int index) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void someMethod() { | ||
} | ||
} |
Oops, something went wrong.