2 Replies Latest reply on Apr 26, 2013 11:58 AM by Michael Hirsch

    How to write an errai-style annotation?

    Michael Hirsch Newbie

      I have an idea for a nice errai-style annotation, by which I mean an annotation that generates code and classes for me, eliminating lots of boilerplate code.  Think of, say, @EventHandler, which behind the scenes creates an implementation of the appropriate class, and registers it as a listener to the appropriate object.  Is there any documentation on how to write something like that?  Where would I look to figure it out?


      I have the 2nd edition of "GWT in Action" which has a chapter on creating generators, but that seems to be tied in with GWT.create().  I was hoping to be able to just use it in any class, like @EventHandler, and have it work.  Can some one tell me (or point me to some docs/code) how Errai works it magic and how I can hook into it?





        • 1. Re: How to write an errai-style annotation?
          Jonathan Fuerth Master

          Hi Michael,


          There two main approaches that we use within Errai: for adding functionality to IOC/CDI beans, we use Errai IOC Code Decorators. These decorators generate code that's incorporated into BootstrapperImpl.java, and influences the behaviour of IOC beans. Errai UI Templating and Data Binding are implemented this way, for example.


          For "standalone" features that can work outside of IOC beans, we use plain old GWT Generators and a cleverly hidden call to GWT.create() somewhere in the library. Starting in Errai 3, we've introduced an annotation-driven system for running these GWT code generators in parallel. You don't have to use this, but you are welcome to if you like! Errai features implemented in this way include Marshalling, JAX-RS Client, JPA, and the IOC container itself.


          In all cases, we generate the Java code itself using the errai-codegen module, which includes a reflection API (MetaClass, MetaMethod, MetaType, etc) and a validating code generator (most of the generator's functionality is accessible through the static methods on the Stmt class).


          Unfortunately, code generation is one of the underdocumented parts of Errai, so the best way to get a feel for it is to clone the Errai git repo, import the sources into your IDE, and play with some of the existing code generators. For an example IOC extension, you could start with ObservesExtension, which implements the CDI @Observes behaviour:




          For an example of a non-IOC generator that works in parallel, have a look at JaxrsProxyLoaderGenerator, which is responsible for generating the client-side call stubs for JAX-RS interfaces:




          Hope this helps!



          1 of 1 people found this helpful
          • 2. Re: How to write an errai-style annotation?
            Michael Hirsch Newbie

            Thanks Jonathan,


            I've got the errai code and I'm looking at it.  It may take a while... That's a lot of interesting stuff going on.


            I've found the code for @Observes and @EventHandler.  What I'm thinking of is somewhere between those two, so I think I'll start there.


            Thanks for the quick response.