Search in sources :

Example 1 with Tacos

use of spoon.test.replace.testclasses.Tacos 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 Tacos

use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.

the class ReplaceTest method testReplaceIntegerReference.

@Test
public void testReplaceIntegerReference() throws Exception {
    // contract: replace a primitive type reference by another one.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtMethod<?> aMethod = aTacos.getMethodsByName("m").get(0);
    assertEquals(factory.Type().INTEGER_PRIMITIVE, aMethod.getType());
    aMethod.getType().replace(factory.Type().DOUBLE_PRIMITIVE);
    assertEquals(factory.Type().DOUBLE_PRIMITIVE, aMethod.getType());
}
Also used : Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 3 with Tacos

use of spoon.test.replace.testclasses.Tacos 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 4 with Tacos

use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.

the class ParentTest method testParentOfPrimitiveReference.

@Test
public void testParentOfPrimitiveReference() throws Exception {
    // contract: parent of a primitive different isn't different of other type. Its parent is the element which used this type.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtMethod<?> aMethod = aTacos.getMethodsByName("m").get(0);
    assertNotNull(aMethod.getType().getParent());
    assertEquals(factory.Type().INTEGER_PRIMITIVE, aMethod.getType());
    assertEquals(aMethod, aMethod.getType().getParent());
}
Also used : Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 5 with Tacos

use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.

the class ReplaceTest method testReplaceAllTypeRefenceWithGenerics.

@Test
public void testReplaceAllTypeRefenceWithGenerics() throws Exception {
    // contract: replace all type references with a generic to the same type reference without generics.
    final Factory factory = build(Tacos.class);
    final List<CtTypeReference> references = Query.getElements(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference reference) {
            return reference.getActualTypeArguments().size() > 0 && super.matches(reference);
        }
    });
    references.get(0).replace(factory.Type().createReference(references.get(0).getQualifiedName()));
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtMethod<?> aMethod = aTacos.getMethodsByName("m2").get(0);
    final CtTypeReference<Object> expected = factory.Type().createReference("spoon.test.replace.testclasses.Tacos");
    assertEquals(expected, aMethod.getType());
    assertEquals(expected.getTypeDeclaration(), aMethod.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0).getType().getTypeDeclaration());
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtTypeReference(spoon.reflect.reference.CtTypeReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 Factory (spoon.reflect.factory.Factory)7 Tacos (spoon.test.replace.testclasses.Tacos)7 CtInvocation (spoon.reflect.code.CtInvocation)4 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 CtVariableRead (spoon.reflect.code.CtVariableRead)2 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)2 CtParameterReference (spoon.reflect.reference.CtParameterReference)2 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1 InvalidReplaceException (spoon.support.visitor.replace.InvalidReplaceException)1