Search in sources :

Example 51 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class DeleteTest method testDeleteAStatementInAnonymousExecutable.

@Test
public void testDeleteAStatementInAnonymousExecutable() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final List<CtAnonymousExecutable> anonymousExecutables = adobada.getAnonymousExecutables();
    final CtAnonymousExecutable instanceExec = anonymousExecutables.get(0);
    assertEquals(2, instanceExec.getBody().getStatements().size());
    final CtStatement statement = instanceExec.getBody().getStatement(1);
    statement.delete();
    assertEquals(1, instanceExec.getBody().getStatements().size());
    assertFalse(instanceExec.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Example 52 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class SwitchCaseTest method insertBeforeStatementInSwitchCaseWithoutException.

@Test
public void insertBeforeStatementInSwitchCaseWithoutException() throws Exception {
    String packageName = "spoon.test.ctCase";
    String className = "ClassWithSwitchExample";
    Factory factory = factoryFor(packageName, className);
    List<CtCase> elements = elementsOfType(CtCase.class, factory);
    assertEquals(3, elements.size());
    CtCase firstCase = elements.get(0);
    List<CtStatement> statements = firstCase.getStatements();
    assertEquals(2, statements.size());
    CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
    statements.get(0).insertBefore(newStatement);
    statements = firstCase.getStatements();
    assertEquals(3, statements.size());
    assertTrue(statements.get(0) == newStatement);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 53 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class ExecutableRefTest method getInvocationFromMethod.

private CtAbstractInvocation<?> getInvocationFromMethod(String methodName) throws Exception {
    Factory factory = build(ExecutableRefTestSource.class, MyIntf.class);
    CtClass<ExecutableRefTestSource> clazz = factory.Class().get(ExecutableRefTestSource.class);
    Assert.assertNotNull(clazz);
    List<CtMethod<?>> methods = clazz.getMethodsByName(methodName);
    assertEquals(1, methods.size());
    CtMethod<?> ctMethod = methods.get(0);
    CtBlock<?> ctBody = (CtBlock<?>) ctMethod.getBody();
    Assert.assertNotNull(ctBody);
    List<CtStatement> ctStatements = ctBody.getStatements();
    assertEquals(1, ctStatements.size());
    CtStatement ctStatement = ctStatements.get(0);
    Assert.assertTrue(ctStatement instanceof CtAbstractInvocation<?>);
    return (CtAbstractInvocation<?>) ctStatement;
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) ExecutableRefTestSource(spoon.test.executable.testclasses.ExecutableRefTestSource) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod)

Example 54 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class ReplaceParametrizedTest method testContract.

@Test
public void testContract() throws Throwable {
    List<String> problems = new ArrayList<>();
    // contract: all elements are replaceable wherever they are in the model
    // this test puts them at all possible locations
    CtType<?> toTest = typeToTest.getModelInterface();
    CtElement o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
    for (MetamodelProperty mmField : typeToTest.getRoleToProperty().values()) {
        Class<?> argType = mmField.getItemValueType().getActualClass();
        if (!CtElement.class.isAssignableFrom(argType)) {
            continue;
        }
        CtTypeReference<?> itemType = mmField.getItemValueType();
        // special cases...
        if (itemType.getQualifiedName().equals(CtStatement.class.getName())) {
            // the children of CtLoop wraps CtStatement into an implicit CtBlock. So make a block directly to test plain get/set and not wrapping.
            itemType = factory.createCtTypeReference(CtBlock.class);
        }
        if (o.getClass().getSimpleName().equals("CtAnnotationFieldAccessImpl") && mmField.getRole() == CtRole.VARIABLE) {
            itemType = factory.createCtTypeReference(CtFieldReference.class);
        } else if (CtFieldAccess.class.isAssignableFrom(o.getClass()) && mmField.getRole() == CtRole.VARIABLE) {
            itemType = factory.createCtTypeReference(CtFieldReference.class);
        }
        CtElement argument = (CtElement) createCompatibleObject(itemType);
        assertNotNull(argument);
        // we create a fresh object
        CtElement receiver = ((CtElement) o).clone();
        RoleHandler rh = RoleHandlerHelper.getRoleHandler(o.getClass(), mmField.getRole());
        if (mmField.isUnsettable()) {
            try {
                // we invoke the setter
                invokeSetter(rh, receiver, argument);
            } catch (SpoonException e) {
                // ok this unsettable property has no setter at all
                return;
            }
            // this unsettable property has setter, but it should do nothing
            CtRole argumentsRoleInParent = argument.getRoleInParent();
            if (argumentsRoleInParent == null) {
                // OK - unsettable property set no value
                continue;
            }
            if (argumentsRoleInParent == mmField.getRole()) {
                problems.add("UnsettableProperty " + mmField + " sets the value");
            } else {
                if (mmField.isDerived()) {
                // it is OK, that setting of value into derived unsettable field influences other field
                // Example 1: CtCatchVariable.setType(x) influences result of getMultitype()
                // Example 2: CtEnumValue.setAssignment(x) influences result of getDefaultExpression()
                } else {
                    problems.add("UnsettableProperty " + mmField + " sets the value into different role " + argumentsRoleInParent);
                }
            }
            continue;
        }
        // we invoke the setter
        invokeSetter(rh, receiver, argument);
        // contract: a property setter sets properties that are visitable by a scanner
        CtElement finalArgument = argument;
        class Scanner extends CtScanner {

            boolean found = false;

            @Override
            public void scan(CtRole role, CtElement e) {
                super.scan(role, e);
                if (e == finalArgument) {
                    if (rh.getRole() == role || rh.getRole().getSuperRole() == role) {
                        found = true;
                        return;
                    }
                    // if (rh.getRole()==CtRole.TYPE && role==CtRole.MULTI_TYPE) {
                    // //CtCatchVaraible#type sets CtCatchVaraible#multiType - OK
                    // found = true;
                    // return;
                    // }
                    problems.add("Argument was set into " + rh.getRole() + " but was found in " + role);
                }
            }
        }
        ;
        Scanner s = new Scanner();
        receiver.accept(s);
        assertTrue("Settable field " + mmField.toString() + " should set value.\n" + getReport(problems), s.found);
        // contract: a property getter on the same role can be used to get the value back
        assertSame(argument, invokeGetter(rh, receiver));
        final CtElement argument2 = argument.clone();
        assertNotSame(argument, argument2);
        // we do the replace
        argument.replace(argument2);
        // the new element is indeed now in this AST
        assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new Filter<CtElement>() {

            @Override
            public boolean matches(CtElement element) {
                return element == argument2;
            }
        }).size() == 1);
    }
    if (problems.size() > 0) {
        fail(getReport(problems));
    }
}
Also used : CtScanner(spoon.reflect.visitor.CtScanner) SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) RoleHandler(spoon.reflect.meta.RoleHandler) ArrayList(java.util.ArrayList) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Filter(spoon.reflect.visitor.Filter) MetamodelProperty(spoon.test.metamodel.MetamodelProperty) CtRole(spoon.reflect.path.CtRole) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 55 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class ReplaceTest method testReplaceStatement.

@Test
public void testReplaceStatement() {
    CtMethod<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo").getMethod("foo");
    Assert.assertTrue(sample.getBody().getStatement(0) instanceof CtVariable);
    CtStatement replacement = factory.Core().createInvocation();
    sample.getBody().getStatement(0).replace(replacement);
    Assert.assertTrue(sample.getBody().getStatement(0) instanceof CtInvocation);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtStatement(spoon.reflect.code.CtStatement) CtVariable(spoon.reflect.declaration.CtVariable) 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