groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Failing to change the value of a Java variable from wihtin a Groovy script (potential issue?) [POTENTIAL FIX]
Date Tue, 05 Apr 2016 19:02:29 GMT
On 05.04.2016 20:00, Thierry Hanser wrote:
[...]
> The solution is indeed close to what Jochen suggested. I just think that for the sake
of symmetry all we need to do is to check whether the variable exists in the binding and behave
accordingly. If it exists change its value in the binding, if it does not exist delegate to
the  meta class (or call super.super). This would ensure a consistent (symmetrical) behaviour
between getProperty() and setProperty().
>
> @Override
> public void setProperty(String property, Object newValue)
> {	
>     if ("binding".equals(property)) setBinding((Binding) newValue);
>     else if("metaClass".equals(property)) setMetaClass((MetaClass)newValue);
>     else if(getBinding().hasVariable(property))  getBinding().setVariable(property, newValue);
 // <== if variable exist => set binding value
>     else getMetaClass().setProperty(this, property, newValue); // <== variable does
not exist => delegate to meta class
> }
>
> Note that the origin of the dissymmetry is actually a consequence of the dissymmetry
between the getVariable() and the setVariable() methods in the Binding class. getVariable()
throws an exception when the variable does not exist where as setVariable() does not (it adds
the variable to the linked map). The former exception is used by getProperty() in Script to
trigger the delegation to the meta class but this can be used by setProperty() since no exception
is thrown by setVariable() in Binding.

well.. this means a script "x=1", without any further doing will fail. 
At the beginning the binding is empty, so 
getBinding().hasVariable(property) will return false, and a setX() does 
not exist.

what I suggested was actually to first ask the meta class and then the 
binding.

[...]
> @Override
> public void setProperty(String property, Object newValue)
> {	
>     if ("binding".equals(property)) setBinding((Binding) newValue);
>     else if("metaClass".equals(property)) setMetaClass((MetaClass)newValue);
>     else
>     {
>        getBinding().setVariable(property, newValue);  // <== update binding
>        getMetaClass().setProperty(this, property, newValue); // <== notify meta class
>     }
> }

hmm... what about exceptions when setting the property? The obvious one 
to handle is of course if the property does not exist. But there is also 
the type conversion problem. For example if your property is an int and 
your value is a String. Will this cause an exception being thrown to the 
user level code? If yes, you will not have the old behaviour. Will the 
binding still have the value? And besides actually being able the call 
the setter, let's not forget the method code itself can still throw 
exception... for example NullPointerException. Different level, but same 
question as before.

bye Jochen


Mime
View raw message