3 Replies Latest reply on Nov 3, 2010 5:52 PM by pmiklos.register-seamframework.sirokko.net

    Decorator beans hide exceptions

    pmiklos.register-seamframework.sirokko.net

      Hi,


      I found that decorator beans shadow the exceptions thrown by the decorated bean.


      Given a Foo class that needs to be decorated:



      public class Foo implements Barable {
      
           @Override
           public void bar() throws CustomRuntimeException {
                throw new CustomRuntimeException();
           }
      
      }
      



      Here is the decorator:



      @Decorator
      public class FooDecorator implements Barable {
      
           @Inject @Delegate @Any Barable foo;
           
           @Override
           public void bar() throws CustomRuntimeException {
                foo.bar();
           }
      
      }



      In this case when calling foo.bar() a java.lang.reflect.InvocationTargetException will be thrown instead of CustomRuntimeException. Although the cause of the exception is CustomRuntimeException this is not what I would expect. I could not find anything related to the handling of exceptions thrown by a decorated method in the JSR-299 spec.



      Is this by spec or is it a bug?


      Regards,


      Peter



      The corresponding stacktrace:


      java.lang.Exception: Unexpected exception, expected<org.example.CustomRuntimeException> but was<java.lang.reflect.InvocationTargetException>
           at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
           at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
           at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
           at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
           at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
           at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
           at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
           at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
           at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
           at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
           at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
           at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
           at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
           at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      Caused by: java.lang.reflect.InvocationTargetException
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304)
           at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
           at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)
           at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:298)
           at org.jboss.weld.introspector.jlr.WeldMethodImpl.invokeOnInstance(WeldMethodImpl.java:200)
           at org.jboss.weld.bean.proxy.DecoratorProxyMethodHandler.doInvoke(DecoratorProxyMethodHandler.java:96)
           at org.jboss.interceptor.util.proxy.TargetInstanceProxyMethodHandler.invoke(TargetInstanceProxyMethodHandler.java:60)
           at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
           at org.example.Foo_$$_javassist_1.bar(Foo_$$_javassist_1.java)
           at org.example.FooTest.testBar(FooTest.java:31)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
           at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
           at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
           at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
           at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:21)
           ... 17 more
      Caused by: org.example.CustomRuntimeException
           at org.example.Foo.bar(Foo.java:7)
           at org.example.FooDecorator.bar(FooDecorator.java:15)
           ... 40 more