Search in sources :

Example 81 with Launcher

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());
}
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 82 with Launcher

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());
}
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 83 with Launcher

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));
}
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 84 with Launcher

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());
}
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 85 with Launcher

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

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22