Search in sources :

Example 66 with Factory

use of spoon.reflect.factory.Factory 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 67 with Factory

use of spoon.reflect.factory.Factory 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 68 with Factory

use of spoon.reflect.factory.Factory 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 69 with Factory

use of spoon.reflect.factory.Factory 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)

Example 70 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class TemplateTest method testTemplateArrayAccess.

@Test
public void testTemplateArrayAccess() throws Exception {
    // contract: the template engine supports substitution of arrays of parameters.
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ArrayAccessTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtClass<?> templateClass = factory.Class().get(ArrayAccessTemplate.class);
    // create array of template parameters, which contains CtBlocks
    TemplateParameter[] params = templateClass.getMethod("sampleBlocks").getBody().getStatements().toArray(new TemplateParameter[0]);
    new ArrayAccessTemplate(params).apply(resultKlass);
    CtMethod<?> m = resultKlass.getMethod("method");
    // check that both TemplateParameter usages were replaced by appropriate parameter value and that substitution which miss the value is silently removed
    assertEquals(2, m.getBody().getStatements().size());
    assertTrue(m.getBody().getStatements().get(0) instanceof CtBlock);
    assertEquals("int i = 0", ((CtBlock) m.getBody().getStatements().get(0)).getStatement(0).toString());
    assertTrue(m.getBody().getStatements().get(1) instanceof CtBlock);
    assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock) m.getBody().getStatements().get(1)).getStatement(0).toString());
    // check that both @Parameter usage was replaced by appropriate parameter value
    CtMethod<?> m2 = resultKlass.getMethod("method2");
    assertEquals("java.lang.System.out.println(\"second\")", m2.getBody().getStatement(0).toString());
    // check that substitution by missing value correctly produces empty expression
    assertEquals("java.lang.System.out.println(null)", m2.getBody().getStatement(1).toString());
}
Also used : TemplateParameter(spoon.template.TemplateParameter) CtBlock(spoon.reflect.code.CtBlock) ArrayAccessTemplate(spoon.test.template.testclasses.ArrayAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19