Understanding groovy closures - groovy

I am trying to understand how the resolution of Groovy's closure is being done in below code,
foo {
a=10
b=20
}
def foo(Closure closure) {
def params = [:]
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.delegate = params
closure()
println params.a
println params.b
}
In the implementation of foo, the value is never assigned to params, then how come params get the values for a & b
I have read about Groovy closure but couldn't really get as to how this works???

the following line defines property and method resolve strategy for closure DELEGATE_FIRST. that means to get/set/call any property/method it will go to delegate object first and when there is no such property/method it will go to owner object.
closure.resolveStrategy = Closure.DELEGATE_FIRST
and the next line sets delegate object of the closure to params
closure.delegate = params
and finally your closure just sets two properties. and those properties set on delegate object (the param at this moment)
{
a=10
b=20
}

Related

modify script variable from a Closure in Groovy

I am trying to modify a script variable from inside a closure in a function. The problem can be distilled down to this:
#groovy.transform.Field int myField = 0
incrementField()
assert myField == 1
def incrementField() {
1.times { myField++ }
}
I think the problem has something to do with closure delegates, but I cannot quite wrap my head around the docs.
This behavior is caused by groovy.lang.Script class and the fact that it overrides following methods:
Object getProperty(String property)
void setProperty(String property, Object newValue)
Closure you have shown in the example uses delegate set to a script object and that's why both overridden methods get executed when you try to access or modify field defined in a script.
Now let's see what happens when your example reaches closure
{ myField++ }
Firstly, getProperty("myField") is called to return a value associated with this property. This method is implemented as:
public Object getProperty(String property) {
try {
return binding.getVariable(property);
} catch (MissingPropertyException e) {
return super.getProperty(property);
}
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Script.java#L54
binding object contains only one variable in the beginning - closure's args array. If we take a look at implementation of binding.getVariable(property) method we will see:
public Object getVariable(String name) {
if (variables == null)
throw new MissingPropertyException(name, this.getClass());
Object result = variables.get(name);
if (result == null && !variables.containsKey(name)) {
throw new MissingPropertyException(name, this.getClass());
}
return result;
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Binding.java#L56
In our case MissingPropertyException is being thrown, so Script.getProperty(property) method returns a value of field myField defined in our Groovy script - 0. Then Groovy increments this value by 1 and tries to set this new value to a field myField. In this case Script.setProperty(property, value) is being called:
public void setProperty(String property, Object newValue) {
if ("binding".equals(property))
setBinding((Binding) newValue);
else if("metaClass".equals(property))
setMetaClass((MetaClass)newValue);
else
binding.setVariable(property, newValue);
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Script.java#L62
As you can see it sets this new value using bindings object. If we display binding.variables we will see that now this internal map contains two entries: args -> [:] and myField -> 1. It explains why assertion in your script always fails. Body of the closure you have defined never reaches myField field from the script class.
Workaround
If you are not satisfied with the fact that Script class overrides setProperty(property, value) method you can always override it by hand in your script and use same implementation as GroovyObjectSupport.setProperty(property, value). Simply add below method to your Groovy script:
#Override
void setProperty(String property, Object newValue) {
getMetaClass().setProperty(this, property, newValue)
}
Now closure defined in incrementField will set a new value to a class field instead of to a bindings object. Of course it may cause some weird side effects, you have to be aware of that. I hope it helps.
Found a possible solution, using closure delegate:
#groovy.transform.Field def stuff = [
myField : 0
]
incrementField()
assert stuff.myField == 1
def incrementField() {
def body = { myField++ }
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = stuff
1.times body
}

how can i assign a closure class with a closure property

I have a class something like this
class SomeClass () {
Closure instClos
SomeClass (Closure clos) { instClos = clos} //constructor
def call() {instClos()}
}
what i'd like to be able to do is do implicit Class constructor like this
SomeClass myInst = {println "hello there}
myInst()
but that doesn't work and throws cast exception. you can make this work by writing [] round the closure to call the constructor. but its not pretty
SomeClass myInst = [{println "hello there}] // or myInst = new ({println "hello there}
myInst()
is there a nice way to create the object through assignment and have that closure stored automatically on the created class instance?
feel i'm missing some groovy syntax here that would sort this (PS i'd prefer not having to extend Closure if i can avoid that )
based on the input provided so far i provided an extended script to show the various options. I tried to add an asType closure to Closure and try and call {...} as SomeClass - but if i tried that the asType is never called so groovy must be using another mechanism when you try a coercion
class SomeClass {
Closure instClos
SomeClass (Closure clos) {
println "\tSomeClass constructor: Will constructor called"
instClos = clos
}
def call() {
println "\tSomeClass.call: calling closure "
return (instClos() + "!")
}
SomeClass asType (Closure clos) {
new SomeClass (instClos: clos)
}
}
//this will call the map constructor - needs to be explicitly provided
SomeClass me = [{println "map style construction"; "echo"}]
assert me() == "echo!"
//use new to get class instance with constructor
me = new SomeClass ({println "new SomeClass () construction"; "echo"})
assert me() == "echo!"
//using layered closure approach - doesnt read well though
def someClos = {new SomeClass(it)}
def c = someClos {println "trying layered closure ";"echo"}
assert c() == "echo!"
//extending the Closure class to add a method
ExpandoMetaClass.enableGlobally()
Closure.metaClass.some = {
if (it == SomeClass) {
new SomeClass (delegate)
}
}
//this will call .some() on closure
me = {println "hello will using .some() "; "echo"}.some ( SomeClass)
assert me() == "echo!"
I'm not aware of anyway to auto-coerce a closure. Even though groovy has closure coercion, it works by changing the closure's type, but it's still a closure, and is not layered. Some ideas:
1. Constructor
class SomeClass {
Closure instClos
SomeClass (Closure clos) { instClos = clos} //constructor
def call() {instClos() + "!"}
}
def c = new SomeClass( { "echo" } )
assert c() == "echo!"
2. Map constructor
class SomeClass {
Closure instClos
def call() {instClos() + "!"}
}
SomeClass c = [instClos: { "echo" }]
assert c() == "echo!"
3. Closure metaprogramming
(Needs enableGlobally())
ExpandoMetaClass.enableGlobally()
Closure.metaClass.some = { new SomeClass(delegate) }
def c = { "echo" }.some()
assert c() == "echo!"
4. Another closure layering
class SomeClass {
Closure instClos
SomeClass (Closure clos) { instClos = clos} //constructor
def call() {instClos() + "!"}
}
def some = { new SomeClass(it) }
def c = some { "echo" }
assert c() == "echo!"
5. Override Closure's asType
ExpandoMetaClass.enableGlobally()
def asType = Closure.metaClass.asType
Closure.metaClass.asType = { Class c ->
(c == SomeClass) ? new SomeClass(delegate) : asType(c)
}
def c = { "echo" } as SomeClass
assert c() == "echo!"
If you have enough flexibility in your design you TRULY only need one method, SomeClass.call(), then you could specify it as an interface instead:
interface SomeClass {
def call()
}
Groovy long ago anticipated the case that Java 8 formalizes with the #FunctionalInterface annotation. If you assign a Groovy Closure to variable or formal parameter of interface type, where the interface has only one method (like SomeClass as defined above), the Groovy compiler will coerce the closure into an instance of that interface. So, given the interface declaration above, the following code:
SomeClass myInst = { println "hello there" }
myInst()
prints "hello there".

Groovy : how to pass closure as parameter to execute the methods inside the class

I need to pass a list of methods to execute inside the class with "closure way", see the code bellow
class A{
def m1(){
println "Method1"
}
def m2(){
println "Method1"
}
def commands = { closure->
println "before"
closure.call()
println "after"
}
}
A a = new A()
a.commands{
println "before execute method m1"
m1() //A need to execute m1 method of the class A
println "after execute method m1"
}
When I comment the m1() the output is
before
before execute method m1
after execute method m1
after
otherwise throw the exception MissingMethodException: No signature of method for method m1()
So, it does not recognize the m1() method as method of class A
Depending on what you are really trying to accomplish, you may want something like this...
class A{
def m1(){
println "Method1"
}
def m2(){
println "Method1"
}
def commands(closure) {
def c = closure.clone()
c.delegate = this
println "before"
c()
println "after"
}
}
The delegate gets an opportunity to respond to method calls that are made inside of the closure. Setting the delegate to this will cause the calls to m1() to be dispatched to the instance of A. You may also be interested in setting the resolveStrategy property of the closure. Valid values for resolveStrategy are Closure.DELEGATE_FIRST, Closure.OWNER_FIRST, Closure.DELEGATE_ONLY, Closure.OWNER_ONLY. The owner is the thing that created the closure and cannot be changed. The delegate can be assigned any object. When the closure makes method calls the methods may be handled by the owner or the delegate and the resolveStrategy comes into play in deciding which to use. The names DELEGATE_ONLY, OWNER_ONLY, DELEGATE_FIRST and OWNER_FIRST I think are self explanatory. If you need any more info, let me know.
I hope that helps.

ext and code block's meaning in the gradle file

ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "build#master.org"
}
Above code is the snippet of build.gradle
I understand that call ext method with { } closure parameter.
it's right?
So I think gradle is accessing springVersion and emailNotification.
I'm gonna verify my assumption with below code
def ext(data) {
println data.springVersion
}
ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "build#master.org"
}
but run that code
below Error occured.
groovy.lang.MissingPropertyException: No such property: springVersion for class: Test
do you explain ext and code block specifically?
ext is shorthand for project.ext, and is used to define extra properties for the project object. (It's also possible to define extra properties for many other objects.) When reading an extra property, the ext. is omitted (e.g. println project.springVersion or println springVersion). The same works from within methods. It does not make sense to declare a method named ext.
Here is the explanation for why the sample code in the question produces an error.
In the code:
ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "build#master.org"
}
Does not pass to the function "ext" an object that has the springVersion and emailNotification properties. The curly braces don't mean a POJO but a closure.
This is why the "ext" function complains it can't get to the properties.
The idea with passing such a closure, known as a configuration closure, is that the receiving function will:
Modify the delegate property of the closure to point to an object that the closure properties/methods should act on.
execute the closure()
Thus the closure executes and when it refers to methods / properties these will be executed on the object to be configured.
Thus, the following modification to your code will make it work:
class DataObject {
String springVersion;
String emailNotification;
}
def ext(closure) {
def data = new DataObject() // This is the object to configure.
closure.delegate = data;
// need this resolve strategy for properties or they just get
// created in the closure instead of being delegated to the object
// to be configured. For methods you don't need this as the default
// strategy is usually fine.
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure() // execute the configuration closure
println data.springVersion
}
ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "build#master.org"
}
​
Hope this helps. Groovy closures get some time getting used to...
It's the overriding of get() and set() by ExtraPropertiesExtension that is the key to making the configuration syntax for never before defined properties work.
class DataObject {
HashMap<String, Object> props = new HashMap<String, Object>()
Object get(String name) {
return props.get(name)
}
void set(String name, #Nullable Object value) {
props.put(name, value)
}
}
def myExtInstance = new DataObject()
def myExt = { Closure closure ->
def data = myExtInstance
closure.delegate = data;
// need this resolve strategy for properties or they just get
// created in the closure instead of being delegated to the object
// to be configured. For methods you don't need this as the default
// strategy is usually fine.
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure() // execute the configuration closure
println data.springVersion
}
myExt {
springVersion = "3.1.0.RELEASE"
emailNotification = "build#master.org"
}
println "myExtInstance.springVersion" + myExtInstance.springVersion
See ExtraPropertiesExtension docs

Is there a way to have callable objects in Groovy?

If for example I have a class named A. Can I make an object be callable, just like Python does? For example :
def myObject = new A()
myObject()
and that would call some object method. Can it be done?
In Groovy only closures are callable by default. E.g. Classes are not callable out of the box. If necessary you can dynamically add a call method to a type's ExpandoMetaClass to make all instances of that type callable.
Hint: you can try out all code sample using the GroovyConsole
Closures are callable by default in Groovy:
// A closure
def doSomething = { println 'do something'}
doSomething()
// A closure with arguments
def sum = {x, y -> x + y}
sum(5,3)
sum.call(5,3)
// Currying
def sum5 = sum.curry(5)
sum5(3)
To make all instances of a specific type callable you can dynamically add a call method to its meta class:
MyObject.metaClass.call = { prinlnt 'I was called' }
def myObject = new MyObject()
myObject()
If you rather only make a specific instance callable you can dynamically add a call method to its meta class:
def myObject = new MyObject()
myObject.metaClass.call = { println 'Called up on' }
myObject()

Resources