Search in sources :

Example 71 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) SubstituteArrayLengthTemplate(spoon.test.template.testclasses.SubstituteArrayLengthTemplate) Test(org.junit.Test)

Example 72 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 73 with CtStatement

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtInvocation(spoon.reflect.code.CtInvocation) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 74 with CtStatement

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());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtWhile(spoon.reflect.code.CtWhile) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 75 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtStatement (spoon.reflect.code.CtStatement)84 Test (org.junit.Test)55 Factory (spoon.reflect.factory.Factory)35 CtMethod (spoon.reflect.declaration.CtMethod)23 Launcher (spoon.Launcher)20 CtBlock (spoon.reflect.code.CtBlock)17 CtIf (spoon.reflect.code.CtIf)11 CtExpression (spoon.reflect.code.CtExpression)10 ArrayList (java.util.ArrayList)9 CtElement (spoon.reflect.declaration.CtElement)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtInvocation (spoon.reflect.code.CtInvocation)8 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 CtCase (spoon.reflect.code.CtCase)6 CtStatementList (spoon.reflect.code.CtStatementList)6 STATEMENT (spoon.reflect.path.CtRole.STATEMENT)6 FileSystemFile (spoon.support.compiler.FileSystemFile)6 Adobada (spoon.test.delete.testclasses.Adobada)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)5