Search in sources :

Example 16 with CtBlock

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

the class IntercessionTest method testResetCollectionInSetters.

@Test
// interesting but too fragile with conventions
@Ignore
public void testResetCollectionInSetters() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    final Factory factory = launcher.getFactory();
    launcher.getEnvironment().setNoClasspath(true);
    // interfaces.
    launcher.addInputResource("./src/main/java/spoon/reflect/code");
    launcher.addInputResource("./src/main/java/spoon/reflect/declaration");
    launcher.addInputResource("./src/main/java/spoon/reflect/reference");
    // implementations.
    launcher.addInputResource("./src/main/java/spoon/support/reflect/code");
    launcher.addInputResource("./src/main/java/spoon/support/reflect/declaration");
    launcher.addInputResource("./src/main/java/spoon/support/reflect/reference");
    launcher.buildModel();
    new IntercessionScanner(factory) {

        @Override
        protected boolean isToBeProcessed(CtMethod<?> candidate) {
            return // 
            candidate.getSimpleName().startsWith("set") && // 
            candidate.hasModifier(ModifierKind.PUBLIC) && // 
            takeSetterCollection(candidate) && // 
            avoidInterfaces(candidate) && // && avoidSpecificMethods(candidate) //
            avoidThrowUnsupportedOperationException(candidate);
        }

        private boolean takeSetterCollection(CtMethod<?> candidate) {
            final CtTypeReference<?> type = candidate.getParameters().get(0).getType();
            final List<CtTypeReference<?>> actualTypeArguments = type.getActualTypeArguments();
            return COLLECTIONS.contains(type) && actualTypeArguments.size() == 1 && actualTypeArguments.get(0).isSubtypeOf(CTELEMENT_REFERENCE);
        }

        @Override
        protected void process(CtMethod<?> element) {
            if (element.getAnnotation(UnsettableProperty.class) != null) {
                // we don't check the contracts for unsettable setters
                return;
            }
            final CtStatement statement = element.getBody().getStatement(0);
            if (!(statement instanceof CtIf)) {
                fail(log(element, "First statement should be an if to check the parameter of the setter"));
            }
            final CtIf anIf = (CtIf) statement;
            if (!createCheckNull(element.getParameters().get(0)).equals(anIf.getCondition())) {
                fail(log(element, "Condition should test if the parameter is null.\nThe condition was " + anIf.getCondition()));
            }
            if (!(anIf.getThenStatement() instanceof CtBlock)) {
                fail(log(element, "Should have a block in the if condition to have the initialization and the return."));
            }
            if (element.getParameters().get(0).getType().equals(SET_REFERENCE)) {
                if (!hasCallEmptyInv(anIf.getThenStatement(), SET_REFERENCE)) {
                    fail(log(element, "Should initilize the list with CtElementImpl#emptySet()."));
                }
            } else {
                if (!hasCallEmptyInv(anIf.getThenStatement(), LIST_REFERENCE)) {
                    fail(log(element, "Should initilize the list with CtElementImpl#emptyList()."));
                }
            }
        }

        private boolean hasCallEmptyInv(CtBlock thenStatement, CtTypeReference<? extends Collection> collectionReference) {
            if (!(thenStatement.getStatement(0) instanceof CtAssignment)) {
                return false;
            }
            final CtExpression assignment = ((CtAssignment) thenStatement.getStatement(0)).getAssignment();
            if (!(assignment instanceof CtInvocation)) {
                return false;
            }
            final CtInvocation inv = (CtInvocation) assignment;
            if (collectionReference.equals(SET_REFERENCE)) {
                if (!inv.getExecutable().getSimpleName().equals("emptySet")) {
                    return false;
                }
            } else if (collectionReference.equals(LIST_REFERENCE)) {
                if (!inv.getExecutable().getSimpleName().equals("emptyList")) {
                    return false;
                }
            }
            return true;
        }

        /**
         * Creates <code>list == null && list.isEmpty()</code>.
         *
         * @param ctParameter <code>list</code>
         */
        private CtBinaryOperator<Boolean> createCheckNull(CtParameter<?> ctParameter) {
            final CtVariableAccess<?> variableRead = factory.Code().createVariableRead(ctParameter.getReference(), true);
            final CtLiteral nullLiteral = factory.Code().createLiteral(null);
            nullLiteral.setType(factory.Type().nullType());
            final CtBinaryOperator<Boolean> checkNull = factory.Code().createBinaryOperator(variableRead, nullLiteral, BinaryOperatorKind.EQ);
            checkNull.setType(factory.Type().BOOLEAN_PRIMITIVE);
            final CtMethod<Boolean> isEmptyMethod = ctParameter.getType().getTypeDeclaration().getMethod(factory.Type().booleanPrimitiveType(), "isEmpty");
            final CtInvocation<Boolean> isEmpty = factory.Code().createInvocation(variableRead, isEmptyMethod.getReference());
            final CtBinaryOperator<Boolean> condition = factory.Code().createBinaryOperator(checkNull, isEmpty, BinaryOperatorKind.OR);
            return condition.setType(factory.Type().booleanPrimitiveType());
        }

        private String log(CtMethod<?> element, String message) {
            return message + "\nin " + element.getSignature() + "\ndeclared in " + element.getDeclaringType().getQualifiedName();
        }
    }.scan(factory.getModel().getUnnamedModule());
}
Also used : CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtAssignment(spoon.reflect.code.CtAssignment) CtExpression(spoon.reflect.code.CtExpression) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtIf(spoon.reflect.code.CtIf) CtInvocation(spoon.reflect.code.CtInvocation) CtBlock(spoon.reflect.code.CtBlock) CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ArrayList(java.util.ArrayList) List(java.util.List) CtMethod(spoon.reflect.declaration.CtMethod) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with CtBlock

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

the class InsertMethodsTest method testInsertBeforeWithBrace.

@Test
public void testInsertBeforeWithBrace() throws Exception {
    CtMethod<?> ifWithBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithBraces = ifWithBraces_m.getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithBraces.getThenStatement().insertBefore(s);
    assertTrue(ifWithBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithBraces.getThenStatement()).getStatement(0));
    assertEquals(ifWithBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 18 with CtBlock

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

the class InsertMethodsTest method testInsertAfterWithBrace.

@Test
public void testInsertAfterWithBrace() throws Exception {
    CtMethod<?> ifWithBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithBraces = ifWithBraces_m.getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithBraces.getThenStatement().insertAfter(s);
    assertTrue(ifWithBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithBraces.getThenStatement()).getStatement(1));
    assertEquals(ifWithBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 19 with CtBlock

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

the class InsertMethodsTest method testInsertAfterWithoutBrace.

@Test
public void testInsertAfterWithoutBrace() throws Exception {
    CtMethod<?> ifWithoutBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithoutBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithoutBraces = ifWithoutBraces_m.getElements(new TypeFilter<>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithoutBraces.getThenStatement().insertAfter(s);
    assertTrue(ifWithoutBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithoutBraces.getThenStatement()).getStatement(1));
    assertEquals(ifWithoutBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 20 with CtBlock

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

the class InsertMethodsTest method testInsertBeforeWithoutBrace.

@Test
public void testInsertBeforeWithoutBrace() throws Exception {
    CtMethod<?> ifWithoutBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithoutBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithoutBraces = ifWithoutBraces_m.getElements(new TypeFilter<>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithoutBraces.getThenStatement().insertBefore(s);
    assertTrue(ifWithoutBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithoutBraces.getThenStatement()).getStatement(0));
    assertEquals(ifWithoutBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Aggregations

CtBlock (spoon.reflect.code.CtBlock)37 Test (org.junit.Test)24 CtStatement (spoon.reflect.code.CtStatement)17 CtIf (spoon.reflect.code.CtIf)15 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)13 CtMethod (spoon.reflect.declaration.CtMethod)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtElement (spoon.reflect.declaration.CtElement)7 CtInvocation (spoon.reflect.code.CtInvocation)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)5 ArrayList (java.util.ArrayList)4 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)4 CtClass (spoon.reflect.declaration.CtClass)4 File (java.io.File)3 List (java.util.List)3 CtAssignment (spoon.reflect.code.CtAssignment)3 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)3 SourcePosition (spoon.reflect.cu.SourcePosition)3