Message-ID: <1437440504.1915.1411193175753.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1914_1985297249.1411193175753" ------=_Part_1914_1985297249.1411193175753 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
GroovyBeans are JavaBeans but using a much simpler syntax.
= Here's an example:
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 pr= operties have been merged so that they act and look the same. So, the Groov= y code above is equivalent to the following Java code:=20 =20
When Groovy is compiled to bytecode, the following rules are used.= =20
So, for example, you could create a read only property or a public read-= only property with a protected setter like this:=20 =20
Note that properties need some kind of identifier: e.g. a varia= ble type ("String") or untyped using the "def" keyword.==20
Why a field with public access modifier do not have getter and setter ge= nerated? If we'd generate getter / setter all the time, it means Groovy wou= ld not let you not define getters / setters, which can be problematic when = you really don't want to geters / setters to be exposed.=20
Though Groovy doesn't support anonymous inner classes, it is possible to= define action listeners inline through the means of closures. So instead o= f writing in Java:=20 =20
You can do that in Groovy with a closure:=20 =20
Notice how the closure is for a method on the listener interfac= e (controllerUpdate), and not for the interface itself(ControllerL= istener). This technique means that Groovy's listener closures are us= ed like a ListenerAdapter where only one method of interest is overridden.&= nbsp; Beware: mistakenly misspelling the method name to override or using t= he interface name instead can be tricky to catch, because Groovy's parser m= ay see this as a property assignment rather than a closure for an event lis= tener.=20
This mechanism is heavily used in the Swing builder to define event list= eners for various components and listeners. The JavaBeans introspector is u= sed to make event listener methods available as properties which can be set= with a closure.=20
The Java Beans introspector (java.beans.Introspector) which will look fo= r 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 y= ou don't provide your own BeanInfo class). We're not performing any naming = conventions ourselves - the standard Java Bean introspector does that for u= s.=20
Basically the BeanInfo is retrieved for a bean and its EventSetDescriptors are exposed as = properties (assuming there is no clash with regular beans). It's actually t= he EventSetDescriptor.getListenerMethods() which is exposed as a writabl= e property which can be assigned to a closure.