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
}
}
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);
}
}
Aggregations