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));
}
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());
}
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());
}
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());
}
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());
}
Aggregations