Get bound TypeListeners

23 views
Skip to first unread message

Alex

unread,
Mar 26, 2012, 10:41:25 PM3/26/12
to google-guice
I hope I haven't missed anything obvious but is there a way to get the
bound TypeListeners for an Injector? I have poked around with
Injector.getAllBindings() which in turn provides access to the
Providers but I don't ever see my bound listeners. The only reason I
want to access this information is to be able to easily glean
information about my Guice configuration and what exactly is bound
through the Injector. I am ultimately hoping to expose this through a
simple web interface for environment testing and debugging.

I even looked at the Grapher but that didn't seem to provide what I
wanted either, assuming once again that I didn't miss something.

Thanks!

Sam Berlin

unread,
Mar 26, 2012, 10:44:50 PM3/26/12
to google...@googlegroups.com
I don't believe there's currently a way.  Injector is missing (and in need of) a getElements() method similar to Elements.getElements(Module).

 sam


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


Christian Edward Gruber

unread,
Mar 27, 2012, 10:45:23 AM3/27/12
to google...@googlegroups.com
Do we have an existing issue for getElements() outstanding?

Christian.

Sam Berlin

unread,
Mar 27, 2012, 11:11:22 AM3/27/12
to google...@googlegroups.com
Hmmm, I don't think so.

 sam

Alex

unread,
Mar 29, 2012, 2:36:26 PM3/29/12
to google...@googlegroups.com
FYI, I am ashamed to admit but as a terrible workaround I simply use reflection to get at the TypeListenerBindings. It would be nice though if these were included from the call to getAllBindings.

Injector injector = Guice.createInjector(...);

Class injectorImplClass = Class.forName("com.google.inject.internal.InjectorImpl");
Field field = injectorImplClass.getDeclaredField("state");
field.setAccessible(true);

Object state = field.get(injector);

Class stateClass = Class.forName("com.google.inject.internal.State");
Method method = stateClass.getDeclaredMethod("getTypeListenerBindings");
method.setAccessible(true);

for (final TypeListenerBinding typeListenerBinding : (List<TypeListenerBinding>) method.invoke(state)) {
    // Do something with the TypeListenerBinding

Sam Berlin

unread,
Mar 29, 2012, 3:15:49 PM3/29/12
to google...@googlegroups.com
getAllBindings would be incorrect because that returns Bindings, not Elements (and TypeListeningBinding is an Element, not a Binding, despite its name).  But you're right that we should expose all the Elements from the Injector).

 sam

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/9tSPMiAyFhAJ.
Reply all
Reply to author
Forward
0 new messages