Package org.codehaus.groovy.transform
Class AsyncTransformHelper
java.lang.Object
org.codehaus.groovy.transform.AsyncTransformHelper
Shared AST utilities for the
async/await/defer language features.
Centralises AST node construction and higher-level rewrites used by the
parser (AstBuilder) so feature logic does not sprawl across the
visitor.
- Since:
- 6.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic ExpressionbuildAsyncCall(Expression closure) BuildsAsyncSupport.async(closure)— starts immediately, returns Awaitable.static ExpressionbuildAsyncGeneratorCall(Expression closure) BuildsAsyncSupport.asyncGenerator(closure)— starts immediately, returns Iterable.static ExpressionbuildAwaitCall(Expression arg) BuildsAsyncSupport.await(arg)or for multi-arg:AsyncSupport.await(Awaitable.all(arg1, arg2, ...)).static ExpressionbuildCloseIterableCall(Expression source) BuildsAsyncSupport.closeIterable(source).static ExpressionbuildDeferCall(Expression action) BuildsAsyncSupport.defer($__deferScope__, action).static ExpressionbuildToIterableCall(Expression source) BuildsAsyncSupport.toIterable(source).static ExpressionBuildsAsyncSupport.yieldReturn($__asyncGen__, expr).static booleancontainsDefer(Statement stmt) Returnstrueif the statement tree contains adefercall, without descending into nested closures.static booleancontainsYieldReturn(Statement stmt) Returnstrueif the statement tree contains ayield returncall, without descending into nested closures.static ParameterCreates the synthetic generator parameter$__asyncGen__.static ExpressiontransformAsyncClosure(ClosureExpression closure) Applies defer-scope wrapping and generator parameter injection to anasync { ... }closure, then builds the runtime call (asyncorasyncGenerator).static StatementwrapForAwaitLoop(ForStatement forStatement) Rewrites afor awaitloop into a block that materialises the source iterable, runs the loop, and closes the source in a finally block:static StatementwrapWithDeferScope(Statement body) Wraps a statement block with defer scope management:
-
Method Details
-
buildAwaitCall
BuildsAsyncSupport.await(arg)or for multi-arg:AsyncSupport.await(Awaitable.all(arg1, arg2, ...)).Single-arg form casts to
Objectso static/dynamic overload selection targetsAsyncSupport.await(Object), avoiding ambiguity when the argument implements multiple async interfaces (e.g.CompletableFutureimplements bothCompletionStageandFuture). -
buildDeferCall
BuildsAsyncSupport.defer($__deferScope__, action). -
buildYieldReturnCall
BuildsAsyncSupport.yieldReturn($__asyncGen__, expr). -
buildAsyncCall
BuildsAsyncSupport.async(closure)— starts immediately, returns Awaitable. -
buildAsyncGeneratorCall
BuildsAsyncSupport.asyncGenerator(closure)— starts immediately, returns Iterable. -
buildToIterableCall
BuildsAsyncSupport.toIterable(source). -
buildCloseIterableCall
BuildsAsyncSupport.closeIterable(source). -
createGenParam
Creates the synthetic generator parameter$__asyncGen__. -
containsYieldReturn
Returnstrueif the statement tree contains ayield returncall, without descending into nested closures. -
containsDefer
Returnstrueif the statement tree contains adefercall, without descending into nested closures. -
wrapWithDeferScope
Wraps a statement block with defer scope management:var $__deferScope__ = AsyncSupport.createDeferScope() try { original body } finally { AsyncSupport.executeDeferScope($__deferScope__) } -
wrapForAwaitLoop
Rewrites afor awaitloop into a block that materialises the source iterable, runs the loop, and closes the source in a finally block:var $__forAwaitSource_N = AsyncSupport.toIterable(collection) try { for (... in $__forAwaitSource_N) { body } } finally { AsyncSupport.closeIterable($__forAwaitSource_N) }- Parameters:
forStatement- the for statement whose collection is the await source- Returns:
- the rewritten statement block
-
transformAsyncClosure
Applies defer-scope wrapping and generator parameter injection to anasync { ... }closure, then builds the runtime call (asyncorasyncGenerator).- Parameters:
closure- the parsed async closure expression- Returns:
- the desugared runtime call expression
-