Groovy 2.2.1

groovy.transform
[Java] Annotation Type InheritConstructors

java.lang.Object
  groovy.transform.InheritConstructors

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.InheritConstructorsASTTransformation")
public @interface InheritConstructors

Class annotation to make constructors from a super class available in a sub class.

@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()
     }
 }

 @InheritConstructors
 class PersonAge extends Person {
     int age
 }

 def js = new PersonAge('John', 'Smith')
 js.age = 25
 println "$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)
     }
 }
 
Advanced note:If you create Groovy constructors with optional arguments this leads to multiple constructors created in the byte code. The expansion to multiple constructors occurs in a later phase to this AST transformation. This means that you can't override (i.e. not inherit) the constructors with signatures that Groovy adds later. If you get it wrong you will get a compile-time error about the duplication.
Authors:
Paul King
Since:
1.7.3


Method Summary
 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Copyright © 2003-2013 The Codehaus. All rights reserved.