Inheriting constructors in Groovy classes

In Java, class inheritance doesn't support the inheriting of constructors for a number of good reasons (leaving the details of constructing an object to the programmer is generally a good idea). There are times when automatically inheriting the constructors of a class would be really useful and make the code less verbose. One of these cases is when inheriting from a class that extends Exception, where all the constructors are just calling super. Groovy has a convenient annotation for doing just that, @InheritConstructors.

In this recipe, we will explore how to use this annotation.

How to do it...

Let's demonstrate the features that the @InheritConstructors annotation gives:

  1. Create an Exception class: one of the classes that are used to communicate that something is horribly wrong with the code or the data:
    class BusinessException extends Exception {
    
    }
  2. Try to instantiate the class using one of the default constructors of the Exception class, for instance:
    def be = new BusinessException('missing resource')

    The code fails at runtime with the following error:

    Could not find matching constructor for:
    BusinessException(java.lang.String)
    
  3. Add the groovy.transform.InheritConstructors annotation to the BusinessException and try to instantiate the class again with a String message. This time, the code will execute without errors:
    @groovy.transform.InheritConstructors
    class BusinessException extends Exception {
    
    }

How it works...

The @InheritConstructors annotation removes the boilerplate of writing matching constructors for a superclass. By adding the @InheritConstructors annotation to the class, we can create the BusinessException by using any of the constructors exposed by java.lang.Exception:

import groovy.transform.InheritConstructors

@InheritConstructors
class BusinessException extends Exception { }

assert new BusinessException('hello').message == 'hello'

def b1 = new BusinessException('missing resource')
def b2 = new BusinessException('catastrophic failure', b1)
assert b2.cause.message == 'missing resource'

The annotation is smart enough to detect your existing constructors, if any, and avoid overriding them during the constructor generation phase.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.124