Class DefaultGroovyMethods


  • public class DefaultGroovyMethods
    extends DefaultGroovyMethodsSupport
    This class defines new groovy methods which appear on normal JDK classes inside the Groovy environment. Static methods are used with the first parameter being the destination class, i.e. public static String reverse(String self) provides a reverse() method for String.

    NOTE: While this class contains many 'public' static methods, it is primarily regarded as an internal class (its internal package name suggests this also). We value backwards compatibility of these methods when used within Groovy but value less backwards compatibility at the Java method call level. I.e. future versions of Groovy may remove or move a method call in this file but would normally aim to keep the method available from within Groovy.

    • Field Detail

      • ADDITIONAL_CLASSES

        public static final Class[] ADDITIONAL_CLASSES
      • DGM_LIKE_CLASSES

        public static final Class[] DGM_LIKE_CLASSES
    • Constructor Detail

      • DefaultGroovyMethods

        public DefaultGroovyMethods()
    • Method Detail

      • is

        public static boolean is​(Object self,
                                 Object other)
        Identity check. Since == is overridden in Groovy with the meaning of equality we need some fallback to check for object identity. Invoke using the 'is' method, like so: def same = this.is(that)
        Parameters:
        self - an object
        other - an object to compare identity with
        Returns:
        true if self and other are both references to the same instance, false otherwise
        Since:
        1.0
      • identity

        public static <T,​U> T identity​(U self,
                                             @DelegatesTo(value=Target.class,target="self",strategy=1)
                                             Closure<T> closure)
        Allows the closure to be called for the object reference self. Synonym for 'with()'.
        Parameters:
        self - the object to have a closure act upon
        closure - the closure to call on the object
        Returns:
        result of calling the closure
        Since:
        1.0
        See Also:
        with(Object, Closure)
      • with

        public static <T,​U> T with​(U self,
                                         @DelegatesTo(value=Target.class,target="self",strategy=1)
                                         Closure<T> closure)
        Allows the closure to be called for the object reference self.

        Any method invoked inside the closure will first be invoked on the self reference. For instance, the following method calls to the append() method are invoked on the StringBuilder instance:

         def b = new StringBuilder().with {
           append('foo')
           append('bar')
           return it
         }
         assert b.toString() == 'foobar'
         
        This is commonly used to simplify object creation, such as this example:
         def p = new Person().with {
           firstName = 'John'
           lastName = 'Doe'
           return it
         }
         
        The other typical usage, uses the self object while creating some value:
         def fullName = person.with{ "$firstName $lastName" }
         
        Parameters:
        self - the object to have a closure act upon
        closure - the closure to call on the object
        Returns:
        result of calling the closure
        Since:
        1.5.0
        See Also:
        with(Object, boolean, Closure), tap(Object, Closure)
      • with

        public static <T,​U extends T,​V extends T> T with​(U self,
                                                                     boolean returning,
                                                                     @DelegatesTo(value=Target.class,target="self",strategy=1)
                                                                     Closure<T> closure)
        Allows the closure to be called for the object reference self.

        Any method invoked inside the closure will first be invoked on the self reference. For example, the following method calls to the append() method are invoked on the StringBuilder instance and then, because 'returning' is true, the self instance is returned:

         def b = new StringBuilder().with(true) {
           append('foo')
           append('bar')
         }
         assert b.toString() == 'foobar'
         
        The returning parameter is commonly set to true when using with to simplify object creation, such as this example:
         def p = new Person().with(true) {
           firstName = 'John'
           lastName = 'Doe'
         }
         
        Alternatively, 'tap' is an alias for 'with(true)', so that method can be used instead. The other main use case for with is when returning a value calculated using self as shown here:
         def fullName = person.with(false){ "$firstName $lastName" }
         
        Alternatively, 'with' is an alias for 'with(false)', so the boolean parameter can be ommitted instead.
        Parameters:
        self - the object to have a closure act upon
        returning - if true, return the self object; otherwise, the result of calling the closure
        closure - the closure to call on the object
        Returns:
        the self object or the result of calling the closure depending on 'returning'
        Since:
        2.5.0
        See Also:
        with(Object, Closure), tap(Object, Closure)
      • <