Search in sources :

Example 1 with FieldAccessOfInnerClassTemplate

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

the class TemplateTest method testFieldAccessNameSubstitutionInInnerClass.

@Test
public void testFieldAccessNameSubstitutionInInnerClass() throws Exception {
    // contract: the substitution of name of whole field is possible in inner class too
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/FieldAccessOfInnerClassTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new FieldAccessOfInnerClassTemplate("value").apply(factory.Class().create("x.X"));
        final CtClass<?> innerClass = result.getNestedType("Inner");
        assertEquals("int value;", innerClass.getField("value").toString());
        assertEquals("value = 7", innerClass.getMethodsByName("m").get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) FieldAccessOfInnerClassTemplate(spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate) 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 FieldAccessOfInnerClassTemplate (spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate)1