4 Replies Latest reply on Jan 25, 2012 10:16 AM by bitec

    CDI events and generics

    bitec

      I'm trying to send events and do this generically. I mean - create one abstract base DAO class with generic type and fire the event from its method. This should work for all descendants. This works if I define the exact type, but doesn't - if I use generics. What I mean:


      AbstractDAO (with generics - doesn't fire the event):



      public abstract class AbstractDAO<T extends Persistable> implements Serializable {
         @Inject @PostSaveEvent Event<T> postSaveEvent;
      
         public T saveOrUpdate(T object) throws DatabaseException {
            T obj = em.merge(object);
      
            postSaveEvent.fire(obj);
         }
      }


      AbstractDAO (no generics, just simple class cast - works ok):



      public abstract class AbstractDAO<T extends Persistable> implements Serializable {
         @Inject @PostSaveEvent Event<Polis> postSaveEvent;
      
         public T saveOrUpdate(T object) throws DatabaseException {
            T obj = em.merge(object);
      
            postSaveEvent.fire((Polis)obj);
         }
      }
      




      His is very strange for me, as fire() method for CDI event should define the event type on runtime, not during compilation or deployment... Both samples are the same on runtime, they operate with totally the same object, but in the first template I don't define the exact type and in the second one - do.


      Thanks