Class Closures
java.lang.Object
net.minecraftforge.gradleutils.shared.Closures
This class contains helper methods for creating closures in Java code.
- API Note:
- Always use
invoke(groovy.lang.Closure, java.lang.Object...)instead ofClosure.call()to avoid issues with classloader exceptions and closure parameter delegations.
-
Method Summary
Modifier and TypeMethodDescriptionCreates a closure backed by the given action.action(org.gradle.api.Action<? super T> action) Creates a closure backed by the given action.static <R> Closure<R> Creates a closure backed by the given callable.static <R> Closure<R> Creates a closure backed by the given callable.Creates a closure backed by the given consumer.Creates a closure backed by the given consumer.empty()Creates an empty closure.Creates an empty closure.static <T,R> Closure <R> Creates a closure backed by the given function.static <T,R> Closure <R> Creates a closure backed by the given function.static <T> @UnknownNullability TInvokes a given closure with no parameters.static <T> @UnknownNullability TInvokes a given closure with the given object as the delegate type and parameter.Creates a closure backed by the given runnable.Creates a closure backed by the given runnable.static <R> Closure<R> Creates a closure backed by the given supplier.static <R> Closure<R> Creates a closure backed by the given supplier.static <T> org.gradle.api.Action<T> Creates an action backed by the given closure.static <R> Closure<R> unaryOperator(Object owner, UnaryOperator<? extends R> function) Creates a closure backed by the given unary operator.static <R> Closure<R> unaryOperator(UnaryOperator<? extends R> function) Creates a closure backed by the given unary operator.
-
Method Details
-
invoke
public static <T> @UnknownNullability T invoke(@DelegatesTo(strategy=1) Closure closure, Object... object) Invokes a given closure with the given object as the delegate type and parameter.
This is used to work around a Groovy DSL implementation detail that involves dynamic objects within buildscripts. By default, Gradle will attempt to locate the dynamic object that is being referenced within a closure and use handlers within the buildscript's class loader to work with it. This is unfortunately very unfriendly to trying to use closures on traditional objects. The solution is to both manually set the closure's delegate, resolve strategy, and temporarily swap out the current thread's context class loader with that of the closure in order to force resolution of the groovy metaclass to the delegate object.
I'm very sorry.
- Type Parameters:
T- The return type- Parameters:
closure- The closure to invokeobject- The parameter(s) to pass into the closure- Returns:
- The result of the closure execution casted to the generic type parameter
- See Also:
-
invoke
Invokes a given closure with no parameters.- Type Parameters:
T- The return type- Parameters:
closure- The closure to invoke- Returns:
- The result of the closure execution casted to the generic type parameter
- See Also:
-
unaryOperator
Creates a closure backed by the given unary operator.- Type Parameters:
R- The return type of the function- Parameters:
function- The function to apply- Returns:
- The closure
- API Note:
- For static methods only.
-
unaryOperator
Creates a closure backed by the given unary operator.- Type Parameters:
R- The return type of the function- Parameters:
owner- The owner of the closurefunction- The function to apply- Returns:
- The closure
- API Note:
- For instance methods only.
-
function
Creates a closure backed by the given function.- Type Parameters:
T- The parameter type of the functionR- The return type of the function- Parameters:
function- The function to apply- Returns:
- The closure
- API Note:
- For static methods only.
-
function
Creates a closure backed by the given function.- Type Parameters:
T- The parameter type of the functionR- The return type of the function- Parameters:
owner- The owner of the closurefunction- The function to apply- Returns:
- The closure
- API Note:
- For instance methods only.
-
supplier
-
supplier
Creates a closure backed by the given supplier.- Type Parameters:
R- The return type of the supplier- Parameters:
owner- The owner of the closuresupplier- The supplier to use- Returns:
- The closure
- API Note:
- For instance methods only.
-
callable
-
callable
Creates a closure backed by the given callable.- Type Parameters:
R- The return type of the callable- Parameters:
owner- The owner of the closurecallable- The callable to use- Returns:
- The closure
- API Note:
- For instance methods only.
-
action
-
action
Creates a closure backed by the given action.- Type Parameters:
T- The type of the action- Parameters:
owner- The owner of the closureaction- The action to execute- Returns:
- The closure
- API Note:
- For instance methods only.
-
toAction
Creates an action backed by the given closure.- Type Parameters:
T- The type of the action- Parameters:
closure- The closure to call- Returns:
- The action
-
consumer
-
consumer
Creates a closure backed by the given consumer.- Type Parameters:
T- The type of the action- Parameters:
owner- The owner of the closureconsumer- The consumer to execute- Returns:
- The closure
- API Note:
- For instance methods only.
-
runnable
-
runnable
-
empty
-
empty
-