-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2018.2 Groovy examples (Groovy 2.5 and 3.0)
- Loading branch information
Showing
13 changed files
with
260 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
.idea/workspace.xml | ||
spring-tricks/target/ | ||
standard-java/target/ | ||
groovy/out | ||
groovy/build |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
plugins { | ||
id 'groovy' | ||
} | ||
apply plugin: 'groovy' | ||
|
||
group 'com.jetbrains' | ||
version '1.0-SNAPSHOT' | ||
sourceCompatibility = 8 | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile 'org.codehaus.groovy:groovy-all:2.3.11' | ||
testCompile group: 'junit', name: 'junit', version: '4.12' | ||
// compile 'org.codehaus.groovy:groovy-all:2.5.0' | ||
compile 'org.codehaus.groovy:groovy-all:3.0.0-alpha-3' | ||
compile 'javax.xml.bind:jaxb-api:2.3.0' // needed by Groovy | ||
testCompile 'org.junit.jupiter:junit-jupiter-api:5.2.0' | ||
|
||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
interface Greetable { | ||
String target() | ||
|
||
default String salutation() { | ||
'Greetings' | ||
} | ||
|
||
default String greet() { | ||
"${salutation()}, ${target()}" | ||
} | ||
} | ||
|
||
class Friend implements Greetable { | ||
String name | ||
|
||
@Override | ||
String target() { | ||
return name | ||
} | ||
|
||
@Override | ||
String salutation() { | ||
return 'Hi' | ||
} | ||
} | ||
|
||
def friend = new Friend(name: 'Pat') | ||
println friend.greet() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import groovy.transform.ToString | ||
|
||
@ToString | ||
class Element { | ||
String name | ||
int atomicNumber | ||
} | ||
|
||
def he = new Element(name: 'Helium') | ||
he.with { | ||
name = name ?: 'Hydrogen' // existing Elvis operator | ||
atomicNumber ?= 2 // new Elvis assignment shorthand | ||
} | ||
assert he.toString() == 'Element(Helium, 2)' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
|
||
class Creature { String type } | ||
|
||
def houseCat = new Creature(type: 'cat') | ||
def copyHouseCat = houseCat | ||
def lion = new Creature(type: 'cat') | ||
|
||
assert !houseCat.equals(lion) // Java logical equality | ||
assert houseCat != lion // Groovy shorthand operator | ||
|
||
assert houseCat.is(copyHouseCat) // Groovy identity | ||
assert houseCat === copyHouseCat // operator shorthand | ||
assert houseCat !== lion // negated operator shorthand | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import org.codehaus.groovy.ast.ClassNode | ||
import org.codehaus.groovy.ast.expr.ClassExpression | ||
import org.codehaus.groovy.ast.expr.ConstantExpression | ||
import org.codehaus.groovy.macro.transform.MacroClass | ||
|
||
def classNameMacro = macro(true) { getClass().name } | ||
|
||
|
||
def date = new ConstantExpression(new Date().toString()) | ||
def version = new ConstantExpression(GroovySystem.version) | ||
def body = macro(true) { | ||
"""\ | ||
Name: ${getClass().name} | ||
Compiled: ${$v { date }} | ||
Using Groovy version: ${$v { version }}""".stripIndent() | ||
} | ||
|
||
|
||
|
||
ClassNode buildInfoClass(ClassNode reference) { | ||
def date = new ConstantExpression(new Date().toString()) | ||
def vers = new ConstantExpression(GroovySystem.version) | ||
def name = new ClassExpression(reference) | ||
ClassNode infoClass = new MacroClass() { | ||
class DummyName { | ||
java.lang.String getName() { $v{ name }.name } | ||
java.lang.String getVersion() { $v{ vers } } | ||
java.lang.String getCompiled() { $v{ date } } | ||
} | ||
} | ||
infoClass.name = reference.name + 'Info' | ||
return infoClass | ||
} | ||
|
||
//@Info | ||
//class Foo { | ||
// | ||
//} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.util.stream.Stream | ||
|
||
import static java.util.stream.Collectors.toList | ||
|
||
class Groovy3Test extends GroovyTestCase { | ||
void testJavaDoWhile() { | ||
def count = 5 | ||
do { | ||
println "count = $count" | ||
} while (count-- > 0) | ||
} | ||
|
||
void testMethodReferences() { | ||
// class::staticMethod | ||
assert ['1', '2', '3'] == Stream.of(1, 2, 3) | ||
.map(String::valueOf) | ||
.collect(toList()) | ||
|
||
// class::instanceMethod | ||
assert [1, 2, 3] == ['a', 'bb', 'ccc'].stream() | ||
.map(String::length) | ||
.collect(toList()) | ||
} | ||
|
||
void testNotInAndNotInstanceOf() { | ||
assert 45 !instanceof Date | ||
|
||
assert 4 !in [1, 3, 5, 7] | ||
} | ||
|
||
void testSafeIndexing() { | ||
String[] array = null | ||
assert null == array ?[1] // return null for all index values | ||
array ?[1] = 'c' // quietly ignore attempt to set value | ||
assert array == null | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class MacroTest { | ||
|
||
|
||
def body = macro(true) { getClass().name } | ||
|
||
|
||
|
||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import groovy.transform.NamedParam | ||
import groovy.transform.NamedVariant | ||
import org.junit.jupiter.api.Test | ||
|
||
class NamedParamsTest { | ||
|
||
@NamedVariant | ||
String fullName(String lastName, @NamedParam String firstName) { | ||
"$firstName $lastName" | ||
} | ||
|
||
@Test | ||
void testNamedParams() { | ||
println fullName('Smith', firstName: 'Ash') | ||
} | ||
} | ||
|