Search in sources :

Example 1 with FieldAccessTemplate

use of spoon.test.template.testclasses.FieldAccessTemplate in project spoon by INRIA.

the class TemplateTest method testFieldAccessNameSubstitution.

@Test
public void testFieldAccessNameSubstitution() 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/FieldAccessTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new FieldAccessTemplate("value").apply(factory.Class().create("x.X"));
        assertEquals("int value;", result.getField("value").toString());
        assertEquals("value = 7", result.getMethodsByName("m").get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AnotherFieldAccessTemplate(spoon.test.template.testclasses.AnotherFieldAccessTemplate) FieldAccessTemplate(spoon.test.template.testclasses.FieldAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtClass (spoon.reflect.declaration.CtClass)1 Factory (spoon.reflect.factory.Factory)1 FileSystemFile (spoon.support.compiler.FileSystemFile)1 AnotherFieldAccessTemplate (spoon.test.template.testclasses.AnotherFieldAccessTemplate)1 FieldAccessTemplate (spoon.test.template.testclasses.FieldAccessTemplate)1