use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testObjectIsNotParamTemplate.
@Test
public void testObjectIsNotParamTemplate() throws Exception {
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ObjectIsNotParamTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
// contract: String value is substituted in substring of literal, named element and reference
final CtClass<?> result = (CtClass<?>) new ObjectIsNotParamTemplate().apply(factory.createClass());
assertEquals(0, result.getMethodsByName("methXXXd").size());
assertEquals(1, result.getMethodsByName("method").size());
}
use of spoon.Launcher in project spoon by INRIA.
the class TemplateTest method testTemplateMatcherWithWholePackage.
@Test
public void testTemplateMatcherWithWholePackage() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/ContextHelper.java");
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/BServiceImpl.java");
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher1")).get(0);
CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());
assertEquals(1, matches.size());
CtElement match = matches.get(0);
assertTrue("Match is not a if", match instanceof CtIf);
CtElement matchParent = match.getParent();
assertTrue("Match parent is not a block", matchParent instanceof CtBlock);
CtElement matchParentParent = matchParent.getParent();
assertTrue("Match grand parent is not a method", matchParentParent instanceof CtMethod);
CtMethod methodHello = (CtMethod) matchParentParent;
assertEquals("Match grand parent is not a method called hello", "hello", methodHello.getSimpleName());
CtElement methodParent = methodHello.getParent();
assertTrue("Parent of the method is not a class", methodParent instanceof CtClass);
CtClass bservice = (CtClass) methodParent;
assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName());
}
use of spoon.Launcher 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.Launcher 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.Launcher 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());
}
Aggregations