Indeed. This is weird:
@Inject
public CountrySpecificBase(@Assisted ISOCountry country) {
this.country = country;
}
Christian, what does this do exactly? I've never seen a combination of
@Inject with a single @Assisted. I assume it would be completely ignored
here since it's not being used in an injected factory?
--
Cédric
On Fri, Jun 21, 2013 at 10:41 AM, Puneet Lakhina
<[email protected]>wrote:
> I am supplying it for B, but Im not supplying it for A unless by supplying
> it for B, I implicitly bind it to every place that expresses a dependency
> on @Assisted ISOCOuntry .
>
>
> On Fri, Jun 21, 2013 at 10:27 AM, Cédric Beust ♔ <[email protected]> wrote:
>
>> It doesn't, you specified it as "@Assisted" so you are supplying it:
>>
>> public B(A baseObject, @Assisted ISOCountry country) {
>>
>> --
>> Cédric
>>
>>
>>
>> On Fri, Jun 21, 2013 at 10:20 AM, Puneet Lakhina <
>> [email protected]> wrote:
>>
>>> But how does Guice find the ISOCountry to inject? There is no binding
>>> for ISOCountry, neither does ISOCountry have a default constructor.
>>>
>>>
>>> On Fri, Jun 21, 2013 at 4:36 AM, Stephan Classen <[email protected]>wrote:
>>>
>>>> You have a constructor which is annotated with @Inject
>>>> Guice finds this constructor (note that at most one constructor may be
>>>> annotated with @Inject).
>>>>
>>>>
>>>>
>>>> On 06/20/2013 07:18 PM, Puneet Lakhina wrote:
>>>>
>>>> Thanks for your response Stephan. I still dont quite understand how
>>>> guice is able to create an instance of A. A does not have a default
>>>> constructor and I havent specified a provider or a binding for ISOCountry.
>>>>
>>>>
>>>> On Thu, Jun 20, 2013 at 2:26 AM, Stephan Classen <[email protected]>wrote:
>>>>
>>>>> Guice does "just in time" binding if it encounters an injection point
>>>>> which requests a type not bound.
>>>>> Guice will look for an Constructor with @Inject annotation or a
>>>>> default constructor in that type. When it encounters a matching
>>>>> constructor
>>>>> it will add "just in time" a binding to the injector.
>>>>>
>>>>> I mostly try to avoid using just in time bindings because I like the
>>>>> explicity of the bindings in my modules.
>>>>>
>>>>>
>>>>>
>>>>> On 06/20/2013 04:31 AM, Puneet Lakhina wrote:
>>>>>
>>>>>> HI,
>>>>>>
>>>>>> I am trying to migrate some existing code to guice and following is a
>>>>>> self contained example of the setup I have:
>>>>>>
>>>>>> public class GuiceMailingListQuestion {
>>>>>>
>>>>>> public static interface CountrySpecificArtifact {
>>>>>>
>>>>>> public abstract ISOCountry getCountry();
>>>>>> }
>>>>>>
>>>>>> public interface CountrySpecificArtifactFactory<T extends
>>>>>> CountrySpecificArtifact> {
>>>>>> public T create(ISOCountry country);
>>>>>> }
>>>>>> public static abstract class Base<T> {
>>>>>> }
>>>>>> public static abstract class CountrySpecificBase<T> extends
>>>>>> Base<T> implements CountrySpecificArtifact {
>>>>>> private ISOCountry country;
>>>>>>
>>>>>> @Inject
>>>>>> public CountrySpecificBase(@Assisted ISOCountry country) {
>>>>>> this.country = country;
>>>>>> }
>>>>>> @Override
>>>>>> public ISOCountry getCountry() {
>>>>>> return country;
>>>>>> }
>>>>>> public String toString() {
>>>>>> return getClass().getSimpleName() + " country=" +
>>>>>> country.getId();
>>>>>> }
>>>>>> }
>>>>>> public static class A extends CountrySpecificBase<String> {
>>>>>> @Inject
>>>>>> public A(@Assisted ISOCountry country) {
>>>>>> super(country);
>>>>>> }
>>>>>> }
>>>>>> public static class B implements CountrySpecificArtifact {
>>>>>> private ISOCountry country;
>>>>>> private A baseObject;
>>>>>> @Inject
>>>>>> public B(A baseObject, @Assisted ISOCountry country) {
>>>>>> this.country = country;
>>>>>> this.baseObject = baseObject;
>>>>>> }
>>>>>> @Override
>>>>>> public ISOCountry getCountry() {
>>>>>> return country;
>>>>>> }
>>>>>> public String toString() {
>>>>>> return "B country=" + country.getId() + " baseObject=" +
>>>>>> baseObject.toString();
>>>>>> }
>>>>>> }
>>>>>> public static void main(String[] args) {
>>>>>> Injector i = Guice.createInjector(new AbstractModule() {
>>>>>> @Override
>>>>>> protected void configure() {
>>>>>> install(new FactoryModuleBuilder().build(
>>>>>> new
>>>>>> TypeLiteral<CountrySpecificArtifactFactory<A>>() {}));
>>>>>> install(new FactoryModuleBuilder().build(
>>>>>> new
>>>>>> TypeLiteral<CountrySpecificArtifactFactory<B>>() {}));
>>>>>> }
>>>>>> });
>>>>>> System.out.println(i.getInstance(Key.get(new
>>>>>> TypeLiteral<CountrySpecificArtifactFactory<B>>(){})).create(NFCountry.US));
>>>>>> System.out.println(i.getInstance(Key.get(new
>>>>>> TypeLiteral<CountrySpecificArtifactFactory<B>>(){})).create(NFCountry.MX));
>>>>>> }
>>>>>> }
>>>>>>
>>>>>>
>>>>>> The part that surprises me about the above example is how does it
>>>>>> work at all? When I try to create a B using the
>>>>>> CountrySpecificArtiFactory<B> , how does guice figure out how to create
>>>>>> an
>>>>>> A to pass to B's constructor. All I have told guice is how to create
>>>>>> CountrySpecificArtifactFactory<A> not how to create A.
>>>>>>
>>>>>> Can you explain why this works at all?
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "google-guice" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To post to this group, send email to [email protected].
>>>>>> Visit this group at http://groups.google.com/group/google-guice.
>>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>>
>>>>>>
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "google-guice" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/google-guice.
>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Regards,
>>>> Puneet
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "google-guice" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at http://groups.google.com/group/google-guice.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "google-guice" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at http://groups.google.com/group/google-guice.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Regards,
>>> Puneet
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "google-guice" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/google-guice.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "google-guice" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/google-guice.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
>
> --
> Regards,
> Puneet
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.