true) class Person { String name int age } assert new Person('Guillaume', 39).toString() == 'Person(name:Guillaume, age:39)' includeNames from @ToString
annotation collection will always be inserted. PREFER_COLLECTOR Annotations from the collector will be added and any existing annotations with the same name will be removed. PREFER_EXPLICIT Annotations from the collector will be ignored if any existing annotations with the same name are found. PREFER_EXPLICIT_MERGED Annotations from the collector will be ignored if any existing annotations with the same name are found but any new parameters on the collector annotation will be added to existing annotations. PREFER_COLLECTOR_MERGED Annotations from the collector will be added and any existing annotations with the same name will be removed but any new parameters found within existing annotations will be merged into the added annotation.
first = first?.toLowerCase() }) class Person { String first } def p = new Person('Jack') assert p.first == 'jack' import groovy.transform.TupleConstructor import static groovy.test.GroovyAssert.shouldFail @TupleConstructor(pre = { assert first }) class Person { String first } def p = new Person('Jack') shouldFail { def unknown = new Person() }
= 'sirName') class Person { String firstName String surName } new Person(firstName: "John", surName: "Doe").clone() Error during @AutoClone processing: 'excludes' property 'sirName' does not exist.
class Person { String name } @Immutable @TupleConstructor(includeSuperProperties = true) @EqualsAndHashCode(callSuper = true) @ToString(includeNames = true, includeSuperProperties = true) class Athlete extends Person { String sport }
{ String name Optional<String> address } def p = new Person('Joe', Optional.of('Home')) assert p.toString() == 'Person(Joe, Optional[Home])' assert p.address.get() == 'Home' @Immutable class Person { String name Optional<Date> birth } Fails compilation, as Date is mutable
Writer { } @AutoImplement(exception = UnsupportedOperationException, message = 'Not supported by MyIterator') class MyIterator implements Iterator<String> { } @AutoImplement(code = { throw new UnsupportedOperationException( 'Should never be called but was called on ' + new Date()) }) class EmptyIterator implements Iterator<String> { boolean hasNext() { false } }
@EqualsAndHashCode @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement class Person { String name int age } def jaxbContext = JAXBContext.newInstance(Person) def p = new Person(name: 'Marion', age: 8) def xml = jaxbContext.marshal(p) assert jaxbContext.unmarshal(xml, Person) == p
age = 39 return it }.toString() == 'Person(Guillaume, 39)' assert new Person().tap { name = 'Guillaume' age = 39 }.toString() == 'Person(Guillaume, 39)' @Canonical class Person { String name int age }
String title int age String password Date dob URL favoriteUrl } Person person = new Person(name: 'John', title: null, age: 21, password: 'secret', dob: Date.parse('yyyy-MM-dd', '1984-12-15'), favoriteUrl: new URL('http://groovy-lang.org/')) def generator = new JsonGenerator.Options() .excludeNulls() .dateFormat('yyyy@MM') .excludeFieldsByName('age', 'password') .excludeFieldsByType(URL) .build() assert generator.toJson(person) == '{"dob":"1984@12","name":"John"}'
new File('./NOTICE') def output = new File('/tmp/zipped.zip') // Java-style try ( FileInputStream fin = new FileInputStream(input); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output)) ) { byte[] buffer = new byte[4096] int nread = 0 while ((nread = fin.read(buffer)) != -1) { out.write(buffer, 0, nread) } }
new File('./NOTICE') def output = new File('/tmp/zipped.zip') // Groovy-style :-) try ( fin = new FileInputStream(input) out = new GZIPOutputStream(new FileOutputStream(output)) ) { out << fin }
@ToString class Person { String name int age } def p = new Person(name: 'Érine') p.with { name = name ?: 'unknown' age = age ?: 3 } assert p.toString() == 'Person(Érine, 3)'
@ToString class Person { String name int age } def p = new Person(name: 'Érine') p.with { name = name ?: 'unknown' age = age ?: 3 } assert p.toString() == 'Person(Érine, 3)' p.with { name ?= 'unknown' age ?= 3 }
https://pixabay.com/fr/route-marquage-de-route-rue-miles-166543/ Prague https://pixabay.com/fr/prague-r%C3%A9publique-tch%C3%A8que-ville-1168302/ Wise men http://img03.deviantart.net/4431/i/2012/044/f/6/three_wise_men_color_by_csoro-d4pmlv2.jpg Rocket lift-off https://pixabay.com/fr/fus%C3%A9e-ses-9-lancement-cap-canaveral-1245696/ Java beans https://pixabay.com/fr/grains-de-caf%C3%A9-caf%C3%A9-caf%C3%A9ine-boire-167001/ Ring http://www.zastavki.com/pictures/originals/2013/Movies__037523_.jpg Letter G https://pixabay.com/fr/typographie-geschtaltung-fontes-1069409/ Grammar https://pixabay.com/fr/grammaire-abc-dictionnaire-mots-390029/
army knife https://pixabay.com/fr/arm%C3%A9e-de-terre-lame-compact-coup%C3%A9-15563/ Punk https://pixabay.com/fr/jeune-jeune-fille-jeunesse-punk-175504/ Jason http://static.comicvine.com/uploads/original/10/103184/3150852-2994250-0447705879-jason.jpg Creep http://orig11.deviantart.net/f87a/f/2012/092/1/5/creep_by_holavengoaflotar-d4urypg.jpg Jammed wires https://www.exchangewire.com/wp-content/uploads/2016/09/7670055210_ceb0c9ef9a_b.jpg