use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testExtensionDecoupledSubstitutionVisitor.
@Test
public void testExtensionDecoupledSubstitutionVisitor() throws Exception {
// contract: substitution can be done on model, which is not based on Template
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/LoggerModel.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> aTemplateModelType = launcher.getFactory().Class().get(LoggerModel.class);
final CtMethod<?> aTemplateModel = aTemplateModelType.getMethod("block");
final CtClass<?> aTargetType = launcher.getFactory().Class().get(Logger.class);
final CtMethod<?> toBeLoggedMethod = aTargetType.getMethodsByName("enter").get(0);
Map<String, Object> params = new HashMap<>();
params.put("_classname_", factory.Code().createLiteral(aTargetType.getSimpleName()));
params.put("_methodName_", factory.Code().createLiteral(toBeLoggedMethod.getSimpleName()));
params.put("_block_", toBeLoggedMethod.getBody());
final List<CtMethod<?>> aMethods = new SubstitutionVisitor(factory, params).substitute(aTemplateModel.clone());
assertEquals(1, aMethods.size());
final CtMethod<?> aMethod = aMethods.get(0);
assertTrue(aMethod.getBody().getStatement(0) instanceof CtTry);
final CtTry aTry = (CtTry) aMethod.getBody().getStatement(0);
assertTrue(aTry.getFinalizer().getStatement(0) instanceof CtInvocation);
assertEquals("spoon.test.template.testclasses.logger.Logger.exit(\"enter\")", aTry.getFinalizer().getStatement(0).toString());
assertTrue(aTry.getBody().getStatement(0) instanceof CtInvocation);
assertEquals("spoon.test.template.testclasses.logger.Logger.enter(\"Logger\", \"enter\")", aTry.getBody().getStatement(0).toString());
assertTrue(aTry.getBody().getStatements().size() > 1);
}
use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testTemplateArrayAccess.
@Test
public void testTemplateArrayAccess() throws Exception {
// contract: the template engine supports substitution of arrays of parameters.
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ArrayAccessTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> resultKlass = factory.Class().create("Result");
CtClass<?> templateClass = factory.Class().get(ArrayAccessTemplate.class);
// create array of template parameters, which contains CtBlocks
TemplateParameter[] params = templateClass.getMethod("sampleBlocks").getBody().getStatements().toArray(new TemplateParameter[0]);
new ArrayAccessTemplate(params).apply(resultKlass);
CtMethod<?> m = resultKlass.getMethod("method");
// check that both TemplateParameter usages were replaced by appropriate parameter value and that substitution which miss the value is silently removed
assertEquals(2, m.getBody().getStatements().size());
assertTrue(m.getBody().getStatements().get(0) instanceof CtBlock);
assertEquals("int i = 0", ((CtBlock) m.getBody().getStatements().get(0)).getStatement(0).toString());
assertTrue(m.getBody().getStatements().get(1) instanceof CtBlock);
assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock) m.getBody().getStatements().get(1)).getStatement(0).toString());
// check that both @Parameter usage was replaced by appropriate parameter value
CtMethod<?> m2 = resultKlass.getMethod("method2");
assertEquals("java.lang.System.out.println(\"second\")", m2.getBody().getStatement(0).toString());
// check that substitution by missing value correctly produces empty expression
assertEquals("java.lang.System.out.println(null)", m2.getBody().getStatement(1).toString());
}
use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testSubstitutionInsertAllNtoN.
@Test
public void testSubstitutionInsertAllNtoN() {
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/NtonCodeTemplate.java"));
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/C.java");
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> cclass = factory.Class().get("spoon.test.template.testclasses.C");
new NtonCodeTemplate(cclass.getReference(), 5).apply(cclass);
Set<CtMethod<?>> listMethods = cclass.getMethods();
assertEquals(0, cclass.getMethodsByName("apply").size());
assertEquals(4, listMethods.size());
}
use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testTemplateInvocationSubstitution.
@Test
public void testTemplateInvocationSubstitution() throws Exception {
// contract: the template engine supports substitution of method names in method calls.
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> resultKlass = factory.Class().create("Result");
new InvocationTemplate(factory.Type().OBJECT, "hashCode").apply(resultKlass);
CtMethod<?> templateMethod = (CtMethod<?>) resultKlass.getElements(new NamedElementFilter<>(CtMethod.class, "invoke")).get(0);
CtStatement templateRoot = (CtStatement) templateMethod.getBody().getStatement(0);
// iface.$method$() becomes iface.hashCode()
assertEquals("iface.hashCode()", templateRoot.toString());
}
use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testAnotherFieldAccessNameSubstitution.
@Test
public void testAnotherFieldAccessNameSubstitution() throws Exception {
// contract: the substitution of name of whole field is possible
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/AnotherFieldAccessTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
{
// contract: String value is substituted in String literal
final CtClass<?> result = (CtClass<?>) new AnotherFieldAccessTemplate().apply(factory.Class().create("x.X"));
assertEquals("int x;", result.getField("x").toString());
assertEquals("int m_x;", result.getField("m_x").toString());
assertEquals("java.lang.System.out.println(((x) + (m_x)))", result.getAnonymousExecutables().get(0).getBody().getStatement(0).toString());
}
}
Aggregations