Over a million developers have joined DZone.

GroovyBeans - Smell the Brew

DZone 's Guide to

GroovyBeans - Smell the Brew

· Java Zone ·
Free Resource

If you've ever worked with Java you have most likely come across the term "Java Bean". The idea was to create a lightweight model based on naming conventions allowing classes to expose their properties to other classes and tools (such as IDE's).

Of course Groovy supports Java Beans but it enhances them (what else would you expect).

First a simple comparison, Java first:

public class Person {
private String first;
private String last;

public String getFirst() { return first; }
public void setFirst(String val) { first = val; }

public String getLast() { return last; }
public void setLast(String val) { last = val; }

Pretty simple, just a class that stores the first and last name of a person. Now in Groovy:

class Person {
String first
String last

Nice, huh? Well Groovy is doing a bit of work in the background to make the code much simpler. The standard Java Beans getter/setter methods are available but they can be bypassed by simply getting/setting the values in a Groovier manner!

p = new Person() 
p.first = 'John'
p.last = 'Doe'

println "First Name: ${p.first}"
println "Last Name: ${p.last}"

Line 2 and 3 are using the underlying "setter" methods to assign a value to the "first" and "last" properties of the bean. In lines 5 and 6 the auto-generated "getter" methods are used to print the values.

Groovy does allow the auto-generated getter/setter methods to be overridden if you so desire. To change how the last name is retrieved, such as reversing it, you would simple override the "getLast" method:

 def getLast() {
return last?.reverse()

So now whenever the "last" property is retrieved it will call our custom written "getLast" method. In case you're wondering what that "?" is for in the previous snippet, well that's another groov-a-licious nugget that helps prevent the funky NullPointerException's. Essentially the "?" checks to see if the object before it is null if so it stops executing the rest of the expression at the point. This allows us to write cleaner more readable code which doesn't have to explicitly check for null values.

Now that you have seen how to override the auto-generated methods that GroovyBeans provides you might wonder, how can I access the actual value of the property even if there is a custom accessor method for it. Well, Groovy has thought of that. Say hello to the "dot-at" operator.

p = new Person(first:'John', last:'Doe')

assert p.last == 'eoD'
assert p.@last == 'Doe'

As the example above demonstrates, by using the dot-at operator you can access the underlying value of a property which bypasses the custom accessor method written earlier.

I hope you enjoyed this lesson in GroovyBeans, enjoy the smell of Groovy in your cup!


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}