4 Replies Latest reply on Jul 2, 2012 6:38 AM by manstis

    errai-codegen questions

    manstis

      Hi,

       

      I'm need to generate some classes from annotations in developer\user-provided classes; and have a few questions.

       

      Sorry for the lengthy questions, but I have tried to give some background as to what I am trying to achieve and evidence of what I find.

       

      1) Extension to Errai's IOC container

       

      I was thinking of using a custom IOCExtension to handle the generation. It would identify the annotated classed and generate the code I need.

       

      I have written a IOCExtensionConfigurator and can see it is called; however it appears to be called after (at least one implementation of) BootstrapperImpl is created:-

       

      I assume this means, if I generate classes that contain CDI annotations I've missed the boat for them being included in the BootStrapperImpl and hence they won't work.

       

      Can I control the order in which IOCExtensions are executed?

       

      [INFO] Compiling module org.drools.guvnor.GuvnorNGCore

      [ERROR] SLF4J: Class path contains multiple SLF4J bindings.

      [ERROR] SLF4J: Found binding in [jar:file:/home/manstis/.m2/repository/org/slf4j/slf4j-log4j12/1.6.4/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]

      [ERROR] SLF4J: Found binding in [jar:file:/home/manstis/.m2/repository/org/jboss/weld/servlet/weld-servlet/1.1.6.Final/weld-servlet-1.1.6.Final.jar!/org/slf4j/impl/StaticLoggerBinder.class]

      [ERROR] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

      [INFO]    Scanning for additional dependencies: jar:file:/home/manstis/.m2/repository/org/jboss/errai/errai-ioc/2.1-SNAPSHOT/errai-ioc-2.1-SNAPSHOT.jar!/org/jboss/errai/ioc/client/Container.java

      [INFO]       Computing all possible rebind results for 'org.jboss.errai.ioc.client.Bootstrapper'

      [INFO]          Rebinding org.jboss.errai.ioc.client.Bootstrapper

      [INFO]             Invoking generator org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator

      [INFO]                Generating Extensions Bootstrapper...

      [INFO]                Checking ErraiApp.properties for configured types ...

      [INFO] ---> WorkbenchIOCExtension.configure

      [INFO] ---> WorkbenchIOCExtension.afterInitialisation

      [INFO]          Rebinding org.jboss.errai.ioc.client.Bootstrapper

      [INFO]             Invoking generator org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator

      [INFO]                Generating Extensions Bootstrapper...

      [INFO]          Rebinding org.jboss.errai.ioc.client.Bootstrapper

      [INFO]             Invoking generator org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator

      [INFO]                Generating Extensions Bootstrapper...

      [INFO]          Rebinding org.jboss.errai.ioc.client.Bootstrapper

      [INFO]             Invoking generator org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator

      [INFO]                Generating Extensions Bootstrapper...

      [INFO]          Rebinding org.jboss.errai.ioc.client.Bootstrapper

      [INFO]             Invoking generator org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator

      [INFO]                Generating Extensions Bootstrapper...

      [INFO]          Rebinding org.jboss.errai.ioc.client.Bootstrapper

      [INFO]             Invoking generator org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator

      [INFO]                Generating Extensions Bootstrapper...

       

      2) IOCExtensionConfigurator.configure

       

      I read the JavaDoc and had a look at JSR299IOCExtensionConfigurator. It looked like it gave me the opportunity to register annotations my extension may be interested in.

       

      However in IOCExtensionConfigurator.afterInitialization I still had to use "ScannerSingleton.getOrCreateInstance().getTypesAnnotatedWith( NameToken.class )" to find the classes I was interested in.

       

      @IOCExtension

      public class WorkbenchIOCExtension

          implements

          IOCExtensionConfigurator {

       

          @Override

          public void configure(IOCProcessingContext context,

                                InjectionContext injectionContext,

                                IOCProcessorFactory procFactory) {

              System.out.println( "---> WorkbenchIOCExtension.configure" );

       

              injectionContext.mapElementType( WiringElementType.DependentBean,

                                               NameToken.class );

       

          }

       

          @Override

          public void afterInitialization(IOCProcessingContext context,

                                          InjectionContext injectionContext,

                                          IOCProcessorFactory procFactory) {

              // TODO Auto-generated method stub

              System.out.println( "---> WorkbenchIOCExtension.afterInitialisation" );

       

              final MetaDataScanner scanner = ScannerSingleton.getOrCreateInstance();

              Set<Class< ? >> types = scanner.getTypesAnnotatedWith( NameToken.class );

              for ( Class< ? > type : types ) {

                  System.out.println( "--------> Found @NameToken on " + type.getName() );

              }

       

          }

       

      Is my understanding correct? What should I be configuring in IOCExtensionConfigurator.configure?

       

      Thank-you for your attention.

       

      With kind regards,

       

      Mike

        • 1. Re: errai-codegen questions
          manstis

          I wonder if I am even going about this the right way?!?!

           

          Looking further at how BootStrapperImpl is generated it's from a deferred binding in Container.gwt.xml - thanks for your time explaining Johnathan.

           

          IOCGenerator calls into IOCBootstrapGenerator that (a) writes the BootStrapperImpl to the .errai folder and (b) returns the generated String to IOCGenerator for persisting to the target\.generated folder (I would ask "why 2 places?").

           

          Would I be better off having following errai's example, and have a deferred binding generate my code? Would my Entry Point's codegen occur before Errai's bootstrapping....

           

          Musing of a curious mind... I will no doubt have trialled some more before receiving comments, if any

           

          Thanks,

           

          Mike

          • 2. Re: errai-codegen questions
            jfuerth

            Hi Mike,

             

            This isn't a direct answer to your question, but my leading suggestion is still to generate your CDI-annotated classes using the standard Java annotation processing APIs. This way, the generated code will already exist when Errai's IoC generator runs.

             

            There is even Eclipse m2e support for running annotation processors now, thanks to Fred Bricon of the JBoss Tools team: https://community.jboss.org/en/tools/blog/2012/05/20/annotation-processing-support-in-m2e-or-m2e-apt-100-is-out

             

            -Jonathan

            • 3. Re: errai-codegen questions
              manstis

              Hi Jonathan,

               

              Yeah, today has been a bit of an experiment into different approaches.

               

              Next on my list is vanilla JAP and a suitable maven plugin.

               

              Saving the best until last

               

              Thanks,

               

              Mike

              • 4. Re: errai-codegen questions
                manstis

                JAP looks to be the best fit solution.