Search in sources :

Example 21 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method testFieldAccessNameSubstitutionInInnerClass.

@Test
public void testFieldAccessNameSubstitutionInInnerClass() throws Exception {
    // contract: the substitution of name of whole field is possible in inner class too
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/FieldAccessOfInnerClassTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new FieldAccessOfInnerClassTemplate("value").apply(factory.Class().create("x.X"));
        final CtClass<?> innerClass = result.getNestedType("Inner");
        assertEquals("int value;", innerClass.getField("value").toString());
        assertEquals("value = 7", innerClass.getMethodsByName("m").get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) FieldAccessOfInnerClassTemplate(spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate) Test(org.junit.Test)

Example 22 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method testExtensionBlock.

@Test
public void testExtensionBlock() throws Exception {
    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/logger/LoggerTemplate.java"));
    launcher.addProcessor(new LoggerTemplateProcessor());
    launcher.getEnvironment().setSourceClasspath(System.getProperty("java.class.path").split(File.pathSeparator));
    try {
        launcher.run();
    } catch (ClassCastException ignored) {
        fail();
    }
    final CtClass<Logger> aLogger = launcher.getFactory().Class().get(Logger.class);
    final CtMethod aMethod = aLogger.getMethodsByName("enter").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 : CtInvocation(spoon.reflect.code.CtInvocation) LoggerTemplateProcessor(spoon.test.template.testclasses.logger.LoggerTemplateProcessor) Launcher(spoon.Launcher) FileSystemFile(spoon.support.compiler.FileSystemFile) Logger(spoon.test.template.testclasses.logger.Logger) CtTry(spoon.reflect.code.CtTry) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 23 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method testTemplateMatcherMatchTwoSnippets.

@Test
public void testTemplateMatcherMatchTwoSnippets() throws Exception {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/TwoSnippets.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);
    // match using legacy TemplateMatcher#find method
    List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());
    assertEquals(2, matches.size());
    CtElement match1 = matches.get(0);
    CtElement match2 = matches.get(1);
    assertTrue(match1.equals(match2));
    // match using TemplateMatcher#matches method and query filter
    matches = factory.getModel().filterChildren(matcher).list();
    assertEquals(2, matches.size());
    match1 = matches.get(0);
    match2 = matches.get(1);
    assertTrue(match1.equals(match2));
}
Also used : 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 24 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method substituteSubString.

@Test
public void substituteSubString() throws Exception {
    // contract: the substitution of substrings works on named elements and references too
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubStringTemplate.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 SubStringTemplate("A").apply(factory.createClass());
        assertEquals("java.lang.String m_A = \"A is here more times: A\";", result.getField("m_A").toString());
        // contract: the parameter of type string replaces substring in method name
        CtMethod<?> method1 = result.getMethodsByName("setA").get(0);
        assertEquals("setA", method1.getSimpleName());
        assertEquals("java.lang.String p_A", method1.getParameters().get(0).toString());
        assertEquals("this.m_A = p_A", method1.getBody().getStatement(0).toString());
        assertEquals("setA(\"The A is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
    }
    {
        // contract: Type value name is substituted in substring of literal, named element and reference
        final CtClass<?> result = (CtClass<?>) new SubStringTemplate(factory.Type().OBJECT.getTypeDeclaration()).apply(factory.createClass());
        assertEquals("java.lang.String m_Object = \"Object is here more times: Object\";", result.getField("m_Object").toString());
        // contract: the parameter of type string replaces substring in method name
        CtMethod<?> method1 = result.getMethodsByName("setObject").get(0);
        assertEquals("setObject", method1.getSimpleName());
        assertEquals("java.lang.String p_Object", method1.getParameters().get(0).toString());
        assertEquals("this.m_Object = p_Object", method1.getBody().getStatement(0).toString());
        assertEquals("setObject(\"The Object is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
    }
    {
        // contract: Type reference value name is substituted in substring of literal, named element and reference
        final CtClass<?> result = (CtClass<?>) new SubStringTemplate(factory.Type().OBJECT).apply(factory.createClass());
        assertEquals("java.lang.String m_Object = \"Object is here more times: Object\";", result.getField("m_Object").toString());
        // contract: the parameter of type string replaces substring in method name
        CtMethod<?> method1 = result.getMethodsByName("setObject").get(0);
        assertEquals("setObject", method1.getSimpleName());
        assertEquals("java.lang.String p_Object", method1.getParameters().get(0).toString());
        assertEquals("this.m_Object = p_Object", method1.getBody().getStatement(0).toString());
        assertEquals("setObject(\"The Object is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
    }
    {
        // contract: String literal value name is substituted in substring of literal, named element and reference
        final CtClass<?> result = (CtClass<?>) new SubStringTemplate(factory.createLiteral("Xxx")).apply(factory.createClass());
        assertEquals("java.lang.String m_Xxx = \"Xxx is here more times: Xxx\";", result.getField("m_Xxx").toString());
        // contract: the parameter of type string replaces substring in method name
        CtMethod<?> method1 = result.getMethodsByName("setXxx").get(0);
        assertEquals("setXxx", method1.getSimpleName());
        assertEquals("java.lang.String p_Xxx", method1.getParameters().get(0).toString());
        assertEquals("this.m_Xxx = p_Xxx", method1.getBody().getStatement(0).toString());
        assertEquals("setXxx(\"The Xxx is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
    }
    {
        // contract: The elements which cannot be converted to String should throw exception
        SubStringTemplate template = new SubStringTemplate(factory.createSwitch());
        try {
            template.apply(factory.createClass());
            fail();
        } catch (SpoonException e) {
        // OK
        }
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) SubStringTemplate(spoon.test.template.testclasses.SubStringTemplate) SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 25 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method testSimpleTemplate.

@Test
public void testSimpleTemplate() {
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SimpleTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> testSimpleTpl = factory.Class().create("TestSimpleTpl");
    new SimpleTemplate("Hello world").apply(testSimpleTpl);
    Set<CtMethod<?>> listMethods = testSimpleTpl.getMethods();
    assertEquals(0, testSimpleTpl.getMethodsByName("apply").size());
    assertEquals(1, listMethods.size());
}
Also used : SimpleTemplate(spoon.test.template.testclasses.SimpleTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) 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