Search in sources :

Example 1 with CtVariableRead

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

the class ReplaceTest method testReplaceAParameterReferenceToFieldReference.

@Test
public void testReplaceAParameterReferenceToFieldReference() throws Exception {
    // contract: replace a parameter reference to a field reference.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
    final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
    final CtFieldReference<?> aFieldReference = aTacos.getField("field").getReference();
    assertEquals(aParameterReference, variableRead.getVariable());
    assertEquals("java.lang.System.err.println(param)", inv.toString());
    aParameterReference.replace(aFieldReference);
    assertEquals(aFieldReference, variableRead.getVariable());
    assertEquals("java.lang.System.err.println(field)", inv.toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtParameterReference(spoon.reflect.reference.CtParameterReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtVariableRead(spoon.reflect.code.CtVariableRead) Test(org.junit.Test)

Example 2 with CtVariableRead

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

the class ParentTest method testParentOfCtVariableReference.

@Test
public void testParentOfCtVariableReference() throws Exception {
    // contract: parent of a variable reference is the element which call getVariable().
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
    final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
    assertNotNull(aParameterReference.getParent());
    assertEquals(variableRead, aParameterReference.getParent());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtParameterReference(spoon.reflect.reference.CtParameterReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtVariableRead(spoon.reflect.code.CtVariableRead) Test(org.junit.Test)

Example 3 with CtVariableRead

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

the class AccessTest method testCanVisitVariableAccessAndSubClasses.

@Test
public void testCanVisitVariableAccessAndSubClasses() throws Exception {
    final Factory factory = build(VariableAccessSample.class);
    final List<CtVariableRead<?>> variablesRead = Query.getElements(factory, new AbstractFilter<CtVariableRead<?>>(CtVariableRead.class) {

        @Override
        public boolean matches(CtVariableRead<?> element) {
            return super.matches(element);
        }
    });
    // System.out and s
    assertEquals(2, variablesRead.size());
    final List<CtVariableWrite<?>> variablesWrite = Query.getElements(factory, new AbstractFilter<CtVariableWrite<?>>(CtVariableWrite.class) {

        @Override
        public boolean matches(CtVariableWrite<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(1, variablesWrite.size());
    final List<CtVariableAccess<?>> variablesAccess = Query.getElements(factory, new AbstractFilter<CtVariableAccess<?>>(CtVariableAccess.class) {

        @Override
        public boolean matches(CtVariableAccess<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(3, variablesAccess.size());
}
Also used : CtVariableWrite(spoon.reflect.code.CtVariableWrite) CtVariableAccess(spoon.reflect.code.CtVariableAccess) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtVariableRead(spoon.reflect.code.CtVariableRead) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 4 with CtVariableRead

use of spoon.reflect.code.CtVariableRead in project dspot by STAMP-project.

the class AssertionRemover method removeAssertion.

/**
 * Replaces an invocation with its arguments.
 *
 * @param invocation Invocation
 */
public void removeAssertion(CtInvocation<?> invocation) {
    final Factory factory = invocation.getFactory();
    invocation.getArguments().forEach(argument -> {
        CtExpression clone = ((CtExpression) argument).clone();
        if (clone instanceof CtUnaryOperator) {
            clone = ((CtUnaryOperator) clone).getOperand();
        }
        if (clone instanceof CtStatement) {
            clone.getTypeCasts().clear();
            invocation.insertBefore((CtStatement) clone);
        } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
            CtTypeReference<?> typeOfParameter = clone.getType();
            if (clone.getType().equals(factory.Type().NULL_TYPE)) {
                typeOfParameter = factory.Type().createReference(Object.class);
            }
            final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
            invocation.insertBefore(localVariable);
        }
    });
    CtElement currentParent = invocation;
    while (!(currentParent.getParent() instanceof CtStatementList)) {
        currentParent = currentParent.getParent();
    }
    ((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtStatementList(spoon.reflect.code.CtStatementList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtVariableRead(spoon.reflect.code.CtVariableRead)

Example 5 with CtVariableRead

use of spoon.reflect.code.CtVariableRead in project dspot by STAMP-project.

the class GeneralMinimizer method removeUselessDuplicateAssertions.

private void removeUselessDuplicateAssertions(CtMethod<?> amplifiedTestToBeMinimized, CtInvocation<?> duplicatesAssertion, List<CtStatement> statements) {
    final CtVariableReference variable = ((CtVariableRead<?>) duplicatesAssertion.filterChildren(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class)).first()).getVariable();
    boolean canBeRemoved = true;
    for (int i = statements.indexOf(duplicatesAssertion) + 1; i < statements.lastIndexOf(duplicatesAssertion); i++) {
        if (!AmplificationChecker.isAssert(statements.get(i))) {
            final CtVariableRead<?> first = (CtVariableRead<?>) statements.get(i).filterChildren(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class) {

                @Override
                public boolean matches(CtVariableRead<?> element) {
                    return element.getVariable().equals(variable);
                }
            }).first();
            if (first != null) {
                canBeRemoved = false;
                break;
            }
        }
    }
    if (canBeRemoved) {
        amplifiedTestToBeMinimized.getBody().getStatements().remove(statements.lastIndexOf(duplicatesAssertion));
    }
}
Also used : CtVariableReference(spoon.reflect.reference.CtVariableReference) CtVariableRead(spoon.reflect.code.CtVariableRead)

Aggregations

CtVariableRead (spoon.reflect.code.CtVariableRead)5 Factory (spoon.reflect.factory.Factory)4 Test (org.junit.Test)3 CtInvocation (spoon.reflect.code.CtInvocation)2 CtParameterReference (spoon.reflect.reference.CtParameterReference)2 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 Tacos (spoon.test.replace.testclasses.Tacos)2 CtExpression (spoon.reflect.code.CtExpression)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtStatement (spoon.reflect.code.CtStatement)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CtUnaryOperator (spoon.reflect.code.CtUnaryOperator)1 CtVariableAccess (spoon.reflect.code.CtVariableAccess)1 CtVariableWrite (spoon.reflect.code.CtVariableWrite)1 CtElement (spoon.reflect.declaration.CtElement)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1 CtVariableReference (spoon.reflect.reference.CtVariableReference)1 MainTest (spoon.test.main.MainTest)1