Search in sources :

Example 1 with InvocationTemplate

use of spoon.test.template.testclasses.InvocationTemplate 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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) InvocationTemplate(spoon.test.template.testclasses.InvocationTemplate) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtStatement (spoon.reflect.code.CtStatement)1 CtMethod (spoon.reflect.declaration.CtMethod)1 Factory (spoon.reflect.factory.Factory)1 FileSystemFile (spoon.support.compiler.FileSystemFile)1 InvocationTemplate (spoon.test.template.testclasses.InvocationTemplate)1