Skip to content

Commit

Permalink
Polish apache#3275 : ReferenceBeanInvocationHandler does not throw th…
Browse files Browse the repository at this point in the history
…e actual exception
  • Loading branch information
mercyblitz committed Feb 22, 2019
1 parent cb8ad13 commit 31ecb9d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
Expand Down Expand Up @@ -155,7 +156,14 @@ private ReferenceBeanInvocationHandler(ReferenceBean referenceBean) {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(bean, args);
Object result = null;
try {
result = method.invoke(bean, args);
} catch (InvocationTargetException e) {
// re-throws the actual Exception.
throw e.getTargetException();
}
return result;
}

private void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,31 @@ public void test() {

}

@Test(expected = UnsupportedOperationException.class)
public void testOnException() {

AnnotationConfigApplicationContext providerContext = new AnnotationConfigApplicationContext();

providerContext.register(ProviderConfiguration.class);

providerContext.refresh();

AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();

consumerContext.register(ConsumerConfiguration.class);

consumerContext.refresh();

ConsumerConfiguration.Child child = consumerContext.getBean(ConsumerConfiguration.Child.class);

// From Child
DemoService demoService = child.getDemoServiceFromChild();

demoService.getBox();

providerContext.close();
consumerContext.close();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public String sayName(String name) {

@Override
public Box getBox() {
return new Box() {
@Override
public String getName() {
return "MyBox";
}
};
throw new UnsupportedOperationException("For Purposes!");
}
}

0 comments on commit 31ecb9d

Please sign in to comment.