use of spoon.support.compiler.FileSystemFile 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.support.compiler.FileSystemFile 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.support.compiler.FileSystemFile 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.support.compiler.FileSystemFile 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());
}
use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.
the class TemplateTest method testExtensionDecoupledSubstitutionVisitor.
@Test
public void testExtensionDecoupledSubstitutionVisitor() throws Exception {
// contract: substitution can be done on model, which is not based on Template
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/LoggerModel.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> aTemplateModelType = launcher.getFactory().Class().get(LoggerModel.class);
final CtMethod<?> aTemplateModel = aTemplateModelType.getMethod("block");
final CtClass<?> aTargetType = launcher.getFactory().Class().get(Logger.class);
final CtMethod<?> toBeLoggedMethod = aTargetType.getMethodsByName("enter").get(0);
Map<String, Object> params = new HashMap<>();
params.put("_classname_", factory.Code().createLiteral(aTargetType.getSimpleName()));
params.put("_methodName_", factory.Code().createLiteral(toBeLoggedMethod.getSimpleName()));
params.put("_block_", toBeLoggedMethod.getBody());
final List<CtMethod<?>> aMethods = new SubstitutionVisitor(factory, params).substitute(aTemplateModel.clone());
assertEquals(1, aMethods.size());
final CtMethod<?> aMethod = aMethods.get(0);
assertTrue(aMethod.getBody().getStatement(0) instanceof CtTry);
final CtTry aTry = (CtTry) aMethod.getBody().getStatement(0);
assertTrue(aTry.getFinalizer().getStatement(0) instanceof CtInvocation);
assertEquals("spoon.test.template.testclasses.logger.Logger.exit(\"enter\")", aTry.getFinalizer().getStatement(0).toString());
assertTrue(aTry.getBody().getStatement(0) instanceof CtInvocation);
assertEquals("spoon.test.template.testclasses.logger.Logger.enter(\"Logger\", \"enter\")", aTry.getBody().getStatement(0).toString());
assertTrue(aTry.getBody().getStatements().size() > 1);
}
Aggregations