Class GeneratedRecipeSupport

java.lang.Object
org.openrewrite.kotlin.recipe.GeneratedRecipeSupport

public final class GeneratedRecipeSupport extends Object
Runtime support for the recipe authoring DSL's K2 compiler plugin (see RecipeIrGenerationExtension). The plugin generates Recipe subclasses whose getVisitor() body is a single call into one of these factories, keeping the IR codegen small and the matching/replacing logic Java-readable.

methodInvocationRewrite (Kotlin) and methodInvocationRewriteJava are parallel paths selected by the LST-structural classifier in the IR extension: the default is methodInvocationRewriteJava (a JavaVisitor walks both Java AND Kotlin sources via

invalid reference
org.openrewrite.kotlin.TreeVisitorAdapter
); the Kotlin variant kicks in only when the before/after templates structurally reference a K.* LST node — see RecipeIrLanguageDescriptors.isKotlinSpecificTreeNode.
  • Method Details

    • methodInvocationRewrite

      public static org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext> methodInvocationRewrite(String matcherSpecsLines, String afterTemplate, String substitutionSourcesCsv, int strictArgCount)
      Visitor for a rewrite { ... } to { ... } recipe whose before lambda body is a method invocation rooted at the receiver param. Matching is delegated to MethodMatcher; the after template is a KotlinTemplate string with one #{any()} placeholder per substitution slot. matcherSpecsLines is a \n-delimited list of MethodMatcher specs — multi-before recipes (rewrite(b1, b2, ...) to a) yield one spec per before lambda and we accept a method invocation when any spec matches. The substitutionSourcesCsv encodes, in template-left-to-right order, how to fill each placeholder from the matched method invocation: -1 for method.getSelect() (the receiver), N >= 0 for method.getArguments().get(N). The CSV is shared across all matchers because the IR pass requires every before lambda to agree on the captured argument shape (same param-index references and same literal (kind, value) at each position).
    • methodInvocationRewriteKotlinNotNull

      public static org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext> methodInvocationRewriteKotlinNotNull(String matcherSpecsLines, String afterTemplate, String substitutionSourcesCsv)
      Variant of methodInvocationRewrite(java.lang.String, java.lang.String, java.lang.String, int) for recipes whose before lambda body is someCall()!! (a method invocation immediately not-null-asserted). In the rewrite-kotlin LST that pattern parses as K.Unary(NotNull, J.MethodInvocation) — visiting only J.MethodInvocation would replace just the call and leave a stranded !! on the rewritten template, so this visitor walks K.Unary instead and swaps the whole not-null expression for the after template.

      Matcher specs, substitution-source CSV, and trailing-lambda fix-up semantics are identical to the bare variant — only the entry point and the replacement coordinate (the K.Unary, not its inner J.MethodInvocation) change.

    • propertyAccessRewrite

      public static org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext> propertyAccessRewrite(String matcherSpecsLines, String afterTemplate, String substitutionSourcesCsv)
      Visitor for a rewrite { d: Duration -> d.inHours } to { d -> d.inWholeHours } recipe whose before lambda body is a property access (rather than a method invocation). The Kotlin parser models obj.prop as a J.FieldAccess — so this visitor walks J.FieldAccess instead of J.MethodInvocation.

      Matcher specs use the <owner-fqn>#<property-name> format (rather than the MethodMatcher spelling), since property access has no parenthesised parameter list. Each line of matcherSpecsLines is one such spec; the visitor accepts a field access iff its target type FQN and selector name match any spec.

      Property accessors have no value arguments, so the substitution source CSV always references the receiver via -1 — the IR pass still emits the receiver placeholder uniformly with the method-invocation path, which keeps the after-template synthesis logic shared.

    • methodInvocationRewriteJava

      public static org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext> methodInvocationRewriteJava(String matcherSpecsLines, String afterTemplate, String substitutionSourcesCsv, int strictArgCount)
      Java-rooted parallel of methodInvocationRewrite(java.lang.String, java.lang.String, java.lang.String, int). Used when the LST-structural classifier defaults a rewrite { } to { } clause to a JavaVisitor: the visitor walks both Java and Kotlin sources via
      invalid reference
      org.openrewrite.kotlin.TreeVisitorAdapter
      , but the template is parsed via JavaTemplate.

      Matching, substitution-source semantics, and the multi-spec \n delimiter are identical to the Kotlin variant — only the visitor type and the template engine differ. There is no trailing-lambda fix-up: Java has no trailing-lambda syntax, so the OmitParentheses dance is Kotlin-specific.

    • composeSequential

      public static org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext> composeSequential(org.openrewrite.TreeVisitor<?,org.openrewrite.ExecutionContext>[] visitors)
      Compose visitors sequentially: each runs in declaration order on the result of the previous one. Backs multi-statement edit/scan/generate bodies in org.openrewrite.RecipeDsl.

      Semantics (per plan §7):

      • isAcceptable(SourceFile, ctx) returns true iff ANY inner visitor accepts the source. Non-accepting inner visitors are skipped for that source.
      • visit(tree, ctx) threads tree through each visitor in declaration order, restarting cursor state between visitors. Cursor messages set by one inner visitor are NOT visible to the next. Returning the same instance does NOT short-circuit — the next visitor still runs.
      • Exceptions propagate; there's no partial-state recovery.