}
}
@Test
public void testTryFinally() throws Exception {
final MutableInteger counter = new MutableInteger(0);
final Expression counterConstant = constant(counter);
final ParameterExpression shouldThrow = parameter(PrimitiveTypes.Boolean);
final LambdaExpression<ShouldThrowDelegate> lambda = lambda(
Type.of(ShouldThrowDelegate.class),
tryFinally(
call(Type.of(CompilerTests.class), "maybeThrow", shouldThrow),
call(counterConstant, "increment")
),
shouldThrow
);
System.out.println();
System.out.println(lambda);
final ShouldThrowDelegate delegate = lambda.compile();
System.out.printf("\n[%s]\n", delegate.getClass().getSimpleName());
try {
delegate.maybeThrow(false);
}
catch (Throwable t) {
fail("Exception should not have been thrown.");
}
assertEquals(1, counter.getValue());
counter.setValue(0);
try {
delegate.maybeThrow(true);
fail("Exception should have been thrown.");
}
catch (Throwable ignored) {
}
assertEquals(1, counter.getValue());
}