Uploaded image for project: 'Kogito'
  1. Kogito
  2. KOGITO-2757

Application.java: Refactor Codegen

XMLWordPrintable

    • 8
    • 2020 Week 31-33 (from Jul 27)

      Improve code generation of Application using templates for CDI and Spring.

      Generated Application.java gets injected several useless fields and uses a mix of field injection, explicit initialization and @PostConstruct.
      We should avoid {{@PostConstruct} if possible, and try and use constructor injection.

      Most initialization boilerplate can be moved to other places, where they make more sense:

      @org.springframework.stereotype.Component()
      public class Application implements org.kie.kogito.Application {
      
         // not required here: it is already injected in {{this.config}}
          @org.springframework.beans.factory.annotation.Autowired(required = false)
          java.util.Collection<org.kie.kogito.event.EventPublisher> eventPublishers;
      
         // Incorrect here: we should inject it inside ProcessConfig. 
          @org.springframework.beans.factory.annotation.Value("${kogito.service.url:#{null}}")
          java.util.Optional<java.lang.String> kogitoService;
      
          @org.springframework.beans.factory.annotation.Autowired()
          org.kie.kogito.Config config;
      
          // We are using explicit instantiation while everywhere else we are using injection
          Processes processes = new Processes(this);
          /// also rules, decisions, etc.
      
          public Config config() {
              return config;
          }
      
         // API: we are exposing this through application, while it is in config().process(); 
         // so does it belong to config().process() or Application ?
         // if this is just a convenience method, it can go; if it should belong to this class, then the config().process() reference should go 
          public UnitOfWorkManager unitOfWorkManager() {
              return config().process().unitOfWorkManager();
          }
      
         // avoid PostConstruct if possible, it makes it harder to reason about the initialization of the class
      
          @javax.annotation.PostConstruct()
          public void setup() {
              // besides, all this is doing is re-injecting eventPublishers into unitOfWorkManager().eventManager()
              if (config().process() != null) {
                  if (eventPublishers != null) {
                      eventPublishers.forEach(publisher -> unitOfWorkManager().eventManager().addPublisher(publisher));
                  }
                // and propagating config properties: this can be done somewhere else; e.g.: in ProcessConfig 
                unitOfWorkManager().eventManager().setService(kogitoService.orElse(""));
                // or propagating addons() 
                unitOfWorkManager().eventManager().setAddons(config().addons());
              }
          }
      
          public Processes processes() {
              return processes;
          }
      }
      

            evacchi Edoardo Vacchi (Inactive)
            evacchi Edoardo Vacchi (Inactive)
            Marian Macik Marian Macik
            Marian Macik Marian Macik
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: