Top Description Methods
groovy.transform

public @Interface InheritConstructors

extends Annotation
Annotations
@Documented
@Retention:SOURCE
@Target:TYPE
@GroovyASTTransformationClass:InheritConstructorsASTTransformation
Imports
org.codehaus.groovy.transform.GroovyASTTransformationClass, java.lang.annotation.ElementType, .Retention, .RetentionPolicy, .Target

Class annotation to make constructors from a super class available in a sub class. Should be used with care with other annotations which create constructors - see "Known Limitations" for more details.

@InheritConstructors saves you typing some boilerplate code.

Example usage:

class Person {
    String first, last
    Person(String first, String last) {
        this.first = first
        this.last = last.toUpperCase()
    }
}

@groovy.transform.InheritConstructors
class PersonAge extends Person {
    int age
}

def js = new PersonAge('John', 'Smith')
js.age = 25

assert "$js.last, $js.first is $js.age years old" == 'SMITH, John is 25 years old'
for this case, the PersonAge class will be equivalent to the following code:
class PersonAge extends Person {
    PersonAge(String first, String last) {
        super(first, last)
    }
    int age
}
You may add additional constructors in addition to inherited ones. If the argument types of a supplied constructor exactly match those of a parent constructor, then that constructor won't be inherited.

Style note: Don't go overboard using this annotation. Typical Groovy style is to use named-arg constructors when possible. This is easy to do for Groovy objects or any objects following JavaBean conventions. In other cases, inheriting the constructors may be useful. However, sub-classes often introduce new properties and these are often best set in a constructor; especially if that matches the style adopted in parent classes. So, even for the example above, it may have been better style to define an explicit constructor for PersonAge that also set the age property. Sometimes, consistent style is much more important than saving a few keystrokes.

As another example, this:

@InheritConstructors class CustomException extends RuntimeException { }
is equivalent to this:
class CustomException extends RuntimeException {
    CustomException() {
        super()
    }
    CustomException(String message) {
        super(message)
    }
    CustomException(String message, Throwable cause) {
        super(message, cause)
    }
    CustomException(Throwable cause) {
        super(cause)
    }
}
Known Limitations:

More examples:

//--------------------------------------------------------------------------
import groovy.transform.InheritConstructors

@InheritConstructors
class MyException extends Exception {
}

def e = new MyException()
def e1 = new MyException('message')   // Other constructors are available.
assert 'message' == e1.message
//--------------------------------------------------------------------------
import groovy.transform.InheritConstructors

class Person {
    String name

    Person(String name) {
        this.name = name
    }
}

@InheritConstructors
class Child extends Person {}

def child = new Child('Liam')
assert 'Liam' == child.name
Since
1.7.3

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if copied constructor should keep constructor annotations
constructorAnnotations
()

Whether to carry over annotations on the copied constructors.

public boolean

Returns:

true if copied constructor should keep parameter annotations
parameterAnnotations
()

Whether to carry over parameter annotations on the copied constructors.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

constructorAnnotationsback to summary
public boolean constructorAnnotations()

Whether to carry over annotations on the copied constructors. Currently Closure annotation members are not supported.

Returns:boolean

true if copied constructor should keep constructor annotations

parameterAnnotationsback to summary
public boolean parameterAnnotations()

Whether to carry over parameter annotations on the copied constructors. Currently Closure annotation members are not supported.

Returns:boolean

true if copied constructor should keep parameter annotations