Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

GroovyBeans - Smell the Brew

DZone's Guide to

GroovyBeans - Smell the Brew

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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!

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}