Search in sources :

Example 1 with TypeReferenceClassAccessTemplate

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

the class TemplateTest method substituteTypeAccessReference.

@Test
public void substituteTypeAccessReference() throws Exception {
    // contract: the substitution of CtTypeAccess expression ignores actual type arguments if it have to
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/TypeReferenceClassAccessTemplate.java"));
    String outputDir = "./target/spooned/test/template/testclasses";
    spoon.setSourceOutputDirectory(outputDir);
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    // contract: String value is substituted in substring of literal, named element and reference
    CtTypeReference<?> typeRef = factory.Type().createReference(TypeReferenceClassAccessTemplate.Example.class);
    typeRef.addActualTypeArgument(factory.Type().DATE);
    final CtClass<?> result = (CtClass<?>) new TypeReferenceClassAccessTemplate(typeRef).apply(factory.Class().create("spoon.test.template.TypeReferenceClassAccess"));
    spoon.prettyprint();
    ModelUtils.canBeBuilt(outputDir, 8);
    CtMethod<?> method = result.getMethodsByName("someMethod").get(0);
    assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getType().toString());
    assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getParameters().get(0).getType().toString());
    assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.out", method.getBody().getStatement(0).toString());
    assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date> ret = new spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>()", method.getBody().getStatement(1).toString());
    assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.currentTimeMillis()", method.getBody().getStatement(2).toString());
    assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.class", method.getBody().getStatement(3).toString());
    assertEquals("o = (o) instanceof spoon.test.template.TypeReferenceClassAccess.Example<?>", method.getBody().getStatement(4).toString());
    assertEquals("java.util.function.Supplier<java.lang.Long> p = spoon.test.template.TypeReferenceClassAccess.Example::currentTimeMillis", method.getBody().getStatement(5).toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeReferenceClassAccessTemplate(spoon.test.template.testclasses.TypeReferenceClassAccessTemplate) 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 TypeReferenceClassAccessTemplate (spoon.test.template.testclasses.TypeReferenceClassAccessTemplate)1