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.
Let's demonstrate the features that the @InheritConstructors
annotation gives:
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 { }
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)
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 { }
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.