Class GradleParseUtils

java.lang.Object
org.openrewrite.gradle.internal.GradleParseUtils

public final class GradleParseUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.openrewrite.java.tree.J.MethodInvocation
    parseMethodInvocation(org.openrewrite.ExecutionContext ctx, String snippet)
    Parse a constant, valid Gradle Groovy snippet whose first statement is a method invocation (e.g.
    static <S extends org.openrewrite.SourceFile>
    Function<org.openrewrite.SourceFile,S>
    requireParsed(Class<S> expected)
    A mapping Function for use in a stream.map(...) / optional.map(...) chain over the result of parsing a generated Gradle code snippet, in place of SomeType.class::cast.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • parseMethodInvocation

      public static org.openrewrite.java.tree.J.MethodInvocation parseMethodInvocation(org.openrewrite.ExecutionContext ctx, String snippet)
      Parse a constant, valid Gradle Groovy snippet whose first statement is a method invocation (e.g. a project(...) or version { } template generated by a recipe) and return that invocation.
      Parameters:
      ctx - the execution context to parse within
      snippet - the Gradle Groovy snippet; its first statement must be a J.MethodInvocation
      Returns:
      the first statement parsed as a J.MethodInvocation
      Throws:
      IllegalStateException - if the snippet fails to parse or its first statement is not a method invocation
    • requireParsed

      public static <S extends org.openrewrite.SourceFile> Function<org.openrewrite.SourceFile,S> requireParsed(Class<S> expected)
      A mapping Function for use in a stream.map(...) / optional.map(...) chain over the result of parsing a generated Gradle code snippet, in place of SomeType.class::cast.

      Recipes parse constant, valid Gradle snippets, so a ParseError here means the underlying Groovy or Kotlin parser threw while parsing the snippet. Rethrow that real cause — its ParseExceptionResult carries the full stack trace — instead of letting the cast mask it with an opaque ClassCastException ("Cannot cast ParseError to ...").

      Parameters:
      expected - the SourceFile subtype the snippet is expected to parse into
      Returns:
      a function that casts to expected, or throws with the underlying parse failure as its cause