Friday, 5 October 2012

@Observes inheritance

Weld-core:1.1.8.Final fixed important issue (WELD-1111) concerning inheritance of member-level metadata for generic types. Now we can have parametrized super classes with @Observes methods!

Imagine that we're having many similar views backed by beans, so we stuff most of logic into parametrized abstract superclass. Until Weld-core:1.1.8.Final we had to overwrite all observer methods in each concrete class just to put @Observes on them. Let's consider following example
public abstract class AbstractDetailsView<E> implements Serializable {
    public void select(@Observes(notifyObserver = Reception.IF_EXISTS)  @EntitySelected final E entity){...}
public abstract class AbstractTranslatableDetailsView<E extends Translatable<T>, T> extends AbstractDetailsView<E> {
    public void select(E entity){;...}
public class CityDetailsView extends AbstractTranslatableDetailsView<City, CityTranslation> implements Serializable {}
public class ConcertDetailsView extends AbstractDetailsView<Concert> implements Serializable {}
Now, when I get injected @EntitySelected Event<Concert> concertSelectedEvent, then Concert()) triggers
But when I get injected @EntitySelected Event<City> citySelectedEvent, then City()) DOES NOT get observed by CityDetailsView.
I guess it's because entity) does not have @Observes and signature of is ignored.

Is this as expected?

Marko Lukša says
Yes, this is expected.

The spec says:

If X declares an ... observer ... method x() then Y inherits x() if and only if neither Y nor any intermediate class that is a subclass of X and a superclass of Y overrides the method x().

So CityDetailsView does not inherit the method, since it is overriden by AbstractTranslatableDetailsView. And since the overriding method does not have @Observers, it is not an observer method.