Last active
December 20, 2020 07:12
-
-
Save Joxebus/39f4a9fdc58a52196d69e778196767e7 to your computer and use it in GitHub Desktop.
Groovy AST transformations examples to see a full list of examples you can go directly to the Groovy Documentation here: http://groovy-lang.org/metaprogramming.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.ToString | |
@ToString | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person = new Person(name:'Omar', lastname:'Bautista', age:33) | |
assert person.toString() == 'Person(Omar, Bautista, 33)' | |
println person |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.EqualsAndHashCode | |
@EqualsAndHashCode | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person1 = new Person(name:'Omar', lastname:'Bautista', age:33) | |
def person2 = new Person(name:'Omar', lastname:'Bautista', age:33) | |
assert person1 == person2 | |
assert person1.hashCode() == person2.hashCode() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.TupleConstructor | |
@TupleConstructor | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person = new Person('Omar','Bautista', 33) | |
assert person.name == 'Omar' | |
assert person.lastname == 'Bautista' | |
assert person.age == 33 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Canonical | |
@Canonical | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person1 = new Person('Omar', 'Bautista', 33) | |
def person2 = new Person('Omar', 'Bautista', 33) | |
assert person1 == person2 | |
assert person1.hashCode() == person2.hashCode() | |
assert person1.toString() == 'Person(Omar, Bautista, 33)' | |
assert person1.name == 'Omar' | |
assert person1.lastname == 'Bautista' | |
assert person1.age == 33 | |
assert person2.name == 'Omar' | |
assert person2.lastname == 'Bautista' | |
assert person2.age == 33 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.AutoClone | |
@AutoClone | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person1 = new Person(name:'Omar', lastname:'Bautista', age:33) | |
def person2 = person1.clone() | |
// Since no hashCode or equals has be overriden | |
assert person1 != person2 // not equals | |
assert person1.hashCode() != person2.hashCode() // not the same hashCode | |
assert person1.name == 'Omar' | |
assert person1.lastname == 'Bautista' | |
assert person1.age == 33 | |
assert person2.name == 'Omar' | |
assert person2.lastname == 'Bautista' | |
assert person2.age == 33 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.builder.Builder | |
@Builder | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person = new Person().builder() | |
.name('Omar') | |
.lastname('Bautista') | |
.age(33) | |
.build() | |
assert person.name == 'Omar' | |
assert person.lastname == 'Bautista' | |
assert person.age == 33 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.builder.Builder | |
import groovy.transform.builder.ExternalStrategy | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
@Builder(builderStrategy = ExternalStrategy, forClass = Person) | |
class PersonBuilder {} | |
def person = new PersonBuilder() | |
.name('Omar') | |
.lastname('Bautista') | |
.age(33) | |
.build() | |
assert person.name == 'Omar' | |
assert person.lastname == 'Bautista' | |
assert person.age == 33 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.lang.Singleton | |
@Singleton | |
class PersonService { | |
static List people = [] | |
} | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person1 = new Person(name:'Omar', lastname:'Bautista', age:33) | |
def person2 = new Person(name:'Jorge', lastname:'Bautista', age:33) | |
def personService1 = PersonService.instance | |
def personService2 = PersonService.instance // same instance | |
personService1.people << person1 | |
personService1.people << person2 | |
assert personService1 == personService2 | |
assert personService1.hashCode() == personService2.hashCode() | |
assert personService1.people == personService2.people | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Canonical | |
import groovy.transform.Sortable | |
@Canonical | |
@Sortable(includes=['age']) | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
List people = [ | |
new Person("Omar", "Bautista", 33), | |
new Person("Diego", "Ventura", 32), | |
new Person("Arturo", "Vargas", 25), | |
new Person("Maria", "Osornio", 30), | |
new Person("Amaia", "Bautista", 21) | |
] | |
String separator = '-'*80 | |
println people | |
println separator | |
println people.sort() | |
println separator | |
println people.reverse() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.NullCheck | |
class Person { | |
String name | |
String lastname | |
int age | |
@NullCheck | |
new Person(String name, String lastname, int age) { | |
this.name = name | |
this.lastname = lastname | |
this.age = age | |
} | |
} | |
new Person("Omar", null, 33) // throws java.lang.IllegalArgumentException |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.NullCheck | |
class Person { | |
String name | |
String lastname | |
int age | |
@NullCheck | |
void setName(String name) { | |
this.name = name | |
} | |
} | |
def person = new Person() | |
person.name = null // throws java.lang.IllegalArgumentException |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Immutable | |
@Immutable | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person = new Person("Omar", "Lastname", 33) | |
person.name = "Jorge" // throws groovy.lang.ReadOnlyPropertyException |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Immutable | |
@Immutable(copyWith=true) | |
class Person { | |
String name | |
String lastname | |
int age | |
} | |
def person1 = new Person('Omar', 'Bautista', 33) | |
// throws groovy.lang.ReadOnlyPropertyException | |
// person.name = 'Jorge' | |
def person2 = person1.copyWith(name:'Jorge') | |
assert person1.name == 'Omar' | |
assert person2.name == 'Jorge' | |
assert person1.age == person2.age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment