Hi Sam, thanks for the quick reply and clarification.
Yup, I got messed up in a faulty mental stack unwind when trying to
redo some nested object hierarchy instantiation that used assisted
inject at the outer layer. It was fine until some of the inner classes
needed to have another level of creation logic (and user-supplied
creation args) which rules out use of AssistedInject. These inner
classes are not bound explicitly to a provider since as you say, there
is no way to access the user supplied parameters.
My primary driver for this learning curve was to support a Handle/
Body idiom with a reasonably large class hierarchy that is mirrored on
both the Handle and Body side (and the Body side having multiple
alternative implementations which are bound in separate modules using
multibinder). In addition, there is the need for at least some of the
composite nodes in the hierarchy to be non-abstract which is what
creates the need for a provider that has handle creation logic and
that can't use straight AssistedInject. The leaf nodes can all be
handled using straight AssistedInject generated providers and
specified in the configuration.
For the intermediate nodes, I create the factories manually but they
can implement the same AssistedInject factory interface as the leaf
nodes so it works out fine. The factory interface and example of
composite and leaf bindings are shown below.
public interface HandleFactory<H, B> {
H create(B body);
}
{
final TypeLiteral<HandleFactory<SomeCompositeNodeHandle,
SomeCompositeNodeBody>>
providerLiteral = new
TypeLiteral<HandleFactory<SomeCompositeNodeHandle,
SomeCompositeNodeBody>>() {
};
bind(providerLiteral).to(TypeLiteral.get
(SomeCompositeNodeProvider.class));
}
{
final TypeLiteral<HandleFactory<SomeLeafHandle,
SomeLeafBody>> factoryLiteral = new
TypeLiteral<HandleFactory<SomeLeafHandle, SomeLeafBody>>() {
};
bind(factoryLiteral).toProvider(FactoryProvider.newFactory
(factoryLiteral, TypeLiteral.get(SomeLeafHandleImpl.class)));
}
Best regards,
Gabe
On Jul 2, 10:31 am, Sam Berlin <[email protected]> wrote:
> The reason is pretty simple: it doesn't make any sense otherwise.
>
> AssistedInject is for when your code needs to inject something that needs
> some "guice managed" objects and some "user managed" objects. If I have a
> class Foo that looks like:
>
> Foo { @Inject Foo(Manager manager, @Assisted String name) { } }
>
> how is it supposed to be injected? If my method is "@Inject setFoo(Foo foo)
> {}", what would the value for 'name' be? Similarly, if I inject "@Inject
> setFoo(Provider<Foo> pFoo) { pFoo.get(); }", when I call get on the
> Provider, what would the value of 'name' be?
>
> AssistedInject requires a factory for creating Foo:
>
> FooFactory { Foo create(String name); }
>
> So that places that need a Foo inject the FooFactory and call the create
> method, so that 'name' can have a value.
>
> Sam
>
> On Thu, Jul 2, 2009 at 1:25 PM, gabe97330 <[email protected]> wrote:
>
> > Is it an explicit design decision that a Provider cannot be created
> > using Assisted Inject? If so, is it based on technical or policy
> > driven considerations?
>
> > If not, is there an example someone could provide for how to make it
> > work? My various attempts fail with the error message indicating that
> > Guice is not finding an implementation for the assisted constructor
> > parameter(s), i.e. something like the following (edited):
>
> > No implementation for java.lang.Integer annotated with
> > @com.google.inject.assistedinject.Assisted(value=) was bound. while
> > locating java.lang.Integer annotated with
> > @com.google.inject.assistedinject.Assisted(value=)
> > for parameter 0 at ProviderOfA.<init>(at ....configure)
>
> > Thanks,
>
> > Gabe
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" 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?hl=en
-~----------~----~----~----~------~----~------~--~---