You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Jan Lukavský <je...@seznam.cz> on 2021/01/12 09:36:13 UTC

Getting return type of generated Closure at runtime

Hi,

I'm struggling with something I believe should be possible - when a 
Closure is parsed at runtime, it should be possible to retrieve its 
generic return type.

I have the following (in Java):

   @Test
   public void testClosureReturnTypeExtraction() {
     String script = "def a = { 1L }";
     Closure<?> closure = (Closure<?>) compile(script).run();
     Class<?> returnType = retrieveReturnTypeFrom(closure);        // 
how to implement this??
     assertEquals(Long.class, returnType);
   }

What I cannot figure out is how to extract the type information from the 
generated closure. I believe the type information should be there (or at 
least it should be possible to put it there). So far, my best attempt 
was, that I got that the type argument is "V". :-)

Any pointers would be appreciated.

Cheers,

  Jan


Re: Getting return type of generated Closure at runtime

Posted by Jan Lukavský <je...@seznam.cz>.
Hi Jochen,

yes, I agree that the type information _could_ be stored in the generics 
(after all the Closure is a named class, the compiler only does not 
provide the type information there). Can you point me out how to 
traverse the AST? I have tried that using ClassNode, but I didn't 
succeed in getting the return type from there either.

Many thanks,

  Jan

On 1/19/21 7:29 PM, Jochen Theodorou wrote:
> On 19.01.21 09:28, Jan Lukavský wrote:
>> Hi OC,
>>
>> I'm pretty sure Closure has a return type, that is what the type
>> parametr V in Closure<V> stands for. When source is compiled using at
>> least @TypeChecked, the return type (e.g. long as in the example I gave)
>> should be known and (I suppose) accessible during runtime somehow.
>
> There is a return type inferred by the static compiler for a Closure in
> Groovy. But the compiler is of course at compile time, not runtime.
> Though in case of
>
>>>>>>     String script = "def a = { 1L }";
>>>>>>     Closure<?> closure = (Closure<?>) compile(script).run();
>
> you could of course have access to the compiler. Basically you would
> progress the AST and then find the Closure node and get the inferred
> return type from it..
>
> I am not 100% sure, but I think we do not store the type in the generics
> information, otherwise there would be another potential way
>
> bye Jochen
>

Re: Getting return type of generated Closure at runtime

Posted by Jochen Theodorou <bl...@gmx.org>.
On 19.01.21 09:28, Jan Lukavský wrote:
> Hi OC,
>
> I'm pretty sure Closure has a return type, that is what the type
> parametr V in Closure<V> stands for. When source is compiled using at
> least @TypeChecked, the return type (e.g. long as in the example I gave)
> should be known and (I suppose) accessible during runtime somehow.

There is a return type inferred by the static compiler for a Closure in
Groovy. But the compiler is of course at compile time, not runtime.
Though in case of

>>>>>     String script = "def a = { 1L }";
>>>>>     Closure<?> closure = (Closure<?>) compile(script).run();

you could of course have access to the compiler. Basically you would
progress the AST and then find the Closure node and get the inferred
return type from it..

I am not 100% sure, but I think we do not store the type in the generics
information, otherwise there would be another potential way

bye Jochen


Re: Getting return type of generated Closure at runtime

Posted by Jan Lukavský <je...@seznam.cz>.
Hi OC,

I'm pretty sure Closure has a return type, that is what the type 
parametr V in Closure<V> stands for. When source is compiled using at 
least @TypeChecked, the return type (e.g. long as in the example I gave) 
should be known and (I suppose) accessible during runtime somehow.

Jan

On 1/18/21 5:29 PM, ocs@ocs.cz wrote:
> Jan,
>
> as always, I might be missing something of importance, but I believe a 
> Closure does not /have/ a return type at all (or, more precisely, the 
> return type of all Closures is, by definition, Object).
>
> After all, is there a way to define a closure which returns a 
> non-object, say, int? I think not.
>
> All the best,
> OC
>
>> On 18. 1. 2021, at 9:55 AM, Jan Lukavský <je.ik@seznam.cz 
>> <ma...@seznam.cz>> wrote:
>>
>> Hi,
>>
>> resending this to dev@ (originally users@). Would anyone know if what 
>> I'm trying to achieve is possible? I think the compiler has enough 
>> information and (using either @TypeChecked or @CompileStatic) it 
>> should be possible to get the return type of Closure at runtime.
>>
>> Thanks for any comments,
>>
>>  Jan
>>
>> On 1/12/21 10:36 AM, Jan Lukavský wrote:
>>> Hi,
>>>
>>> I'm struggling with something I believe should be possible - when a 
>>> Closure is parsed at runtime, it should be possible to retrieve its 
>>> generic return type.
>>>
>>> I have the following (in Java):
>>>
>>>   @Test
>>>   public void testClosureReturnTypeExtraction() {
>>>     String script = "def a = { 1L }";
>>>     Closure<?> closure = (Closure<?>) compile(script).run();
>>>     Class<?> returnType = retrieveReturnTypeFrom(closure);        // 
>>> how to implement this??
>>>     assertEquals(Long.class, returnType);
>>>   }
>>>
>>> What I cannot figure out is how to extract the type information from 
>>> the generated closure. I believe the type information should be 
>>> there (or at least it should be possible to put it there). So far, 
>>> my best attempt was, that I got that the type argument is "V". :-)
>>>
>>> Any pointers would be appreciated.
>>>
>>> Cheers,
>>>
>>>  Jan
>>>
>

Re: Getting return type of generated Closure at runtime

Posted by "ocs@ocs.cz" <oc...@ocs.cz>.
Jan,

as always, I might be missing something of importance, but I believe a Closure does not have a return type at all (or, more precisely, the return type of all Closures is, by definition, Object).

After all, is there a way to define a closure which returns a non-object, say, int? I think not.

All the best,
OC

> On 18. 1. 2021, at 9:55 AM, Jan Lukavský <je...@seznam.cz> wrote:
> 
> Hi,
> 
> resending this to dev@ (originally users@). Would anyone know if what I'm trying to achieve is possible? I think the compiler has enough information and (using either @TypeChecked or @CompileStatic) it should be possible to get the return type of Closure at runtime.
> 
> Thanks for any comments,
> 
>  Jan
> 
> On 1/12/21 10:36 AM, Jan Lukavský wrote:
>> Hi,
>> 
>> I'm struggling with something I believe should be possible - when a Closure is parsed at runtime, it should be possible to retrieve its generic return type.
>> 
>> I have the following (in Java):
>> 
>>   @Test
>>   public void testClosureReturnTypeExtraction() {
>>     String script = "def a = { 1L }";
>>     Closure<?> closure = (Closure<?>) compile(script).run();
>>     Class<?> returnType = retrieveReturnTypeFrom(closure);        // how to implement this??
>>     assertEquals(Long.class, returnType);
>>   }
>> 
>> What I cannot figure out is how to extract the type information from the generated closure. I believe the type information should be there (or at least it should be possible to put it there). So far, my best attempt was, that I got that the type argument is "V". :-)
>> 
>> Any pointers would be appreciated.
>> 
>> Cheers,
>> 
>>  Jan
>> 


Re: Getting return type of generated Closure at runtime

Posted by Jan Lukavský <je...@seznam.cz>.
Hi,

resending this to dev@ (originally users@). Would anyone know if what 
I'm trying to achieve is possible? I think the compiler has enough 
information and (using either @TypeChecked or @CompileStatic) it should 
be possible to get the return type of Closure at runtime.

Thanks for any comments,

  Jan

On 1/12/21 10:36 AM, Jan Lukavský wrote:
> Hi,
>
> I'm struggling with something I believe should be possible - when a 
> Closure is parsed at runtime, it should be possible to retrieve its 
> generic return type.
>
> I have the following (in Java):
>
>   @Test
>   public void testClosureReturnTypeExtraction() {
>     String script = "def a = { 1L }";
>     Closure<?> closure = (Closure<?>) compile(script).run();
>     Class<?> returnType = retrieveReturnTypeFrom(closure);        // 
> how to implement this??
>     assertEquals(Long.class, returnType);
>   }
>
> What I cannot figure out is how to extract the type information from 
> the generated closure. I believe the type information should be there 
> (or at least it should be possible to put it there). So far, my best 
> attempt was, that I got that the type argument is "V". :-)
>
> Any pointers would be appreciated.
>
> Cheers,
>
>  Jan
>