TypeListener.hear not being called when using requestStaticInjection?

23 views
Skip to first unread message

Michael Burton

unread,
Nov 10, 2009, 5:46:30 PM11/10/09
to google-guice
I have a custom TypeListener, but its hear() method doesn't seem to
ever get triggered when I register classes for static injection using
requestStaticInjection in my module. Is this expected behavior in
guice 2.0 no-aop? Do I need to do anything special?

Cheers,
Mike

Michael Burton

unread,
Nov 10, 2009, 5:48:30 PM11/10/09
to google-guice
I neglected to mention that I registered the TypeListener using
bindListener(Matchers.any(), ...)

limpb...@gmail.com

unread,
Nov 10, 2009, 9:26:09 PM11/10/09
to google-guice
Looks like a bug in our documentation. As they were designed,
TypeListeners aren't appropriate for static injection. They are
intended to expose facilities for per-instance callbacks
(InjectionListener) and per-instance injections (MembersInjector),
neither of which is appropriate for static injection.

What did you have in mind?

Michael Burton

unread,
Nov 13, 2009, 7:26:34 PM11/13/09
to google...@googlegroups.com
Ah, that would explain it then. It's certainly unexpected that they wouldn't work for static injection -- is that a technical limitation? I've created my own custom injection annotations (@InjectResource) which I use to inject Android resources into objects. I wasn't able to use the standard @Inject annotation because android resources are identified with an int identifier, and there's no way to pass the value of an annotation to a provider in guice 2.0, at least. Here's an example: @InjectResource(R.id.network_error_msg) protected String networkErrorMsg; Like any other injected objects, these are usually done as non-static members, but occasionally when dealing with legacy code it is necessary to inject these values into static fields. So I tried injecting them statically into a legacy singleton I had, and you know the rest. From some conversations on the group it seems like TypeListeners are the preferred mechanism to define custom injection mechanisms like the one I mention above. As such, it seems like it would be useful for them to also support static injection. Does that sound reasonable? Cheers, Mike

Michael Burton

unread,
Nov 20, 2009, 6:24:20 PM11/20/09
to google-guice
Just thought I'd check in and see if you had any comments on this. I
think supporting requestStaticInjection for typelisteners would be a
natural extension to the typelistener functionality, and not having it
means you can't use custom injection annotations when trying to
support legacy singletons.

I'd like to be able to support this for users of my libraries. Is
there another mechanism I can plug into in order to work around this
limitation?

Cheers,
Mike

limpb...@gmail.com

unread,
Nov 21, 2009, 2:09:49 AM11/21/09
to google-guice
On Nov 20, 3:24 pm, Michael Burton <m...@niskala.org> wrote:
> I'd like to be able to support this for users of my libraries.  Is
> there another mechanism I can plug into in order to work around this
> limitation?

You can setup your own static injection, and have that delegate.
@Inject static void doMyInjections() {
Foo.staticSetup();
Bar.staticSetup();
}

All static injection knows is the set of classes specified by calls to
requestStaticInjection(). You probably don't need this list to do your
own static configuration; the statically Guice-injected classes are
probably unrelated to the classes you want to hear about.
Reply all
Reply to author
Forward
0 new messages