7 Replies Latest reply on Sep 6, 2006 10:13 PM by gavin.king

    Can you merge multiple annotations in one?

    bfo81

      Many of my beans are annotated like this:

      @Stateful
      @Scope(...)
      @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
      @Conversational(ifNotBegunOutcome="main")
      @Name("...")


      Is it possible to create a new annotation that encapsulates all those annotations? e.g.
      @MyBean(scope=CONVERSATION, name="whatever")

      This would reduce the code lines slightly and there would be one source for mistakes less (e.g. you won't accidently leave out one of the annotations anymore). And you know the golden rule: Don't repeat yourself ;).

      Didn't find something like that, and I'm absolutely unexperienced with creating custom annotations. What do you think?

        • 1. Re: Can you merge multiple annotations in one?
          pmuir

          I would say no (for Seam)

          If you look at Seam it uses annotations like this:

          if (clazz.hasAnnotation(Name.class)) {
          }


          and so merging annotations would mean that the annotations would no longer be recognised. From the bit I've read about annotations I don't think Java supports annotation inheritance.

          • 2. Re: Can you merge multiple annotations in one?

            @Stateful isn't Seam's annotation to begin with, and most of the other annotations are pretty orthogonal to each other. @Role subsumes @Name and @Scope, and maybe they could make it suck up some of the other annotations too. You still need @Name though, or seam won't even pick it up as a component.

            Consolidated annotations aren't really a panacea either -- look at Tapestry for how baroque those can get. Clearly we're screaming for real syntax macros, but I won't be holding my breath. Macro-haters do have one point -- textual source transformations suck when it comes to reasoning about code. There's some macro processors for Java, but I don't think any of them support annotations, and of course most IDEs will fight with them. Personally, I just make use of IDE templates to make the copying and pasting less arduous.

            • 3. Re: Can you merge multiple annotations in one?
              bfo81

              That's a pity. Thanks for your help.

              • 4. Re: Can you merge multiple annotations in one?

                Actually I'm pretty sure Java does have limited meta-annotation support. Look at how Seam implements its DataBinding mechanism.

                @Target(ANNOTATION_TYPE)
                @Retention(RUNTIME)
                @Documented
                public @interface DataBinderClass
                {
                 Class<? extends DataBinder> value();
                }


                Then in whatever your databinder annotation is
                @Target({FIELD, METHOD})
                @Retention(RUNTIME)
                @Documented
                @DataBinderClass(SelectItemsBinder.class)
                public @interface SelectItems
                {...


                Code that looks for @DataBinderClass annotated elements will also find elements annotated with @SelectItems. This is all pretty static, so you won't have the nice macro effect that you're looking for, but could have your own @SeamSLSB annotation that encapsulates some boilerplate code (including imports). I haven't tried this with any annotation other than @DataBinderClass, and it has a @Target(ANNOTATION_TYPE). It's possible that other Seam annotations may also work those with @Target(TYPE), it's possible things may fail. Good luck.

                I hope this is useful.

                -Jim

                • 5. Re: Can you merge multiple annotations in one?

                  EJB 3 interceptors also make use of meta-annotations.

                  http://docs.jboss.com/seam/1.0.0.GA/reference/en/html/concepts.html#d0e2635

                  • 6. Re: Can you merge multiple annotations in one?
                    bfo81

                    Thanks for your hints.

                    But I think I'll give it up (for now). There are too many other problems to handle with at the moment (JSF Chart Creator doesn't render images, AJAX4JSF doesn't change the values of SelectMenus correctly, ...) and I'm short of time.

                    • 7. Re: Can you merge multiple annotations in one?
                      gavin.king

                      FYI, I want to introduce annotation refactoring using meta-annotations in the next rev of EJB3. I will be proposing this formally to Sun quite soon, and I hope to get it into the JSR proposal.

                      So you could do something like this:

                      @Stateless
                      @Transaction(NOT_SUPPORTED)
                      public @interface Action {}