xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

binding to a generic and fix for it too... #564

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
short path to fix....

line 275 in TypeLiteral.java (function getFieldType(Field field)
change field.getGenericType() and instead call field.getType() and then 
everything works.  Below is more detail on the bug though...

In InjectionPoint.java, there is the key lookup

key = Annotations.getKey(type.getFieldType(field), field, annotations, errors);

type.getFieldType(field) makes a call to field.getGenericType 

so in a unit test, I have 

    @Inject
    private MockSimpleEditAddGridFactory<EPWFormularyMsg> mockFactory;

If I erase the <EPWFormularyMsg> part, my unit test passes(and the injection is 
correct).  If I put it back, the unit test fails(because injection is 
wrong).....very odd considering generics are supposedly erased at runtime but 
read on....

In my binding(which won't work because field.getGenericType is different then 
the class I am binding here and different then 
field.getType())....(field.getGenericType returns not just the class but the 
generic <EPWFormularyMsg> as well!!! so it can't find this binding from below)
       b.bind(SimpleEditAddGridFactory.class).to(MockSimpleEditAddGridFactory.class);
b.bind(MockSimpleEditAddGridFactory.class).in(Singleton.class);

If field.getGenericType where changed to field.getType(), my stuff would work 
again!!!!

The other option, is I could try to create my own Key by getting the class and 
then getting the field and calling field.getGenericType to pass to the 
Key...that is such a pain....I should just be able to bind the types.

unless there is a way that I have not seen to bind generics somehow???

I have tried creating a class object like this but naturally the compiler 
complains on this one...(and the generics would be erased I think too unlike 
the field which is capturing generics)...

Class<MockSimpleEditAddGridFactory<EPWFormularyMsg>> inst = 
MockSimpleEditAddGridFactory.class;

thanks,
Dean

Original issue reported on code.google.com by dean.hil...@gmail.com on 27 Oct 2010 at 2:42

GoogleCodeExporter commented 9 years ago
hmmmm, I think the fix needs to be more upstream now.  It turns out I have an 
annotation working just fine where the key is with the generic but it still 
seems to wire up the correct implementation via the annotation on the XXX<M> 
generic class so I am not sure the best fix here now.

Original comment by dean.hil...@gmail.com on 27 Oct 2010 at 2:55

GoogleCodeExporter commented 9 years ago
ok, so, that seems to be only injecting since it finds the annotation at the 
delayed binding time(rather than during b.bind method calls) so that is why 
that works as it finds the interface and adds the generic form in the key and 
reads the annotation so that is why annotations works with generics but the 
b.bind method is not working.

so, maybe just change field.getGenericType to field.getType to fix the issue.

Original comment by dean.hil...@gmail.com on 27 Oct 2010 at 3:28

GoogleCodeExporter commented 9 years ago
You need to give more information on the actual problem you're having. Am I 
right in assuming that SimpleEditAddGridFactory is generic like 
MockSimpleEditAddGridFactory? I think what you're missing is the use of 
TypeLiteral:

  bind(new TypeLiteral<SimpleEditAddGriddFactory<EPWFormularyMessage>>(){})
      .to(new TypeLiteral<MockSimpleEditAddGridFactory<EPWFormularyMsg>>(){})
      .in(Scopes.SINGLETON);

TypeLiteral is how binding generic types is done. Note the {} after each 
TypeLiteral, this is necessary. See the FAQ here: 
http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions

Original comment by cgdec...@gmail.com on 27 Oct 2010 at 3:41

GoogleCodeExporter commented 9 years ago
sweet, I came back to post maybe the api should be added to instead...but I 
guess you have it...this rocks!! 
thanks,
Dean

Original comment by dean.hil...@gmail.com on 27 Oct 2010 at 5:21

GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 27 Oct 2010 at 5:28