Blog

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> {
    @Override
    public void select(E entity){super.select(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 concertSelectedEvent.fire(new Concert()) triggers ConcertDetailsView.select().
But when I get injected @EntitySelected Event<City> citySelectedEvent, then citySelectedEvent.fire(new City()) DOES NOT get observed by CityDetailsView.
I guess it's because AbstractTranslatableDetailsView.select(E entity) does not have @Observes and signature of AbstractDetailsView.select 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 AbstractDetailsView.select(), since it is overriden by AbstractTranslatableDetailsView. And since the overriding method does not have @Observers, it is not an observer method.

No comments:

Post a Comment