Search in sources :

Example 6 with Tacos

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

the class ReplaceTest method testReplaceExecutableReferenceByAnotherOne.

@Test
public void testReplaceExecutableReferenceByAnotherOne() throws Exception {
    // contract: replace an executable reference to another one in an invocation.
    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 CtExecutableReference oldExecutable = inv.getExecutable();
    final CtExecutableReference<Object> newExecutable = factory.Executable().createReference("void java.io.PrintStream#print(java.lang.String)");
    assertSame(oldExecutable, inv.getExecutable());
    oldExecutable.replace(newExecutable);
    assertSame(newExecutable, inv.getExecutable());
    assertEquals("print(java.lang.String)", inv.getExecutable().toString());
    assertEquals("java.io.PrintStream", inv.getExecutable().getDeclaringType().toString());
    // contract: replace of single value by multiple values in single value field must fail
    try {
        newExecutable.replace(Arrays.asList(oldExecutable, null));
        fail();
    } catch (InvalidReplaceException e) {
    // OK
    }
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Tacos(spoon.test.replace.testclasses.Tacos) InvalidReplaceException(spoon.support.visitor.replace.InvalidReplaceException) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 7 with Tacos

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

the class ParentTest method testParentOfCtExecutableReference.

@Test
public void testParentOfCtExecutableReference() throws Exception {
    // contract: parent of a executable reference is the element which call getExecutable().
    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 CtExecutableReference oldExecutable = inv.getExecutable();
    assertNotNull(oldExecutable.getParent());
    assertEquals(inv, oldExecutable.getParent());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) 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