6 Replies Latest reply on Mar 22, 2010 12:03 PM by nickarls

    Question on Qualifiers

    wujek
      Hi. According to the specs (section 2.3.2):
      "A qualifier type is a Java annotation defined as @Target({METHOD, FIELD, PARAMETER, TYPE}) and @Retention(Runtime).
      A qualifier type may be declared by specifying the @javax.inject.Qualifier meta-annotation."

      Consider such an injection point:

      @Inject
      @QualifierOne
      @QualifierTwo
      @NonQualifier
      private SomeBean bean;

      where NonQualifier fulfils the above contract, but it was never meant to be used as a qualifier, it comes from a different domain. For example, it could be used for some sort of runtime processing. The point is - it is completely unrelated to CDI.
      Now, I have a bean whose one of the types is SomeBean that specifies QualifierOne and QualifierTwo, but it doesn't know anything about NonQualifier as, again, this one has never been meant for CDI. Will this result in UnsatisfiedResolutionException or similar? If so, what is the correct approach to take to make this work (I know two, dirty workarounds: 1. removing NonQualifier from the injection point, which might not be an option 2. adding NonQualifer to SomeBean, which seams very dirty)?

      Regards,
      wujek
        • 1. Re: Question on Qualifiers
          wujek

          I checked this and NonQualifier is not considered a valid qualifierwhen I don't use the meta annotation @Qualifier. It only works when used in BeforeBeanDiscovery. Is this on purpose? Moreover, when I change the annotation to not comply to the mentioned contract, it still works (for example, Target set to ElementType.FIELD only still works).
          This looks like either this behaviour is not specs-compliant in this regard, or the specs do not fully explain what and when should happen.
          Can someone clarify?


          Wujek

          • 2. Re: Question on Qualifiers
            nickarls

            You are correct on the point that it's the @Qualifier meta-annotation that makes an annotation a qualifier (and no non-qualifier should ever have it, surprise, surprise)


            If I recall correctly, addQualifier in BBD gets an implicit @Qualifier added


            The @Target is not really checked, it's more for the compiler than CDI. If CDI checks a field and finds a qualifier, it doesn't care if the same qualifier can't be placed on a method. Technically it might be a breach of spec but I don't see how anyone could do that much damage (more than limiting oneself intentionally)

            • 3. Re: Question on Qualifiers
              wujek

              Nicklas Karlsson wrote on Mar 20, 2010 22:15:


              You are correct on the point that it's the @Qualifier meta-annotation that makes an annotation a qualifier (and no non-qualifier should ever have it, surprise, surprise)



              If I recall correctly, addQualifier in BBD gets an implicit @Qualifier added

              So this sounds like it is somewhat not what the specs says. It says that a Qualifier may have @Qualifier meta-annotation, and I also couldn't find anything about automatically adding this annotatino in BBD. From the end user point of view I think CDI specs should specifically describe this, now it is very confusing.

              • 4. Re: Question on Qualifiers
                nickarls

                You could file a JIRA for it.

                • 5. Re: Question on Qualifiers
                  wujek

                  Could you tell me where such an issue should be filed? Somewhere under CDI API maybe?

                  • 6. Re: Question on Qualifiers
                    nickarls

                    On closer thought, perhaps a mail to the JSR-299 EG might be a better since it would be good to have feedback from there what can't/should be done...