Search in sources :

Example 1 with TemplateWithConstructor

use of spoon.test.template.testclasses.constructors.TemplateWithConstructor in project spoon by INRIA.

the class TemplateTest method testTemplateC1.

@Test
public void testTemplateC1() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/constructors/C1.java"), SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/constructors/TemplateWithConstructor.java", "./src/test/java/spoon/test/template/testclasses/constructors/TemplateWithFieldsAndMethods.java")).build();
    CtClass<?> c1 = factory.Class().get(C1.class);
    // before template: 1 constructor
    assertEquals(// this is the default implicit constructor
    1, c1.getConstructors().size());
    // the actual substitution
    new TemplateWithConstructor(factory.Type().createReference(Date.class)).apply(c1);
    // after template: 3 constructors
    // System.out.println("==>"+c1.getConstructors());
    assertEquals(3, c1.getConstructors().size());
    CtField<?> toBeInserted = c1.getElements(new NamedElementFilter<>(CtField.class, "toBeInserted")).get(0);
    assertEquals(Date.class, toBeInserted.getType().getActualTypeArguments().get(0).getActualClass());
    assertEquals("java.util.List<java.util.Date> toBeInserted = new java.util.ArrayList<java.util.Date>();", toBeInserted.toString());
    new TemplateWithFieldsAndMethods("testparam", factory.Code().createLiteral("testparam2")).apply(c1);
    assertEquals(3, c1.getConstructors().size());
    assertNotNull(c1.getField("fieldToBeInserted"));
    CtMethod<?> m = c1.getMethod("methodToBeInserted");
    assertNotNull(m);
    assertEquals("return \"testparam\"", m.getBody().getStatement(0).toString());
    CtMethod<?> m2 = c1.getMethod("methodToBeInserted2");
    assertNotNull(m2);
    assertEquals("return \"testparam2\"", m2.getBody().getStatement(0).toString());
    new ModelConsistencyChecker(factory.getEnvironment(), false, true).scan(c1);
    assertEquals(0, factory.getEnvironment().getErrorCount());
    assertEquals(0, factory.getEnvironment().getWarningCount());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TemplateWithConstructor(spoon.test.template.testclasses.constructors.TemplateWithConstructor) ModelConsistencyChecker(spoon.reflect.visitor.ModelConsistencyChecker) TemplateWithFieldsAndMethods(spoon.test.template.testclasses.constructors.TemplateWithFieldsAndMethods) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Launcher (spoon.Launcher)1 Factory (spoon.reflect.factory.Factory)1 ModelConsistencyChecker (spoon.reflect.visitor.ModelConsistencyChecker)1 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)1 TemplateWithConstructor (spoon.test.template.testclasses.constructors.TemplateWithConstructor)1 TemplateWithFieldsAndMethods (spoon.test.template.testclasses.constructors.TemplateWithFieldsAndMethods)1