Search in sources :

Example 1 with InvalidReplaceException

use of spoon.support.visitor.replace.InvalidReplaceException 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 2 with InvalidReplaceException

use of spoon.support.visitor.replace.InvalidReplaceException in project spoon by INRIA.

the class ReplacementVisitor method replaceElementIfExist.

private void replaceElementIfExist(CtElement candidate, ReplaceListener listener) {
    if (candidate == original) {
        CtElement val = null;
        if (replace.length > 0) {
            if (replace.length > 1) {
                throw new InvalidReplaceException("Cannot replace single value by multiple values in " + listener.getClass().getSimpleName());
            }
            val = replace[0];
        }
        if (val != null) {
            val.setParent(candidate.getParent());
        }
        listener.set(val);
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) InvalidReplaceException(spoon.support.visitor.replace.InvalidReplaceException)

Aggregations

InvalidReplaceException (spoon.support.visitor.replace.InvalidReplaceException)2 Test (org.junit.Test)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtElement (spoon.reflect.declaration.CtElement)1 Factory (spoon.reflect.factory.Factory)1 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)1 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1 Tacos (spoon.test.replace.testclasses.Tacos)1