[webbeans-dev] removeObserver type

Gavin King gavin.king at gmail.com
Wed May 27 17:07:51 EDT 2009


On Wed, May 27, 2009 at 1:47 PM, Clint Popetz <cpopetz at gmail.com> wrote:

>> (2) adding a type variable doesn't help you, since it just gets erased
>> at runtime
>>
>> You can't do T.class in Java :-)
>
> Good point.  Is the parameterization of addObserver just to ensure that
> Observer<T> is registered for Class<T>?

yes, exactly.

So now I'm wondering (again) whether the TypeLiteral/Class/Type on
addObserver() is really adding any value here.

It is there to accommodate two cases:

(1) where the event type that you are registering an observer for is a
type variable, in which case you could potentially have a Class<T> or
a TypeLiteral<T>, which hold an actual value for T at runtime, but an
Observer<T> which is generic and does not.

(2) If Foo extends Bar, I was thinking that you could register an
Observer<Bar> for an event of type Foo. However, I now notice that the
type declarations don't actually support that (though this could by
fixed easily).

I don't think (2) is a very important case, since it is trivial to
wrap Observer<Bar> in Observer<Foo>.

Case (1) is a little more subtle, but it seems like I can't really
think of a real usecase for it.

I think we could reasonably have a single addObserver() method as follows:

    public <T> void addObserver(Observer<T> observer, Annotation... bindings);



-- 
Gavin King
gavin.king at gmail.com
http://in.relation.to/Bloggers/Gavin
http://hibernate.org
http://seamframework.org




More information about the weld-dev mailing list