CDI events and generics

2020-07-18 09:09发布

问题:

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 - fires the event):

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);
   }
}

PolisDAO class, which extends AbstractDAO and defines the generic type:

@Stateless
@Named
@PolisType
public class PolisDAO extends AbstractDAO<Polis> {
   // some methods (saveOrUpdate is not overriden!)
}

My observer class:

@Stateless
@Named
public class ProlongationService {

    public void attachProlongationToPolisOnSave(@Observes @PostSaveEvent Polis polis) throws DatabaseException {
        // ... DO smth with polis object. This is NOT called in the first case and  called in the second
    }

THis is very strange for me, as "fire()" method for CDI event should define the event type on runtime, not during compilation or deployment... When I debug, I see, that

postSaveEvent.fire(obj);

from the first sample operates exactly with Polis entity. But no event is fired nevertheless...

Upd. I tried the base generic class, but no luck:

@Inject @PostSaveEvent Event<Persistable> postSaveEvent;

Thanks.

回答1:

This should, in theory, work, however in practice inspecting the type of generic objects at runtime with Java Reflection is, at times, impossible. This is due to type erasure. IIRC the type of the concrete sub class isn't erased, so it should be possible to reconnect this, but I guess the implementation isn't doing this right now.

File this as a bug in the http://issues.jboss.org/browse/WELD issue tracker (if you are using Weld), with the classes you provide as an example and we can try to fix it.

To work around, try injecting the event into the concrete subclass, and passing it as an argument, or using an accessor method, to get it into the abstract super class.