Status: New
Owner: ----
New issue 660 by [email protected]: Add utility method to get the
Set<Dependency<?>> representing injection of an object
http://code.google.com/p/google-guice/issues/detail?id=660
An object (as instance or provider) that exposes its dependencies
explicitly may wish to depend on the injection of other specific objects as
well as availability of targets of given keys. There should be a method
(in either the Dependency class itself or a utility extension) tentatively
named "dependenciesOfObject" to get the set of dependencies corresponding
to the injection of an object.
dependenciesOfObject(obj) would be equivalent to
Dependency.forInjectionPoints(InjectionPoint.forInstanceMethodsAndFields(obj.getClass())),
the same chain of calls that occurs in BindingBuilder#toInstance and
InstanceBindingImpl#getDependencies.
USE CASE:
A Google-internal class uses Guice for a number of purposes but in one case
takes a list of widgets (redacted term) as a constructor parameter.
(Probably the better design is to get the list via Multibinder, but that
migration will affect a lot of code, so it won't happen right away.) For
maximum flexibility, the constructor allows widgets to be specified in the
form of keys, providers, or instances.
The class had been instantiating the widgets lazily, which allowed certain
problems to go undetected. When I tried to change it to instantiate the
widgets during injector creation, I ran into dependency problems manifest
via the exception, "This Provider cannot be used until the Injector has
been created." My current idea is to build the widget list via a
Multibinder in a private child binder, which has the side effect of
establishing the correct dependencies. However, this would mean binding
the list for use by a single injection point in the same file, which is
silly; I prefer more direct techniques in such cases. I looked into using
HasDependencies directly, and the proposed new method would make such usage
more convenient. (I am actually planning to stay with the
Multibinder/private child binder solution in anticipation of the migration
described above.)
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.