Alternative for Class.isAssignableFrom() ?

1,360 views
Skip to first unread message

Dominik Steiner

unread,
Feb 18, 2010, 9:58:43 PM2/18/10
to Google Web Toolkit
Hi there,

I'm trying to implement an event bus that will receive register
listeners like this

public <T extends Event> void registerListener(Class<T>
eventClassname, EventListener<T> eventListener);
(Event is a custom interface)

what i would like then to achieve would be to be able to fire an event

public void fireEvent(Event event);

and be able to trigger all my event listeners that either are of the
same event.getClass() or that are assignable from the event class that
has been fired. Getting all the listeners that correspond to the event
class itself is no problem, but how can i achieve getting all the
listeners that registered to a interface that the event class might
implement without being able to use Class.isAssignableFrom()?

Thanks for any hints and help in advance.

Dominik

Chris Lercher

unread,
Feb 19, 2010, 6:43:34 AM2/19/10
to Google Web Toolkit
The JRE Emulation Reference says, that getSuperclass() is supported.
http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html
Maybe you can make do with this?

Chris

On Feb 19, 3:58 am, Dominik Steiner <dominik.j.stei...@googlemail.com>
wrote:

Nathan Wells

unread,
Feb 19, 2010, 8:36:37 AM2/19/10
to Google Web Toolkit
I think you'll probably end up implementing your own version of a map
that would look something like this:

Map<Class<?>, List<Listener<?>> handledEventClasses;

<T extends Event> List<Listener<T>> get(Class<T> eventClass) {
List<Listenter<T>> result = new ArrayList<Listener<T>>
for ( Entry<Class<T>, List<Listener<T> entry :
handledEventClasses.entrySet() ) {
if ( eventClass.isAssignableFrom(entry.getKey()) {
result.addAll((List<Listener<T>>)entry.getValue());
}
}
return result;
}

Of course, I neglect any null checking, and I don't know if that will
compile or not, but I think you can follow the basic logic.

Also, this method will probably need to be better optimized since it
runs in linear time according to the number of event classes you
register. This could be done by making the put method intelligent
enough to put the listeners into bins for each class you register.


On Feb 19, 4:43 am, Chris Lercher <cl_for_mail...@gmx.net> wrote:
> The JRE Emulation Reference says, that getSuperclass() is supported.http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html

Dominik Steiner

unread,
Feb 19, 2010, 10:09:12 AM2/19/10
to google-we...@googlegroups.com
@Chris,

yes, thanks for the tip, I already tried that by getSuperclass() will
not get any classes that are implemented by this class but only a
superclass. So I would not be able to use interfaces but only extend
from a base class to which i could also attach a listener too and that
would be triggered if a subclass event would be fired, but that's not
the same than having the power and flexibility of interfaces.

@Nathan,

your solution is what I tried first and my junit tests would run just
fine with this but GWT doesn't implement the isAssignableFrom()
method, so that's why I'm looking for some alternatives if there are?

Thanks again for your great help and perhaps there are other solutions
out there still?

Dominik

> --
> You received this message because you are subscribed to the Google
> Groups "Google Web Toolkit" group.
> To post to this group, send email to google-we...@googlegroups.com
> .
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en
> .
>

Dominik Steiner

unread,
Feb 22, 2010, 10:24:26 AM2/22/10
to Google Web Toolkit
I added an issue to the issue tracker if you also are in need of that
method and want to star it

http://code.google.com/p/google-web-toolkit/issues/detail?id=4663

Dominik

On 19 Feb., 09:09, Dominik Steiner <dominik.j.stei...@googlemail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages