public Object invoke(Invocation invocation) throws Throwable { if (invocation.getInvokedMethod().getDeclaringClass() == declaringType) { return invocation.applyTo(target); } else { return next.invoke(invocation); } } }
public void testCanApplyInvocationToAnotherObject() throws Throwable { Target target = new Target(); target.result = "result"; Invocation invocation = new Invocation("receiver", TargetInterface.class.getMethod("doSomething", String.class), new Object[]{"arg"}); String actualResult = (String)invocation.applyTo(target); assertEquals("received argument", "arg", target.receivedArg); assertEquals("result returned from apply", target.result, actualResult); }
public void testUnwrapsInvocationTargetExceptionsFromAppliedInvocation() throws Throwable { Target target = new Target(); target.exception = new TargetException(); Invocation invocation = new Invocation("receiver", TargetInterface.class.getMethod("doSomething", String.class), new Object[]{"arg"}); try { invocation.applyTo(target); fail("should have thrown TargetException"); } catch (TargetException ex) { // expected } } }