Java Reference
In-Depth Information
Google Chart takes the URL generated here and returns a binary image, so converting it to
text isn't very helpful.
Groovy Properties
Accessing properties in Groovy automatically invokes the associated getter or setter meth-
od.
Next I'll build a Swing user interface that includes the image in a javax.swing
.ImageIcon . This will give me a chance to illustrate a builder, which is a great illustra-
tion of Groovy metaprogramming.
2.2.3. Creating a UI with SwingBuilder
In Groovy every class has a metaclass. A metaclass is another class that manages the actual
invocation process. If you invoke a method on a class that doesn't exist, the call is ulti-
mately intercepted by a method in the metaclass called methodMissing . Likewise, ac-
cessing a property that doesn't exist eventually calls propertyMissing in the meta-
class. Customizing the behavior of methodMissing and propertyMissing is the
heart of Groovy runtime metaprogramming.
Groovy metaprogramming is a large subject, but here I'll demonstrate one of its helpful
results: the creation of builder classes. In a builder, the call to methodMissing does
something specific for that type of builder.
Here I'll illustrate a Swing builder. This is a class that intercepts names of components and
constructs a Swing user interface out of the results. This is actually easier to demonstrate
than to explain. I'll start, however, by adding some imports to the Google Chart script I've
been constructing so far: [ 3 ]
3 That's another one of the “Duh! Why didn't we do that all along?” type of revelations that Java developers get all
the time when they first learn Groovy. Why is it we only import java.lang in Java programs? Why not import
lots of typical packages? Wouldn't that make coding easier? Groovy says yes.
import java.awt.BorderLayout as BL
import javax.swing.WindowConstants as WC
 
Search WWH ::




Custom Search