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