Class Closures

java.lang.Object
net.minecraftforge.gradleutils.shared.Closures

public final class Closures extends Object
This class contains helper methods for creating closures in Java code.
API Note:
Always use invoke(groovy.lang.Closure, java.lang.Object...) instead of Closure.call() to avoid issues with classloader exceptions and closure parameter delegations.
  • 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 invoke
      object - The parameter(s) to pass into the closure
      Returns:
      The result of the closure execution casted to the generic type parameter
      See Also:
      • AbstractTask.ClosureTaskAction.doExecute(org.gradle.api.Task)
    • invoke

      public static <T> @UnknownNullability T invoke(Closure closure)
      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

      public static <R> Closure<R> unaryOperator(UnaryOperator<? extends R> function)
      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

      public static <R> Closure<R> unaryOperator(Object owner, UnaryOperator<? extends R> function)
      Creates a closure backed by the given unary operator.
      Type Parameters:
      R - The return type of the function
      Parameters:
      owner - The owner of the closure
      function - The function to apply
      Returns:
      The closure
      API Note:
      For instance methods only.
    • function

      public static <T,R> Closure<R> function(Function<? super T, ? extends R> function)
      Creates a closure backed by the given function.
      Type Parameters:
      T - The parameter type of the function
      R - The return type of the function
      Parameters:
      function - The function to apply
      Returns:
      The closure
      API Note:
      For static methods only.
    • function

      public static <T,R> Closure<R> function(Object owner, Function<? super T, ? extends R> function)
      Creates a closure backed by the given function.
      Type Parameters:
      T - The parameter type of the function
      R - The return type of the function
      Parameters:
      owner - The owner of the closure
      function - The function to apply
      Returns:
      The closure
      API Note:
      For instance methods only.
    • supplier

      public static <R> Closure<R> supplier(Supplier<? extends R> supplier)
      Creates a closure backed by the given supplier.
      Type Parameters:
      R - The return type of the supplier
      Parameters:
      supplier - The supplier to use
      Returns:
      The closure
      API Note:
      For static methods only.
    • supplier

      public static <R> Closure<R> supplier(Object owner, Supplier<? extends R> supplier)
      Creates a closure backed by the given supplier.
      Type Parameters:
      R - The return type of the supplier
      Parameters:
      owner - The owner of the closure
      supplier - The supplier to use
      Returns:
      The closure
      API Note:
      For instance methods only.
    • callable

      public static <R> Closure<R> callable(Callable<? extends R> callable)
      Creates a closure backed by the given callable.
      Type Parameters:
      R - The return type of the callable
      Parameters:
      callable - The callable to use
      Returns:
      The closure
      API Note:
      For static methods only.
    • callable

      public static <R> Closure<R> callable(Object owner, Callable<? extends R> callable)
      Creates a closure backed by the given callable.
      Type Parameters:
      R - The return type of the callable
      Parameters:
      owner - The owner of the closure
      callable - The callable to use
      Returns:
      The closure
      API Note:
      For instance methods only.
    • action

      public static <T> Closure<Void> action(Action<? super T> action)
      Creates a closure backed by the given action.
      Type Parameters:
      T - The type of the action
      Parameters:
      action - The action to execute
      Returns:
      The closure
      API Note:
      For static methods only.
    • action

      public static <T> Closure<Void> action(Object owner, Action<? super T> action)
      Creates a closure backed by the given action.
      Type Parameters:
      T - The type of the action
      Parameters:
      owner - The owner of the closure
      action - The action to execute
      Returns:
      The closure
      API Note:
      For instance methods only.
    • toAction

      public static <T> Action<T> toAction(Closure<?> closure)
      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

      public static <T> Closure<Void> consumer(Consumer<? super T> consumer)
      Creates a closure backed by the given consumer.
      Type Parameters:
      T - The type of the action
      Parameters:
      consumer - The consumer to execute
      Returns:
      The closure
      API Note:
      For static methods only.
    • consumer

      public static <T> Closure<Void> consumer(Object owner, Consumer<? super T> consumer)
      Creates a closure backed by the given consumer.
      Type Parameters:
      T - The type of the action
      Parameters:
      owner - The owner of the closure
      consumer - The consumer to execute
      Returns:
      The closure
      API Note:
      For instance methods only.
    • runnable

      public static Closure<Void> runnable(Runnable runnable)
      Creates a closure backed by the given runnable.
      Parameters:
      runnable - The runnable to execute
      Returns:
      The closure
      API Note:
      For static methods only.
    • runnable

      public static Closure<Void> runnable(Object owner, Runnable runnable)
      Creates a closure backed by the given runnable.
      Parameters:
      owner - The owner of the closure
      runnable - The runnable to execute
      Returns:
      The closure
      API Note:
      For instance methods only.
    • empty

      public static Closure<Void> empty()
      Creates an empty closure.
      Returns:
      The empty closure
      API Note:
      For static methods only.
    • empty

      public static Closure<Void> empty(Object owner)
      Creates an empty closure.
      Parameters:
      owner - The owner of the closure
      Returns:
      The empty closure
      API Note:
      For instance methods only.