Java Reference
In-Depth Information
Listing 5.12. Part of the Java stub generated from
Weather.groovy
Any Java class can be treated as though it was Groovy source by implementing the
GroovyObject
interface,asthestubdoeshere.Thefirstfivemethodsinthestubprovide
no-op implementations for all the methods in that interface. The rest of the stub consists of
empty implementations for the remaining methods, which in this case are the getters and
setters and the
toString
method.
The stub generated for the
RunDemo
class is slightly different, in an interesting way. The
Groovy implementation is just a couple lines of scripting code. As noted in the demonstra-
every Groovy script is ultimately converted to a class by the compiler, and the correspond-
ing RunDemo.java stub illustrates this:
public class RunDemo extends groovy.lang.Script {
public RunDemo() {}
public RunDemo(groovy.lang.Binding context) {}
public static void main(java.lang.String... args) { }
public java.lang.Object run() { return null;}
}
The class extends
groovy.lang.Script
, has a default constructor and a constructor
that takes a
groovy.lang.Binding
, a standard Java
main
method, and a
run
meth-
od. All Groovy scripts look like this to the JVM. Running the script is like executing the
main
method, which delegates to the
run
operation here.