Search in sources :

Example 6 with FileSystemFile

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ObjectIsNotParamTemplate(spoon.test.template.testclasses.ObjectIsNotParamTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 7 with FileSystemFile

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtBlock(spoon.reflect.code.CtBlock) CtElement(spoon.reflect.declaration.CtElement) TemplateMatcher(spoon.template.TemplateMatcher) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtMethod(spoon.reflect.declaration.CtMethod) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 8 with FileSystemFile

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());
}
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 9 with FileSystemFile

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeReferenceClassAccessTemplate(spoon.test.template.testclasses.TypeReferenceClassAccessTemplate) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 10 with FileSystemFile

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);
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtTry(spoon.reflect.code.CtTry) SubstitutionVisitor(spoon.support.template.SubstitutionVisitor) CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) 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