You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by chavan77 <ch...@hotmail.com> on 2016/06/10 22:05:43 UTC

TypeChecked and with custom methods/parameters using ExpandoMetaClass

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

So my question is this. How do I inject the dynamic parameters and values into the script class  before I parse/compile my script file? kind of intercept the parse right after the metaclass is created, add my variables and then have it compile the script.


regards




--
View this message in context: http://groovy.329449.n5.nabble.com/TypeChecked-and-with-custom-methods-parameters-using-ExpandoMetaClass-tp5733289.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: TypeChecked and with custom methods/parameters using ExpandoMetaClass

Posted by chavan77 <ch...@hotmail.com>.
It seems like the solution is to use groovy's type checking extensions.

http://docs.groovy-lang.org/next/html/documentation/type-checking-extensions.html#_precompiled_type_checking_extensions

But how do I do this from inside Java? I want to load my script and for
every property that the compiler does not resolve, I should be able to
provide a resolution based on the runtime state the script was compiled in.
Meaning compiling a script that came from team X should allow variables CAR,
BIKE and SCOOTER. Team Y should not have these variables, instead it should
allow variables POT, PAN and CUP. I will know what variables are valid
through string lists in my java code.



--
View this message in context: http://groovy.329449.n5.nabble.com/TypeChecked-and-with-custom-methods-parameters-using-ExpandoMetaClass-tp5733289p5733291.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: TypeChecked and with custom methods/parameters using ExpandoMetaClass

Posted by chavan77 <ch...@hotmail.com>.
The code I used is this. If I remove the comment for TypeChecked, it will
fail the compile step.





--
View this message in context: http://groovy.329449.n5.nabble.com/TypeChecked-and-with-custom-methods-parameters-using-ExpandoMetaClass-tp5733289p5733290.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: TypeChecked and with custom methods/parameters using ExpandoMetaClass

Posted by Jochen Theodorou <bl...@gmx.org>.
On 11.06.2016 21:59, chavan77 wrote:
> I solved it this way. I changed it to cache the type checked script and then
> clone it every time I need it. Question though about your suggestion, can I
> parse the script with typeChecked and then use the binding after when I want
> to run the parsed script? that way I dont need to add the base class. A
> follow up to that is, will these variables I bind be class level variables
> or global?

A variable in a script binding is only in that binding. Other scripts 
wont see it, unless you decide to share the binding.

Can you use the binding in a type checked script? Not really. If you 
want to use the binding, you would have to use type checking extensions, 
that tell the compiler (a) where to find a vanilla variable, that is not 
found otherwise and (b) tell the compiler the type of the binding variable

bye Jochen

Re: TypeChecked and with custom methods/parameters using ExpandoMetaClass

Posted by chavan77 <ch...@hotmail.com>.
I solved it this way. I changed it to cache the type checked script and then
clone it every time I need it. Question though about your suggestion, can I
parse the script with typeChecked and then use the binding after when I want
to run the parsed script? that way I dont need to add the base class. A
follow up to that is, will these variables I bind be class level variables
or global?





--
View this message in context: http://groovy.329449.n5.nabble.com/TypeChecked-and-with-custom-methods-parameters-using-ExpandoMetaClass-tp5733289p5733295.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: TypeChecked and with custom methods/parameters using ExpandoMetaClass

Posted by Jochen Theodorou <bl...@gmx.org>.
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 
check.

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