Search in sources :

Example 21 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class TemplateTest method testCheckBoundTemplate.

@Test
public void testCheckBoundTemplate() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/bounds/FooBound.java"), SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/bounds/CheckBoundTemplate.java")).build();
    CtClass<?> c = factory.Class().get(FooBound.class);
    CtMethod<?> method = c.getMethodsByName("method").get(0);
    assertEquals(1, Parameters.getAllTemplateParameterFields(CheckBoundTemplate.class).size());
    assertEquals(1, Parameters.getAllTemplateParameterFields(CheckBoundTemplate.class, factory).size());
    // creating a template instance
    CheckBoundTemplate t = new CheckBoundTemplate();
    assertTrue(t.isWellFormed());
    assertFalse(t.isValid());
    CtParameter<?> param = method.getParameters().get(0);
    t.setVariable(param);
    assertTrue(t.isValid());
    // getting the final AST
    CtStatement injectedCode = (t.apply(null));
    assertTrue(injectedCode instanceof CtIf);
    CtIf ifStmt = (CtIf) injectedCode;
    // contains the replaced code
    assertEquals("(l.size()) > 10", ifStmt.getCondition().toString());
    // adds the bound check at the beginning of a method
    method.getBody().insertBegin(injectedCode);
    assertEquals(injectedCode, method.getBody().getStatement(0));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CheckBoundTemplate(spoon.test.template.testclasses.bounds.CheckBoundTemplate) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 22 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class TemplateTest method testStatementTemplateRootSubstitution.

@Test
public void testStatementTemplateRootSubstitution() throws Exception {
    // contract: the template engine supports substitution of root element
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteRootTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> templateClass = factory.Class().get(SubstituteRootTemplate.class);
    CtBlock<Void> templateParam = (CtBlock) templateClass.getMethod("sampleBlock").getBody();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtStatement result = new SubstituteRootTemplate(templateParam).apply(resultKlass);
    assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock) result).getStatement(0).toString());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) SubstituteRootTemplate(spoon.test.template.testclasses.SubstituteRootTemplate) Test(org.junit.Test)

Example 23 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class TemplateTest method testTemplateInvocationSubstitution.

@Test
public void testTemplateInvocationSubstitution() throws Exception {
    // contract: the template engine supports substitution of method names in method calls.
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    new InvocationTemplate(factory.Type().OBJECT, "hashCode").apply(resultKlass);
    CtMethod<?> templateMethod = (CtMethod<?>) resultKlass.getElements(new NamedElementFilter<>(CtMethod.class, "invoke")).get(0);
    CtStatement templateRoot = (CtStatement) templateMethod.getBody().getStatement(0);
    // iface.$method$() becomes iface.hashCode()
    assertEquals("iface.hashCode()", templateRoot.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) InvocationTemplate(spoon.test.template.testclasses.InvocationTemplate) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 24 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class SerializableTest method testSerialCtStatement.

@Test
public void testSerialCtStatement() throws Exception {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    CtStatement sta2 = (factory).Code().createCodeSnippetStatement("String hello =\"t1\"; System.out.println(hello)").compile();
    byte[] ser = ByteSerialization.serialize(sta2);
    CtStatement des = (CtStatement) ByteSerialization.deserialize(ser);
    String sigBef = sta2.getShortRepresentation();
    String sigAf = des.getShortRepresentation();
    CtType<?> typeBef = sta2.getParent(CtType.class);
    assertNotNull(typeBef);
    assertEquals(sigBef, sigAf);
    des.setFactory(factory);
    String toSBef = sta2.toString();
    String toSgAf = des.toString();
    assertEquals(toSBef, toSgAf);
    CtType<?> typeDes = des.getParent(CtType.class);
    assertNotNull(typeDes);
    // After deserialization, getDeclaringType throws an exception
    CtType<?> decl = typeDes.getDeclaringType();
    assertNull(decl);
    CtPackage parentOriginal = (CtPackage) typeBef.getParent();
    CtPackage parentDeser = (CtPackage) typeDes.getParent();
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentOriginal.getSimpleName());
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentDeser.getSimpleName());
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtPackage(spoon.reflect.declaration.CtPackage) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 25 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class ReplaceTest method testReplaceStmtByList.

@Test
public void testReplaceStmtByList() {
    CtClass<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
    // replace retry content by statements
    CtStatement stmt = sample.getMethod("retry").getBody().getStatement(0);
    CtBlock lst = sample.getMethod("statements").getBody();
    // replace a single statement by a statement list
    stmt.replace(lst);
    // we should have only 2 statements after (from the stmt list)
    assertEquals(1, sample.getMethod("retry").getBody().getStatements().size());
    assertEquals(2, ((CtBlock) sample.getMethod("retry").getBody().getStatement(0)).getStatements().size());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Test(org.junit.Test)

Aggregations

CtStatement (spoon.reflect.code.CtStatement)84 Test (org.junit.Test)55 Factory (spoon.reflect.factory.Factory)35 CtMethod (spoon.reflect.declaration.CtMethod)23 Launcher (spoon.Launcher)20 CtBlock (spoon.reflect.code.CtBlock)17 CtIf (spoon.reflect.code.CtIf)11 CtExpression (spoon.reflect.code.CtExpression)10 ArrayList (java.util.ArrayList)9 CtElement (spoon.reflect.declaration.CtElement)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtInvocation (spoon.reflect.code.CtInvocation)8 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 CtCase (spoon.reflect.code.CtCase)6 CtStatementList (spoon.reflect.code.CtStatementList)6 STATEMENT (spoon.reflect.path.CtRole.STATEMENT)6 FileSystemFile (spoon.support.compiler.FileSystemFile)6 Adobada (spoon.test.delete.testclasses.Adobada)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)5