Java Reference
In-Depth Information
Getters, setters, and constructors are all generated in the normal manner. The
@EqualsAndHashCode
AST transformation takes care of
equals
and
hashCode
method implementations. The
@ToString
annotation could also have been used, but the
desired
toString
method is barely longer than that, so I just wrote it out.
Speaking of AST transformations, the
@Singleton
annotation is applied to the
Jdb-
cPersonDAO
class when implemented in Groovy. That automatically implements and en-
forces the
singleton
property on the class by making the constructor private, adding a
static
instance
variable, and so on. That class implements the same interface as before.
Here's the beginning of the class:
@Singleton
class JdbcPersonDAO implements PersonDAO {
static Sql sql = Sql.newInstance(
url:'jdbc:h2:db', driver:'org.h2.Driver')
static {
sql.execute 'drop table if exists people'
...
}
...
}
Groovy and Java interfaces
Java tools prefer Java interfaces. Most Java/Groovy integration problems vanish if you use
Java interfaces with Groovy implementations.
There'soneslightsyntaxvariationrequiredbytheswitchfromJavatoGroovy.The
@Pro-
duces
and
@Consumes
annotations take a list of media types that they support. In the
Java implementation this is expressed as an array, using the braces notation:
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
In Groovy, braces indicate closures. Square brackets delimit a list, however, so the Groovy
implementation just replaces the braces with brackets.