use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class TemplateArrayAccessTest method testArrayLengthAccess.
@Test
public void testArrayLengthAccess() throws Exception {
// contract: the template engine replaces length of collection of parameter values by number
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteArrayLengthTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> resultKlass = factory.Class().create("Result");
CtStatement result = new SubstituteArrayLengthTemplate(new String[] { "a", null, "b" }).apply(resultKlass);
assertEquals("if (3 > 0);", result.toString());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ImportTest method testImportStaticAndFieldAccess.
@Test
public void testImportStaticAndFieldAccess() throws Exception {
// contract: Qualified field access and an import static should rewrite in fully qualified mode.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/");
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/Tacos.java");
launcher.buildModel();
final CtType<Object> aTacos = launcher.getFactory().Type().get(Tacos.class);
final CtStatement assignment = aTacos.getMethod("m").getBody().getStatement(0);
assertTrue(assignment instanceof CtLocalVariable);
assertEquals("spoon.test.imports.testclasses.internal4.Constants.CONSTANT.foo", ((CtLocalVariable) assignment).getAssignment().toString());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ImportTest method testAnotherMissingImport.
@Test
public void testAnotherMissingImport() throws Exception {
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--output-type", "nooutput" });
Factory factory = spoon.createFactory();
factory.getEnvironment().setNoClasspath(true);
factory.getEnvironment().setLevel("OFF");
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/AnotherMissingImport.java"));
compiler.build();
List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "doSomething"));
List<CtParameter> parameters = methods.get(0).getParameters();
CtTypeReference<?> type = parameters.get(0).getType();
assertEquals("SomeType", type.getSimpleName());
assertEquals("externallib", type.getPackage().getSimpleName());
CtMethod<?> mainMethod = factory.Class().getAll().get(0).getMethodsByName("main").get(0);
List<CtStatement> statements = mainMethod.getBody().getStatements();
CtStatement invocationStatement = statements.get(1);
assertTrue(invocationStatement instanceof CtInvocation);
CtInvocation invocation = (CtInvocation) invocationStatement;
CtExecutableReference executableReference = invocation.getExecutable();
assertEquals("doSomething(externallib.SomeType)", executableReference.getSignature());
assertSame(methods.get(0), executableReference.getDeclaration());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class InsertMethodsTest method insertBeforeAndUpdateParent.
@Test
public void insertBeforeAndUpdateParent() throws Exception {
/**
* if (condition)
* while (loop_condition)
*
* In this case the 'while' is inside an implicit block, but
* when we insert a new statement
*
* if (condition) {
* newStatement
* while (loop_condition)
* ...
* }
*
* Now the while is inside an explicit block.
*/
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/spoon/test/intercession/insertBefore/InsertBeforeExample2.java")).build();
// Get the 'while'
List<CtWhile> elements = Query.getElements(factory, new TypeFilter<CtWhile>(CtWhile.class));
assertTrue(1 == elements.size());
CtWhile theWhile = elements.get(0);
// We make sure the parent of the while is the CtIf and not the block
CtElement parent = theWhile.getParent();
assertTrue(parent instanceof CtBlock);
assertTrue(parent.isImplicit());
CtIf ifParent = (CtIf) parent.getParent();
// Create a new statement to be inserted before the while
CtStatement insert = factory.Code().createCodeSnippetStatement("System.out.println()");
// Insertion of the new statement
theWhile.insertBefore(insert);
// We make sure the parent of the while is updated
CtElement newParent = theWhile.getParent();
assertTrue(newParent != ifParent);
assertTrue(newParent instanceof CtBlock);
assertFalse(newParent.isImplicit());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class InsertMethodsTest method testInsertBefore.
@Test
public void testInsertBefore() {
CtMethod<Void> foo = (CtMethod<Void>) assignmentClass.getMethods().toArray()[0];
CtBlock<?> body = foo.getBody();
assertEquals(3, body.getStatements().size());
CtStatement s = body.getStatements().get(2);
assertEquals("int z = x + y", s.toString());
// adding a new statement;
CtCodeSnippetStatement stmt = factory.Core().createCodeSnippetStatement();
stmt.setValue("System.out.println(x);");
s.insertBefore(stmt);
assertEquals(4, body.getStatements().size());
assertSame(stmt, body.getStatements().get(2));
assertEquals(s.getParent(), stmt.getParent());
}
Aggregations