The Road from Java to Groovy
Greach Lightning Talk
About Me
1st Career
About Me
2nd Career
OCI Grails Team
Legend
Cruisin' to easy migration and interop
Caution for potholes & speed bumps
Stopping for rework or collisions
Legend
Anywhere you see a parrot means this inconsistency with Java will be changing when the new "parrot" grammar changes are available in Groovy 3.0 and maybe as options in 2.5.x
- Generally, migration is very easy
- Often, just renaming the file works
- There are a few roadblocks and detours
Java to Groovy Migration
- Kind of like Java 8 interfaces
- Kind of like classes
- Can have state and behavior
In Groovy we can't create default methods in interfaces, but we have traits!
trait Singer {
int songLength
void sing() {
println "Oh say can you see..."
}
}
class Teacher implements Singer { }
class Student implements Singer {
void sing() {
println "Twinkle twinkle little star..."
}
}
Java 8 Streams, Method Refs & Lambdas
Java 8 introduced streams, method references and lambdas
Groovy doesn't support lambdas and Java method references, but has its own method reference mechanism)
GroovY "Parrot" Grammars to Support Lambda expressions
Detouring Around The Java 8 Gaps
- Java 8 method references are a kind of shorthand for lambdas
- Lambdas implement functional interfaces
- Groovy closures can be coerced into meeting contract of the functional interface
- Wherever you find a Java 8 method reference or lambda, you can replace it with a closure
Mostly Smooth running in Regards
to Migration and Interoperability
- Often can simply rename file to .groovy
- Use closures to work around Java 8 gaps
- AST transformations are at compile time, so support interop with Java
Method Dispatching Differences (JAVA)
Method Dispatching Differences (Groovy)
Groovy Equality
== means compareTo(obj) or equals(obj)
"spacheship" operator means compareTo
In Java, x == y means: Is x same obj ref as y?
Groovy Equality
a.is(b)
a === b
Groovy Dynamic Method Dispatch
Cruising Performance (Groovy)
@CompileStatic bypasses Groovy MOP
Note Groovy method pointers
Missing Pavement
Do / While (Java)
Do / While (Groovy)
Groovy has never supported do/while
To Groovy, do { } looks like do(Closure)
Types and promotion - Java vs Groovy
In Groovy, all types are objects so the variable five is not an int, but an Integer. This means the method(Integer) will be called.
Output in Java:
In Java, promotion has higher precedence than autoboxing
Keyword Collisons
Summary
Migration from Java and interop with Java is almost trivial
There are a few issues arising from the differences between Groovy and Java, but these are easy to remedy
Very few issues are irreconcilable, such as keyword conflicts.
Resources
- What you saw today
- Slides: https://objistics.slides.com/jackfrosch/road-from-java-to-groovy-greach (Full talk also on Github.)
- Code: https://github.com/jfrosch/RoadFromJavaToGroovy
- http://groovy-lang.org/differences.html
- https://github.com/danielsun1106/groovy-parser
-
Connect with me
- LinkedIn: jackfrosch
- Twitter: @jackfrosch
- Email: jackfrosch@gmail.com
Thank you!
Greach - Road from Java to Groovy
By Jack Frosch
Greach - Road from Java to Groovy
- 2,327