6 Replies Latest reply on Sep 28, 2010 2:23 AM by nimo22

    stereotypes solves annotation hell

    nimo22

      CDI allows @Stereotypes which frees us from annotation hell.


      For example:

      @Model is the abbreviation of two annotations (@RequestScoped and @Name). So I have to use only one annotation instead of two.


      Now my question:


      Can I use stereotypes for ALL annotations?


      For example, I want to declare a new Annotation called AnnotationHell which stands for an abbreviation of all these 6 annotations:



      @RequestScoped
      @Transactional(requiresNew=true)
      @Secure
      @Named
      @Getter @Setter // from Lombok-Annotation
      @Stereotype
      @Retention(RUNTIME)
      @Target(TYPE)
      public @interface AnnotationHell{}



      Now, I want to make use of it:



      @AnnotationHell
      public class Bean()
      ..
      }




      Is that legal ?




        • 1. Re: stereotypes solves annotation hell
          marcelkolsteren

          That's not legal. Quote from chapter 12 of the Weld reference documentation:



          A stereotype encapsulates any combination of:

          a default scope, and
          a set of interceptor bindings.
          A stereotype may also specify that:

          all beans with the stereotype have defaulted bean EL names, or that
          all beans with the stereotype are alternatives.

          Your AnnotationHell stereotype is illegal because the @Getter and @Setter annotations are not among the mentioned categories of annotations.

          • 2. Re: stereotypes solves annotation hell
            nimo22

            Yes, it is illegal due to the specification.


            But it would be nice, if the CDI-Specification supported something like this!


            Dont you think so?


            • 3. Re: stereotypes solves annotation hell
              marcelkolsteren

              Yea, would be nice, but it would be far beyond the scope of a contexts and dependency injection framework. Let alone that implementing such a feature that works for all annotations is challenging, and maybe even impossible without byte code manipulation.

              • 4. Re: stereotypes solves annotation hell
                nimo22

                it would be far beyond the scope of a contexts and dependency injection framework.

                far beyond the scope? I think, aggregating objects (and in this case annotations) to a CONTEXT and use this CONTEXT is not far beyond that scope. I would have a reusable annotation which can be injected into a context.




                Let alone that implementing such a feature that works for all annotations is challenging

                yes, indeed, that is true. I think, the (actual) specification of annotations in java is not prepared for that. However, in this case, the specification of annotations is really far beyond the scope of a contexts and dependency injection framework.


                • 5. Re: stereotypes solves annotation hell
                  marcelkolsteren

                  I don't get your point about aggregating annotations to a context, and having annotations injected into contexts.


                  The stereotypes are just a way of making it easier to configure multiple similar beans in one place. It's not strange that it only supports annotations that are under control of CDI. How could a CDI implementation ever ensure that the behavior that is linked to an arbitrary annotation, would actually be transferred to all places where the stereotype is used?

                  • 6. Re: stereotypes solves annotation hell
                    nimo22

                    It's not strange that it only supports annotations that are under control of CDI.

                    Stereotypes show us how annotation hell can be solved: by aggregating annotations together to a new annotation. However, you are right with that:



                    How could a CDI implementation ever ensure that the behavior that is linked to an arbitrary annotation, would actually be transferred to all places where the stereotype is used?


                    It is not the problem of CDI and its stereotypes, maybe something is missing in the Specification of Annotation JSR 175..something, which would enable us a syntactically grouping of annotations to avoid annotation hell and secure DRY. But that is far beyond CDI.