Package org.openrewrite.gradle.internal
Class GradleParseUtils
java.lang.Object
org.openrewrite.gradle.internal.GradleParseUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic org.openrewrite.java.tree.J.MethodInvocationparseMethodInvocation(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 mappingFunctionfor use in astream.map(...)/optional.map(...)chain over the result of parsing a generated Gradle code snippet, in place ofSomeType.class::cast.
-
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. aproject(...)orversion { }template generated by a recipe) and return that invocation.- Parameters:
ctx- the execution context to parse withinsnippet- the Gradle Groovy snippet; its first statement must be aJ.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 mappingFunctionfor use in astream.map(...)/optional.map(...)chain over the result of parsing a generated Gradle code snippet, in place ofSomeType.class::cast.Recipes parse constant, valid Gradle snippets, so a
ParseErrorhere means the underlying Groovy or Kotlin parser threw while parsing the snippet. Rethrow that real cause — itsParseExceptionResultcarries the full stack trace — instead of letting the cast mask it with an opaqueClassCastException("Cannot cast ParseError to ...").- Parameters:
expected- theSourceFilesubtype the snippet is expected to parse into- Returns:
- a function that casts to
expected, or throws with the underlying parse failure as its cause
-