In this section I am just going to highlight the code, this follows on from the Annotations and AST section, many of them have properties and will leave you to the Groovy documentation.
| @ToString example | // @ToString // will include all fields
@ToString(includeNames = true, excludes = ["email"]) // we can customize the ToString
class Person {
String first
String last
String email
}
Note: @ToString is the same as in Java |
| @EqualsandHashCode example | @EqualsAndHashCode
class Person {
String first
String last
String email
}
Note: @EqualsAndHashCode is the same as in Java |
| @TupleConstructor example | @ToString // used to print out object
@TupleConstructor
class Person {
String first
String last
String email
}
Note: @TupleConstructor creates the constructors including name constructor |
| @Canonical example | @Canonical /* toString, tuple and equals combined into one) */
class Person {
String first
String last
String email
}
Note: @Canonical is @ToString, @EqualsAndHashCode and @TupleConstructor all rolled together in one annotation |
| @Singleton example | @Singleton
class DatabaseConnection {
// Some Code
}
// To get an instance use the below, you cannot use new
DatabaseConnection db = DatabaseConnection.instance
println db
Note: @Singleton is the same as in Java (Singleton Object) |
| @Sortable example | @Canonical // @ToString, @EqualasAnadHashCode and @TupleConstructor
@Sortable( includes = ['last', 'first'] )
class Person {
String first
String last
}
Note: @Sortable generates the comparable part as you do in Java to allow you to sort |
| @Immutable example | @ToString
@Immutable
class Person {
String first // Groovy will make this final (only getters created)
String last // Groovy will make this final (only getters created)
}
Person p = new Person("Paul, "Valle)
println p.toString()
p.first = "Will" // cannot change state, will throw exception (readonly)
Note: @Immutable means that once an instance is created you cannot change its state |
| @TypeChecked example | @TypeChecked
class Person94 {
String firstName
String lastName
// this will compile even through we have used the wrong lastname (should be lastName)
String getFullName(){
"$firstName $lastname" // lastname (should be lastName) will be picked up using @TypeChecked
}
}
Note: @TypeChecked will let the Groovy compiler use compile time checks (use to become static typed langauge) |
| @CompileStatic example | @CompileStatic
class SomeClass {
String foo(){
"foo"
}
String bar(){
"bar"
}
@CompileStatic(TypeCheckingMode.SKIP)
void noReturn(){
}
}
Note: @CompileStatic this will let the Groovy compiler use compile time checks in the style of Java then |
| @Builder example | @Builder
@ToString(includeNames = true)
class Developer {
String firstName
String lastName
String middleInitial
String email
Date hireDate
List langugages
}
Developer paul = Developer
.builder()
.firstName("Paul")
.lastName("Valle")
.middleInitial("F")
.email("paul.valle@example.com")
.hireDate(new Date())
.langugages(["Java","Groovy"])
.build()
Note: @Builder this is the same as the builder pattern in Java |