On 20 April 2010 13:23, GuptaJi <[email protected]> wrote: > Hi Fred, > > I tried my best to follow the documentation provided on Annotation > when defining the implementation for my custom annotation. Here is > the class that I created. Hopefully you can spot something I missed. > For testing I have used a String value instead of Enum that I > eventually wish to use (if I ever get this to work). The annotation > has only one field. > > public class RegistrarImpl implements Registrar{ > > public String theValue; > > public RegistrarImpl(String value){ > theValue = value; > } > > public String value(){ > return theValue; > } > > public Class<? extends Annotation> annotationType() { > return Registrar.class; > } > > public boolean equal(Object obj){ > if (obj instanceof Registrar){ > return theValue.equals( > ((Registrar)obj).value()); > } > > return false; > } > > public int hashCode(){ > return value().hashCode(); > } >
^ this hashCode implementation does not match the Annotation spec: http://java.sun.com/javase/6/docs/api/java/lang/annotation/Annotation.html#hashCode%28%29 it should be something like: return ( 127 * "value".hashCode() ) ^ value().hashCode(); for your example with a single value member > } > > > It is quite strpublic class RegistrarImpl implements Registrar{ > > public String theValue; > > public RegistrarImpl(String value){ > theValue = value; > } > > public String value(){ > return theValue; > } > > public Class<? extends Annotation> annotationType() { > return Registrar.class; > } > > public boolean equal(Object obj){ > if (obj instanceof Registrar){ > return theValue.equals( > ((Registrar)obj).value()); > } > > return false; > } > > public int hashCode(){ > return value().hashCode(); > } > > } > > I find it quite strange that passing the class Registrar.class as the > fourth parameter to the newBinder method matches it to the annotation > with the value provided. > @Inject(optional=true) > public void registerAll (@Registrar ("100") Map<String, Object> > aMap) > { > > theMap.putAll(aMap); > } > > It is almost like Guice is ignoring the value provided with the > annotation when creating the MapBinder. > ^ if you pass in an annotation type instead of an annotation instance then this binding will match against any instance of that annotation regardless of the actual value http://code.google.com/p/google-guice/wiki/BindingAnnotations HTH Thanks, > GuptaJi > > On Apr 20, 12:09 am, Fred Faber <[email protected]> wrote: > > GuptaJi, > > > > What does your implementation of your Annotation look like? The javadoc > has > > a unique contract that you might want to browse to be sure your class is > in > > accord w/ it. > > > > -Fred > > > > > > > > > > > > On Tue, Apr 20, 2010 at 12:05 AM, GuptaJi <[email protected]> wrote: > > > Hi Fred, > > > > > I tried what you had suggested but still no luck. I am trying to use > > > the method: > > > MapBinder.newMapBinder(Binder binder, Class<K> keyType, Class<V> > > > valueType, Annotation annotation) ; > > > > > If I pass the AnnotationImpl object as the fourth parameter, the map > > > inject elsewhere contains no value. However, if I modify the > > > Annotation to be without value, it works fine. (I did remove the value > > > method from AnnotationImpl and at the point of injection). > > > > > Any ideas on what I might be doing wrong? > > > > > Thanks, > > > GuptaJi > > > > > On Apr 19, 7:58 pm, Fred Faber <[email protected]> wrote: > > > > Basically you need to be able to reference an instance of your > annotation > > > in > > > > your binding. > > > > > > You can do this by implementing your annotation: > > > > > > class AnnotationNameImpl implements @AnnotationName { > > > > .... > > > > > > } > > > > > > and then you can instantiate an instance in your binding: > > > > > > bind(...).annotatedWith(new AnnotationNameImpl(AnEnum.someValue)); > > > > > > I bet you could also develop a simple service that would allow you do > do: > > > > > > class YourModule { > > > > .... > > > > > > @AnnotationName(AnEnum.someValue) > > > > private static final String annotatedString = null; > > > > > > protected void configure() { > > > > Annotation annotation = > > > > AnnotationReader.readAnnotationFrom(getClass(), > > > "annotatedString"); > > > > bind(....) > > > > } > > > > > > } > > > > > > -Fred > > > > > > On Mon, Apr 19, 2010 at 11:44 AM, GuptaJi <[email protected]> wrote: > > > > > Hi, > > > > > > > I am relatively new to Guice but I have read the documentation > well. > > > > > I am looking for a way to create a map binder in my module with an > > > > > annotation. Creating with an annotation is easy enough by > supplying > > > > > AnnotationName.class as the fourth parameter in newBinder(). My > > > > > requirements need the annotation to be supplied with a value so > that > > > > > it is possible to use something like: > > > > > @AnnotationName(AnEnum.someValue). > > > > > > > Is it even possible to do something like what I am describing? > > > > > > > Thanks a lot for your help. > > > > > > > Cheers, > > > > > GuptaJi > > > > > > > -- > > > > > 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]<google-guice%[email protected]> > <google-guice%2bunsubscr...@googlegroups.com> > > > <google-guice%2bunsubscr...@googlegroups.com> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/google-guice?hl=en. > > > > > > -- > > > > 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]<google-guice%[email protected]> > <google-guice%2bunsubscr...@googlegroups.com> > > > . > > > > For more options, visit this group athttp:// > > > groups.google.com/group/google-guice?hl=en.- Hide quoted text - > > > > > > - Show quoted text - > > > > > -- > > > 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]<google-guice%[email protected]> > <google-guice%2bunsubscr...@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/google-guice?hl=en. > > > > -- > > 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]<google-guice%[email protected]> > . > > For more options, visit this group athttp:// > groups.google.com/group/google-guice?hl=en.- Hide quoted text - > > > > - Show quoted text - > > -- > 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]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- Cheers, Stuart -- 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.
