Search in sources :

Example 1 with Bar

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) {
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Bar(spoon.test.compilation.testclasses.Bar) IBar(spoon.test.compilation.testclasses.IBar) IBar(spoon.test.compilation.testclasses.IBar) SpoonException(spoon.SpoonException) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 SpoonException (spoon.SpoonException)1 CtClass (spoon.reflect.declaration.CtClass)1 SpoonClassNotFoundException (spoon.support.SpoonClassNotFoundException)1 Bar (spoon.test.compilation.testclasses.Bar)1 IBar (spoon.test.compilation.testclasses.IBar)1