Search in sources :

Example 1 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateInvocationSubstitutionTest method testInvocationSubstitutionByStatement.

@Test
public void testInvocationSubstitutionByStatement() throws Exception {
    // contract: the template engine supports substitution of any method invocation
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByStatementTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtBlock<?> templateArg = factory.Class().get(InvocationSubstitutionByStatementTemplate.class).getMethod("sample").getBody();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtStatement result = new InvocationSubstitutionByStatementTemplate(templateArg).apply(resultKlass);
    assertEquals("throw new java.lang.RuntimeException(\"Failed\")", result.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) InvocationSubstitutionByStatementTemplate(spoon.test.template.testclasses.InvocationSubstitutionByStatementTemplate) Test(org.junit.Test)

Example 2 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateInvocationSubstitutionTest method testInvocationSubstitutionByExpression.

@Test
public void testInvocationSubstitutionByExpression() throws Exception {
    // contract: the template engine supports substitution of any method invocation
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByExpressionTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtBlock<?> result = new InvocationSubstitutionByExpressionTemplate(factory.createLiteral("abc")).apply(resultKlass);
    assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(0).toString());
    assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(1).toString());
}
Also used : InvocationSubstitutionByExpressionTemplate(spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 3 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateInvocationSubstitutionTest method testSubstitutionByExpression.

@Test
public void testSubstitutionByExpression() throws Exception {
    // contract: the template engine understands fields whose type extends from TemplateParameter as template parameter automatically. No need for extra annotation
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstitutionByExpressionTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtBlock<?> result = new SubstitutionByExpressionTemplate(factory.createLiteral("abc")).apply(resultKlass);
    assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(0).toString());
}
Also used : InvocationSubstitutionByExpressionTemplate(spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate) SubstitutionByExpressionTemplate(spoon.test.template.testclasses.SubstitutionByExpressionTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 4 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method substituteStringLiteral.

@Test
public void substituteStringLiteral() throws Exception {
    // contract: the substitution of literals is possible too
    // contract: the template engine supports substitution of root element
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteLiteralTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate("value1").apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"value1\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring value1 is substituted too - value1\";", result.getField("stringField2").toString());
        // contract: the parameter of type string replaces only method name
        assertEquals("java.lang.System.out.println(spoon.test.template.testclasses.Params.value1())", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
    {
        // contract: String Literal value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate(factory.createLiteral("value2")).apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"value2\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring value2 is substituted too - value2\";", result.getField("stringField2").toString());
        // contract: the parameter of type String literal replaces whole invocation
        assertEquals("java.lang.System.out.println(\"value2\")", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
    {
        // contract: simple name of type reference is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate(factory.Type().createReference("some.ignored.package.TypeName")).apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"TypeName\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring TypeName is substituted too - TypeName\";", result.getField("stringField2").toString());
        // contract type reference is substituted in invocation as class access
        assertEquals("java.lang.System.out.println(some.ignored.package.TypeName.class)", result.getMethodsByName("m1").get(0).getBody().getStatement(0).toString());
    }
    {
        // contract: number literal is substituted in String literal as number converted to string
        final CtClass<?> result = (CtClass<?>) new SubstituteLiteralTemplate(factory.createLiteral(7)).apply(factory.createClass());
        assertEquals("java.lang.String stringField1 = \"7\";", result.getField("stringField1").toString());
        assertEquals("java.lang.String stringField2 = \"Substring 7 is substituted too - 7\";", result.getField("stringField2").toString());
        // contract number literal is substituted in invocation as number literal
        assertEquals("java.lang.System.out.println(7)", result.getMethodsByName("m1").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) SubstituteLiteralTemplate(spoon.test.template.testclasses.SubstituteLiteralTemplate) Test(org.junit.Test)

Example 5 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method testSubstituteInnerClass.

@Test
public void testSubstituteInnerClass() throws Exception {
    // contract: the inner class is substituted well too and references to target class are substituted well
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InnerClassTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> result = factory.Class().create("x.Result");
    new InnerClassTemplate().apply(result);
    assertEquals(1, result.getNestedTypes().size());
    CtType<?> innerType = result.getNestedType("Inner");
    assertNotNull(innerType);
    CtField<?> innerField = innerType.getField("innerField");
    assertNotNull(innerField);
    assertSame(innerType, innerField.getDeclaringType());
    CtFieldReference<?> fr = innerType.filterChildren((CtFieldReference<?> e) -> true).first();
    // check that reference to declaring type is correctly substituted
    assertEquals("x.Result$Inner", fr.getDeclaringType().getQualifiedName());
}
Also used : FieldAccessOfInnerClassTemplate(spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate) InnerClassTemplate(spoon.test.template.testclasses.InnerClassTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 FileSystemFile (spoon.support.compiler.FileSystemFile)28 Launcher (spoon.Launcher)26 Factory (spoon.reflect.factory.Factory)25 CtClass (spoon.reflect.declaration.CtClass)8 CtMethod (spoon.reflect.declaration.CtMethod)8 CtStatement (spoon.reflect.code.CtStatement)6 File (java.io.File)5 CtBlock (spoon.reflect.code.CtBlock)4 CtIf (spoon.reflect.code.CtIf)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtTry (spoon.reflect.code.CtTry)2 CtElement (spoon.reflect.declaration.CtElement)2 TemplateMatcher (spoon.template.TemplateMatcher)2 AnotherFieldAccessTemplate (spoon.test.template.testclasses.AnotherFieldAccessTemplate)2 FieldAccessOfInnerClassTemplate (spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate)2 InvocationSubstitutionByExpressionTemplate (spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate)2 ReturnReplaceTemplate (spoon.test.template.testclasses.ReturnReplaceTemplate)2 ModelUtils.getOptimizedString (spoon.testing.utils.ModelUtils.getOptimizedString)2 HashMap (java.util.HashMap)1