Class ConvertCommandHandlerConstructorToStaticMethod

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

public class ConvertCommandHandlerConstructorToStaticMethod extends org.openrewrite.Recipe
Rewrites Axon Framework 4 @CommandHandler constructors into static handle methods, the AF5 idiom for "creational" command handlers.

In AF4, an aggregate's create-command was typically dispatched into a constructor annotated with @CommandHandler. AF5 narrows the @CommandHandler target to METHOD / ANNOTATION_TYPE only, so a literal AF4-style constructor handler fails to compile. The AF5-equivalent shape (see @EntityCreator javadoc, "Immutable entity" example) is a static method on the entity that publishes the creation event; the entity itself is then created by an @EntityCreator factory invoked when the event is sourced.

The transformation applied per matching constructor:

 @CommandHandler                                    @CommandHandler
 public Payment(PreparePaymentCommand cmd,    →    public static void handle(PreparePaymentCommand cmd,
                EventAppender appender) {                                EventAppender appender) {
     appender.append(...);                              appender.append(...);
 }                                                  }
 
Both AF4 (org.axonframework.commandhandling.CommandHandler) and AF5 (org.axonframework.messaging.commandhandling.annotation.CommandHandler) FQNs are matched, so the recipe is order-independent with respect to the package-rename recipes in Axon4ToAxon5Messaging.

For mutable entities whose @EntityCreator can construct the instance without an event payload (e.g. a no-arg constructor or one that only takes @InjectEntityId), the entity exists when the create-command arrives and an instance method would also work; this recipe always emits a static method because that shape is correct for both the mutable and the immutable entity patterns.

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

    • ConvertCommandHandlerConstructorToStaticMethod

      public ConvertCommandHandlerConstructorToStaticMethod()
  • 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