use of spoon.test.compilation.testclasses.Bar in project spoon by INRIA.
the class CompilationTest method testNewInstanceFromExistingClass.
@Test
public void testNewInstanceFromExistingClass() throws Exception {
CtClass<Bar> barCtType = (CtClass<Bar>) ModelUtils.buildClass(Bar.class);
CtReturn<Integer> m = barCtType.getMethod("m").getBody().getStatement(0);
// we cannot use Bar because it causes a runtime cast exception (2 different Bar from different classloader)
IBar bar = barCtType.newInstance();
int value = bar.m();
assertEquals(1, value);
// change the return value
m.setReturnedExpression(m.getFactory().Code().createLiteral(2));
bar = barCtType.newInstance();
value = bar.m();
assertEquals(2, value);
m.replace(m.getFactory().Code().createCodeSnippetStatement("throw new FooEx()"));
try {
bar = barCtType.newInstance();
value = bar.m();
fail();
} catch (Exception ignore) {
}
}
Aggregations