11 Replies Latest reply on Jan 5, 2010 2:11 PM by nickarls

    Qualifiers with Members

    meetoblivion

      I think I found a bug.


      In Section 4.4 of the docs


      The example shown is


      @Qualifier
      @Retention(RUNTIME)
      @Target({METHOD, FIELD, PARAMETER, TYPE})
      public @interface PayBy {
         PaymentMethod value();
      }



      This actually does not work.  In practice, it appears that the member has to be public.

        • 1. Re: Qualifiers with Members
          nickarls

          Doesn't work == the member isn't taken into account?


          Strange that it doesn't work since I think meta-annotations go through the same isEquivalent that uses reflection for access (at a quick glance)...

          • 2. Re: Qualifiers with Members
            meetoblivion

            Doesn't work in the sense that Weld seems to ignore it as a qualifier.  InjectionPoint.getAnnotated().getAnnotation(RepositorySession.class) returns null.  No errors or anything, just something really bizarre and I noticed that it wasn't public, switched it to public and it worked fine.


            My annotation in question looks like this:




            @Qualifier
            @Retention(RUNTIME)
            @Target({METHOD, FIELD, PARAMETER, TYPE})
            public @interface RepositorySession {
               @Nonbinding String value();
            }





            I changed it to




            @Qualifier
            @Retention(RUNTIME)
            @Target({METHOD, FIELD, PARAMETER, TYPE})
            public @interface RepositorySession {
               @Nonbinding public String value();
            }



            and it worked correctly.

            • 3. Re: Qualifiers with Members
              nickarls

              I tried fiddling with @Random in the numberguess-example.



              • Added a nonbinding String value() and injected with @Random(foo), worked

              • Removed the @Nonbinding, worked



              Can you try your findings in numberguess?

              • 4. Re: Qualifiers with Members
                meetoblivion

                Well that's the trick, I guess.


                You need to give it a try with




                @Random("foo") int random1;
                @Random("bar") int random2;





                I'll number guess a try.

                • 5. Re: Qualifiers with Members
                  nickarls

                  Annotation members can only be public so no qualifier probably equals to public.
                  If you can reproduce it, file a JIRA, in that case it must be a bug in the qualifier comparision algorithm

                  • 6. Re: Qualifiers with Members
                    nickarls

                    I have


                    @Target( { TYPE, METHOD, PARAMETER, FIELD })
                    @Retention(RUNTIME)
                    @Documented
                    @Qualifier
                    public @interface Stuff
                    {
                       String value();
                    }
                    



                    and


                       @Produces @Stuff("good") 
                       public String getGood() {
                          return "good";
                       }
                       
                       @Produces @Stuff("bad") 
                       public String getBad() {
                          return "bad";
                       } 
                    



                    and


                       @Inject @Stuff("good") String good;
                       @Inject @Stuff("bad") String bad;
                    



                    works for me

                    • 7. Re: Qualifiers with Members
                      nickarls

                      Adding @Nonbinding gives ambiguous, OK


                      I can also do


                         @Produces @Stuff 
                         public String getUgly() {
                            return "ugly";
                         }
                      



                      and


                         @Inject @Stuff String ugly;
                      



                      if I default @Stuff value to ugly

                      • 8. Re: Qualifiers with Members
                        meetoblivion

                        Nicklas Karlsson wrote on Jan 05, 2010 12:16:


                        Adding @Nonbinding gives ambiguous, OK



                        I'm still trying to figure out what the purpose is of @Nonbinding.


                        The producer I was using was something like this:




                        @Produces
                        @RepositorySession("")
                        public Session produceRepositorySession(InjectionPoint ip) {
                        RepositorySession rs = ip.getAnnotated().getAnnotation(RepositorySession.class);
                        return someMember.get(rs.value());
                        }



                        I'm more inclined to say the issue's with using @Nonbinding in my annotation.  Is it necessary?  Every annotation example I've seen uses it, but it seems to result in some odd behavior.

                        • 9. Re: Qualifiers with Members
                          nickarls

                          Simple case is qualifier with no members


                          To keep number of qualifiers down, members can be added (as in our examples above)


                          If you don't want to consider some member when resolving (but it might provide info for factories etc, use @Nonbinding)

                          • 10. Re: Qualifiers with Members
                            meetoblivion

                            So then by the spec, both versions of my qualifier should be valid, right? E.g. this looks like a bug.

                            • 11. Re: Qualifiers with Members
                              nickarls

                              Post a reproducible JIRA