Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>GroovyBeans are JavaBeans but using a much simpler syntax.<br /> Here's an example:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class Customer { // properties Integer id String name Date dob // sample code static void main(args) { def customer = new Customer(id:1, name:"Gromit", dob:new Date()) println("Hello ${customer.name}") } } </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Hello Gromit </pre></td></tr></table> <p>Notice how the properties look just like public fields. You can also set named properties in a bean constructor in Groovy. In Groovy, fields and properties have been merged so that they act and look the same. So, the Groovy code above is equivalent to the following Java code:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import java.util.Date; public class Customer { // properties private Integer id; private String name; private Date dob; public Integer getId() { return this.id; } public String getName() { return this.name; } public Date getDob() { return this.dob; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setDob(Date dob) { this.dob = dob; } // sample code public static void main(String[] args) { Customer customer = new Customer(); customer.setId(1); customer.setName("Gromit"); customer.setDob(new Date()); System.out.println("Hello " + customer.getName()); } } </pre></td></tr></table> <h2>Property and field rules</h2> <p>When Groovy is compiled to bytecode, the following rules are used.</p> <ul class="alternate"> <li>If the name is declared with an access modifier (public, private or protected) then a field is generated.</li> <li>A name declared with no access modifier generates a private field with public getter and setter (i.e. a property).</li> <li>If a property is declared final the private field is created final and no setter is generated.</li> <li>You can declare a property and also declare your own getter or setter.</li> <li>You can declare a property and a field of the same name, the property will use that field then.</li> <li>If you want a private or protected property you have to provide your own getter and setter which must be declared private or protected.</li> <li>If you access a property from within the class the property is defined in at compile time with implicit or explicit this (for example this.foo, or simply foo), Groovy will access the field directly instead of going though the getter and setter.</li> <li>If you access a property that does not exist using the explicit or implicit foo, then Groovy will access the property through the meta class, which may fail at runtime.</li> </ul> <p>So, for example, you could create a read only property or a public read-only property with a protected setter like this:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class Foo { // read only property final String name = "John" // read only property with public getter and protected setter Integer amount protected void setAmount(Integer amount) { this.amount = amount } // dynamically typed property def cheese } </pre></td></tr></table> <p>Note that properties need <em>some</em> kind of identifier: e.g. a variable type ("String") or untyped using the "def" keyword.</p> <p>Why a field with public access modifier do not have getter and setter generated? If we'd generate getter / setter all the time, it means Groovy would not let you not define getters / setters, which can be problematic when you really don't want to geters / setters to be exposed.</p> <h2>Closures and listeners</h2> <p>Though Groovy doesn't support anonymous inner classes, it is possible to define action listeners inline through the means of closures. So instead of writing in Java:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Processor deviceProc = ... deviceProc.addControllerListener(new ControllerListener() { public void controllerUpdate(ControllerEvent ce) { ... } } </pre></td></tr></table> <p>You can do that in Groovy with a closure:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> // Add a closure for a particular method on the listener interface deviceProc.controllerUpdate = { ce -> println "I was just called with event $ce" } </pre></td></tr></table> <p>Notice how the closure is for a <em>method</em> on the listener interface (controllerUpdate), and <em>not for the interface itself</em>(ControllerListener). This technique means that Groovy's listener closures are used like a ListenerAdapter where only one method of interest is overridden. Beware: mistakenly misspelling the method name to override or using the interface name instead can be tricky to catch, because Groovy's parser may see this as a property assignment rather than a closure for an event listener.</p> <p>This mechanism is heavily used in the Swing builder to define event listeners for various components and listeners. The JavaBeans introspector is used to make event listener methods available as properties which can be set with a closure.</p> <p>The Java Beans introspector (java.beans.Introspector) which will look for a BeanInfo for your bean or create one using its own naming conventions. (See the Java Beans spec for details of the naming conventions it uses if you don't provide your own BeanInfo class). We're not performing any naming conventions ourselves - the standard Java Bean introspector does that for us.</p> <p>Basically the BeanInfo is retrieved for a bean and its <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/beans/EventSetDescriptor.html">EventSetDescriptors</a> are exposed as properties (assuming there is no clash with regular beans). It's actually the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/beans/EventSetDescriptor.html#getListenerMethods()">EventSetDescriptor.getListenerMethods()</a> which is exposed as a writable property which can be assigned to a closure.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced