Class MigrateCommandGatewayInEventHandler

java.lang.Object
org.openrewrite.Recipe
org.axonframework.migration.MigrateCommandGatewayInEventHandler
All Implemented Interfaces:
Cloneable

public class MigrateCommandGatewayInEventHandler extends org.openrewrite.Recipe
Migrates an Axon Framework 4 event-handling component that dispatches commands via a class-level CommandGateway field to the AF5 in-handler pattern: a method-parameter CommandDispatcher.

Decision rule from CommandDispatcher's Javadoc — gateway for top-of-chain entry points (controllers, runners), dispatcher inside another message handler. Inside an @EventHandler the active ProcessingContext makes CommandDispatcher the right tool, and AF5's dispatcher is auto-injected into handler methods by the CommandDispatcherParameterResolverFactory. This recipe automates that mechanical part of the migration:

  1. Inside every @EventHandler method that calls <field>.send(...) or <field>.sendAndWait(...) on a class field of type CommandGateway, the call is rewritten to commandDispatcher.send(...) (the AndWait suffix is dropped — AF5's dispatcher only exposes the async send family).
  2. A CommandDispatcher commandDispatcher parameter is appended to the method signature when the method does not already declare one.
  3. For methods whose return type is void and whose body matches one of two supported shapes — a single dispatch expression statement, or a single try/catch where each branch ends in a dispatch expression statement — the return type is widened to CompletableFuture<?>, the dispatch expression is converted to return ... .getResultMessage();, and java.util.concurrent.CompletableFuture is imported. Methods with non-void return types (e.g. Mono<?>, custom types) are left alone — the call rewrite still applies, but the surrounding adaptation is left to the human.
  4. Once every @EventHandler in the class has been visited, the recipe checks whether the CommandGateway field is still referenced anywhere. If it is not, the field declaration, the corresponding constructor parameter, and any matching this.field = field; assignment are removed — and the now-unused CommandGateway import is dropped.
The recipe matches AF5 fully-qualified type names because it is intended to run after the Axon4ToAxon5Messaging package renames in the same composite. Anything that does not match the supported shapes (loops, multi-dispatch branches, conditional dispatch, reactive return types) is left untouched: the build will still surface those call sites as compile errors so the human can address them.
Since:
5.1.1
Author:
Mateusz Nowak
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.openrewrite.Recipe

    org.openrewrite.Recipe.Builder, org.openrewrite.Recipe.DelegatingRecipe
  • Field Summary

    Fields inherited from class org.openrewrite.Recipe

    contributors, examples, PANIC
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
     
     
    org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext>
     

    Methods inherited from class org.openrewrite.Recipe

    addDataTable, builder, buildRecipeList, causesAnotherCycle, clone, createRecipeDescriptor, equals, getContributors, getDataTableDescriptors, getDescriptor, getEstimatedEffortPerOccurrence, getExamples, getInstanceName, getInstanceNameSuffix, getJacksonPolymorphicTypeTag, getMaintainers, getName, getRecipeList, getTags, hashCode, maxCycles, noop, onComplete, run, run, run, setContributors, setExamples, validate, validate, validateAll, validateAll, withOptions

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MigrateCommandGatewayInEventHandler

      public MigrateCommandGatewayInEventHandler()
  • Method Details

    • getDisplayName

      public String getDisplayName()
      Specified by:
      getDisplayName in class org.openrewrite.Recipe
    • getDescription

      public String getDescription()
      Specified by:
      getDescription in class org.openrewrite.Recipe
    • getVisitor

      public org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext> getVisitor()
      Overrides:
      getVisitor in class org.openrewrite.Recipe