groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <>
Subject Re: TypeChecked and with custom methods/parameters using ExpandoMetaClass
Date Sat, 11 Jun 2016 08:11:41 GMT
On 11.06.2016 00:05, chavan77 wrote:
> Hello
> I am trying to use java to load groovy scripts. My requirements are
>   * Scripts have to be type checked. So I added a compilerConfiguration
>     to use the ASTTransformationCustomizer for TypeChecked. That works
>   * My scripts will have properties it tries to access . These
>     properties are dynamic in nature that I will know about only just
>     before I load the script. For example, I may have a variable CARMAKE
>     that I can use in the script and I know it has to be set to FORD
>     only when I load the script. So to do this, I enabled
>     ExpandoMetaClass.enableGlobally so all my groovy scripts will use
>     the expandometaclass.
>   * I added a base
>   * I then created a groovyshell and parsed my file to get a script object.
>   * I then got the expandometaclass for my script object and added my
>     CARMAKE parameter.
>   * This does not work if I reference the CARMAKE variable in the
>     script, because it is dynamically injected after the compile step
>     and I have turned on type checking.
>   * This works if I have turned off TypeChecked

I think this approach is a bit problematic. When you create the script, 
you do potentially not know about the properties, so you cannot compile 

I see 3 rather flawed solutions to this

1) Create the script every time new (like in your example so far). If 
you do, you could make a transform, that adds the properties to the 
script and not use any meta class anymore. Just need to set the 
properties using InvokerHelper before actual execution. The big flaw: 
you create the script every time new, so no caching will be possible.

2) Forget about type checking... which is against your requirements

3) Use the base class to provide all possible properties - as actual 
getter. The flaws here are that this works only for a limited, well 
known set of properties, and you can use a property, that is not 
supposed to exist in this incarnation.

the approach with a type checking extension would have the same flaws as 
1, so I would rather go with 1, of course if you used the type checking 
extensions you could just use the binding.

  bye Jochen

View raw message