Class GroovyClosure

java.lang.Object
space.jasan.support.groovy.closure.GroovyClosure

public class GroovyClosure extends Object
Makes Java API Groovy Closure friendly.
  • Constructor Details

    • GroovyClosure

      public GroovyClosure()
  • Method Details

    • setDelegate

      public static <T> T setDelegate(T potentialClosure, Object delegateWanted)
      Attempts to set delegate of object which might be a Closure.

      This usually happens when the object is functional interface or single abstract method class. Only the first use case is supported at the moment.

      The possibility of delegate being set should be documented with @DelegatesTo annotation (use provided scope to groovy dependency to retain the annotation to Groovy enabled projects).

      Type Parameters:
      T - the type of the object which might be backed by Groovy closure
      Parameters:
      potentialClosure - object which might be backed by Groovy closure
      delegateWanted - delegate to be set if the object is backed by Groovy closure
      Returns:
      always the original potentialClosure parameter which might have the delegate set if possible
    • getPropagatedOwner

      public static Object getPropagatedOwner(Object object)
      Returns closure's owner if the object is closure or the object self otherwise
      Parameters:
      object - maybe a closure
      Returns:
      closure's owner if the object is closure or the object self otherwise
    • cloneWithTopLevelOwner

      public static <T> groovy.lang.Closure<T> cloneWithTopLevelOwner(groovy.lang.Closure<T> closure)
      Clone with top level owner.
      Type Parameters:
      T - the closure type
      Parameters:
      closure - the closure
      Returns:
      the returned closure
    • cloneWithTopLevelOwner

      public static <T> groovy.lang.Closure<T> cloneWithTopLevelOwner(groovy.lang.Closure<T> closure, Object delegate)
      Clone with top level owner.
      Type Parameters:
      T - the closure type
      Parameters:
      closure - the closure
      delegate - the delegate
      Returns:
      the returned closure
    • cloneWithTopLevelOwner

      public static <T> groovy.lang.Closure<T> cloneWithTopLevelOwner(groovy.lang.Closure<T> closure, Object delegate, int strategy)
      Clone with top level owner.
      Type Parameters:
      T - the closure type
      Parameters:
      closure - the closure
      delegate - the delegate
      strategy - the strategy
      Returns:
      the returned closure