bineanzhou / google-guice

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

Annotations with Attributes using enums not working #126

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Created my own BindingAnnotation that takes an enum value and injection or 
lookup is failing even though you can see the entries in the binder. As 
far as I can see it is because  (InjectorImpl: 459)

Key<?> key = Key.get(field.getGenericType(), field, field.getAnnotations
(), errorHandler);

the Annotation[] returned by field.getAnnotations() contains Proxy object 
and not actual Annotation instances. These proxy objects don't compare 
equal with the original concrete annotation instances used as part of the 
binder maps Key$SimpleKey and so the Map lookup is returning null at 
(InjectorImpl:408)

  public <T> BindingImpl<T> getBinding(Key<T> key) {
    return (BindingImpl<T>) bindings.get(key);
  }

Original issue reported on code.google.com by luke.stu...@gmail.com on 3 Jul 2007 at 6:54

GoogleCodeExporter commented 9 years ago
Thanks Luke.  Can you show us what your Module code looks like?

Original comment by kevin...@gmail.com on 3 Jul 2007 at 8:07

GoogleCodeExporter commented 9 years ago
Luke, I assume you implemented the annotation interface by hand? Did you follow 
the
rules for equals() and hashCode() in Annotation
(http://java.sun.com/javase/6/docs/api/java/lang/annotation/Annotation.html)? 
If so,
it should compare to the proxy just fine.

Original comment by crazybob...@gmail.com on 3 Jul 2007 at 8:17

GoogleCodeExporter commented 9 years ago
I implemented the interface by cutting and pasting the code out of the Guice 
Users 
Guide :-) I was playing around with the equals and hash code and it doesn't 
seem to 
be being called through the proxy (in debugger). Teh toString is not called 
either, 
you can see this from the Guice error and inspection in the debugger, e.g my 
toString methiod would return "DS(GDS)" where as the Proxy on the same 
annotation 
returns "@com.rbs.gbm.rates.common.guice.DS(value=GDS)". I can't see the source 
for 
sun.reflect.annotation.AnnotationInvocationHandler but it looks like it is not 
passing through methods such as toString, hashCode, equals etc. So it could be 
you 
cannot naively use the bare Annotation as part of your Key ... Happy to help 
further 
but I have to find workaround first to get something working.

Original comment by luke.stu...@gmail.com on 4 Jul 2007 at 8:22

Attachments:

GoogleCodeExporter commented 9 years ago
Apologies - I didn't read the link until after I posted, neither did I 
correctly cut 
and paste your code. Didn't realise the significance of the 127 or the XOR from 
the 
sun spec. Working now. You learn something useless every day! Thanks for your 
time.

Original comment by luke.stu...@gmail.com on 4 Jul 2007 at 8:49

GoogleCodeExporter commented 9 years ago
Our next release will give you a much, much better way to do this that will be
failsafe and easy!

Original comment by kevin...@gmail.com on 4 Jul 2007 at 1:44