6 Replies Latest reply on Jan 21, 2010 2:57 PM by Nicklas Karlsson

    Problem with Using Decorator

    Michael Schuetz Novice

      Hi to all,


      I am using JBoss 6.0.0.M1 and weld-1.0.0.SP1 and have problems with using Decorators.


      Exception:




      javax.enterprise.inject.CreationException
              sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
              sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
              sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
              java.lang.reflect.Constructor.newInstance(Constructor.java:513)
              java.lang.Class.newInstance0(Class.java:355)
              java.lang.Class.newInstance(Class.java:308)
              org.jboss.weld.injection.Exceptions.rethrowException(Exceptions.java:37)
              org.jboss.weld.injection.Exceptions.rethrowException(Exceptions.java:54)
              org.jboss.weld.injection.Exceptions.rethrowException(Exceptions.java:74)
              org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:115)
              org.jboss.weld.bean.ManagedBean$1.produce(ManagedBean.java:247)
              org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:140)
              org.jboss.weld.context.DependentContext.get(DependentContext.java:62)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:958)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:1019)
              org.jboss.weld.bean.AbstractClassBean.applyDecorators(AbstractClassBean.java:204)
              org.jboss.weld.bean.ManagedBean$1.produce(ManagedBean.java:261)
              org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:140)
              org.jboss.weld.context.DependentContext.get(DependentContext.java:62)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:958)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:1019)
              org.jboss.weld.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:1041)
              org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
              org.jboss.weld.util.Beans.injectBoundFields(Beans.java:727)
              org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:739)
              org.jboss.weld.bean.ManagedBean$1$1.proceed(ManagedBean.java:219)
              org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:51)
              org.jboss.weld.bean.ManagedBean$1.inject(ManagedBean.java:213)
              org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:141)
              org.jboss.weld.context.AbstractMapContext.get(AbstractMapContext.java:108)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.getProxiedInstance(ClientProxyMethodHandler.java:143)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:101)
              weldTest.weldJsfJee.controller.PersonController_$$_javassist_20.getPersonen(PersonController_$$_javassist_20.java)
              sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              java.lang.reflect.Method.invoke(Method.java:597)
              org.jboss.weld.introspector.jlr.WeldMethodImpl.invokeOnInstance(WeldMethodImpl.java:228)
              org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstance(MethodInjectionPoint.java:148)
              org.jboss.weld.bean.ProducerMethod$1.produce(ProducerMethod.java:123)
              org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:301)
              org.jboss.weld.context.AbstractMapContext.get(AbstractMapContext.java:108)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.getProxiedInstance(ClientProxyMethodHandler.java:143)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:101)
              org.javassist.tmp.java.util.Collection_$$_javassist_19.size(Collection_$$_javassist_19.java)
              javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:110)
              javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:185)
              javax.faces.model.ListDataModel.setWrappedData(ListDataModel.java:220)
              javax.faces.model.ListDataModel.<init>(ListDataModel.java:79)
              javax.faces.component.UIData.getDataModel(UIData.java:1249)
              javax.faces.component.UIData.setRowIndex(UIData.java:447)
              com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
              javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:823)
              javax.faces.component.UIData.encodeBegin(UIData.java:932)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1608)
              javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
              javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1610)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
              com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:379)
              com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
              javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
              com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
              com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
              com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
              javax.faces.webapp.FacesServlet.service(FacesServlet.java:311)
              org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:68)
              org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      
      root cause
      
      java.lang.InstantiationException
              sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
              java.lang.reflect.Constructor.newInstance(Constructor.java:513)
              org.jboss.weld.introspector.jlr.WeldConstructorImpl.newInstance(WeldConstructorImpl.java:246)
              org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:107)
              org.jboss.weld.bean.ManagedBean$1.produce(ManagedBean.java:247)
              org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:140)
              org.jboss.weld.context.DependentContext.get(DependentContext.java:62)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:958)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:1019)
              org.jboss.weld.bean.AbstractClassBean.applyDecorators(AbstractClassBean.java:204)
              org.jboss.weld.bean.ManagedBean$1.produce(ManagedBean.java:261)
              org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:140)
              org.jboss.weld.context.DependentContext.get(DependentContext.java:62)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:958)
              org.jboss.weld.BeanManagerImpl.getReference(BeanManagerImpl.java:1019)
              org.jboss.weld.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:1041)
              org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
              org.jboss.weld.util.Beans.injectBoundFields(Beans.java:727)
              org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:739)
              org.jboss.weld.bean.ManagedBean$1$1.proceed(ManagedBean.java:219)
              org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:51)
              org.jboss.weld.bean.ManagedBean$1.inject(ManagedBean.java:213)
              org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:141)
              org.jboss.weld.context.AbstractMapContext.get(AbstractMapContext.java:108)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.getProxiedInstance(ClientProxyMethodHandler.java:143)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:101)
              weldTest.weldJsfJee.controller.PersonController_$$_javassist_20.getPersonen(PersonController_$$_javassist_20.java)
              sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              java.lang.reflect.Method.invoke(Method.java:597)
              org.jboss.weld.introspector.jlr.WeldMethodImpl.invokeOnInstance(WeldMethodImpl.java:228)
              org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstance(MethodInjectionPoint.java:148)
              org.jboss.weld.bean.ProducerMethod$1.produce(ProducerMethod.java:123)
              org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:301)
              org.jboss.weld.context.AbstractMapContext.get(AbstractMapContext.java:108)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.getProxiedInstance(ClientProxyMethodHandler.java:143)
              org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:101)
              org.javassist.tmp.java.util.Collection_$$_javassist_19.size(Collection_$$_javassist_19.java)
              javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:110)
              javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:185)
              javax.faces.model.ListDataModel.setWrappedData(ListDataModel.java:220)
              javax.faces.model.ListDataModel.<init>(ListDataModel.java:79)
              javax.faces.component.UIData.getDataModel(UIData.java:1249)
              javax.faces.component.UIData.setRowIndex(UIData.java:447)
              com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
              javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:823)
              javax.faces.component.UIData.encodeBegin(UIData.java:932)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1608)
              javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
              javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1610)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
              javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
              com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:379)
              com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
              javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
              com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
              com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
              com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
              javax.faces.webapp.FacesServlet.service(FacesServlet.java:311)
              org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:68)
              org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)







             
              Controller:
              ...
              @Inject
              private MyLogger myLogger;
              ...


           
       



      public class LogFactory {
      
              @Produces
              Logger createLogger(InjectionPoint injectionPoint) {
                      return Logger.getLogger(injectionPoint.getMember().getDeclaringClass()
                                      .getName());
              }
      
      }






             
              public interface MyLogger extends Serializable {
      
              public void info(String msg);
      






      public class MyLoggerImpl implements MyLogger {
      
              /** Injects default logger. */
              @Inject
              private Logger log;
      
              @Override
              public void info(String msg) {
                      log.info("--- logger called, msg: " + msg);
              }
      
      }






             
              @Decorator
      public abstract class MyLoggerDecorator implements MyLogger {
      
              /** Delegate for all MyLogger Implementations. */
              @Inject
              @Delegate
              @Any
              private MyLogger myLogger;
      
              public void info(String msg) {
                      myLogger.info("--- decorated with message: " + msg);
              }
      }
      


      Without decorator, everything works fine.
      Any idea? Thanks for the help.
             


      regards,
      Michael

        • 1. Re: Problem with Using Decorator
          Nicklas Karlsson Master

          What does the PersonController constructor look like?

          • 2. Re: Problem with Using Decorator
            Michael Schuetz Novice

            Nicklas Karlsson wrote on Jan 21, 2010 11:10:


            What does the PersonController constructor look like?


            I use default constructor. When I add noarg contructor explicitely with log statements, I see the log statement and after that the stated exception occurs. The Code works fine if I disable Decorator in beans.xml. So IMO there must be a problem with the way I use/inject the decorator.


            Here is the complete PersonController code:


            package incubator.weldJsfJee.controller;
            
            import incubator.weldJsfJee.em.WeldEntityManager;
            import incubator.weldJsfJee.event.PersonCreated;
            import incubator.weldJsfJee.model.Person;
            import incubator.weldJsfJee.tx.Transactional;
            import incubator.weldJsfJee.util.log.MyLogger;
            
            import java.io.Serializable;
            import java.util.List;
            
            import javax.enterprise.context.RequestScoped;
            import javax.enterprise.event.Event;
            import javax.enterprise.inject.Produces;
            import javax.inject.Inject;
            import javax.inject.Named;
            import javax.persistence.EntityManager;
            
            @GuiController
            public class PersonController implements Serializable {
            
                 @Inject
                 @WeldEntityManager
                 private EntityManager em;
            
                 @Inject
                 private MyLogger myLogger;
            
                 @Inject
                 @PersonCreated
                 private Event<Person> personCreatedEvent;
            
                 @Inject
                 private Person person;
            
                 private Long selectedPersonId;
            
                 @Produces
                 @Named
                 @RequestScoped
                 @SuppressWarnings("unchecked")
                 public List<Person> getPersons() {
                      return em.createQuery("select o from Person o order by o.name")
                                .getResultList();
                 }
            
                 @Transactional
                 public void createPerson() throws Exception {
                      Person p = new Person(person);
                      em.persist(p);
                      personCreatedEvent.fire(p);
                 }
            
                 public void selectPerson() {
                      myLogger.info("person selected with id " + selectedPersonId);
                 }
            
                 public Long getSelectedPersonId() {
                      return selectedPersonId;
                 }
            
                 public void setSelectedPersonId(Long selectedPersonId) {
                      this.selectedPersonId = selectedPersonId;
                 }
            
                 public EntityManager getEm() {
                      return em;
                 }
            }





            GuiController-Stereotype:


            @Named
            @SessionScoped
            @Documented
            @Stereotype
            @Target( { TYPE, METHOD, FIELD })
            @Retention(RUNTIME)
            public @interface GuiController {}




            Thanks for any help,


            Michael

            • 3. Re: Problem with Using Decorator
              Nicklas Karlsson Master

              The exception appears to be targetted at the


                      @Produces
                   @Named
                   @RequestScoped
                   @SuppressWarnings("unchecked")
                   public List<Person> getPersons() {
                        return em.createQuery("select o from Person o order by o.name")
                                  .getResultList();
                   }
              

              • 4. Re: Problem with Using Decorator
                Nicklas Karlsson Master

                Is the decorator supposed to be abstract?

                • 5. Re: Problem with Using Decorator
                  Michael Schuetz Novice

                  Nicklas Karlsson wrote on Jan 21, 2010 13:32:


                  Is the decorator supposed to be abstract?



                  Great, Nicklas! After not marking decorater abstract, everything works fine.
                  Hm, that's a little bit confusing, because alle examples in weld reference, Chapter 10 refer to an abstract decorator class:



                  A decorator is a bean (possibly even an abstract class) that implements the type it decorates and is annotated @Decorator.


                  So what does possibly even an abstract class mean, exactly?



                  Regards,


                  Michael



                  • 6. Re: Problem with Using Decorator
                    Nicklas Karlsson Master

                    It should be able to be abstract. The producer method getPersons() is called so the declaring bean PersonController is instantiated and the decorators applied. The stack traces mentions a constructor injection point but I don't see any in your code...