You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Schalk Cronjé <ys...@gmail.com> on 2015/05/01 16:48:54 UTC

respondsTo and constructors

I cannot figure out how to check for a constructor using respondsTo. If 
I had the following

    class ABC {
       ABC(String s, File f) {}
    }

How can I use respondsTo to see if the constructor exists?

-- 
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r


Re: respondsTo and constructors

Posted by Schalk Cronjé <ys...@gmail.com>.
Thank you Dinko.

On 06/05/2015 09:25, Dinko Srkoč wrote:
> How about this:
>
>      static IDownload createDownloader(Logger logger) {
>          Download.constructors.findResult { ctor ->
>              ctor.parameterTypes == [Logger, String, String] as Class[] ?
>                  ctor.newInstance(logger, 'GradleTestPlugin','1.0') :
>                  ctor.newInstance('GradleTestPlugin','1.0')
>          }
>      }
>
> Cheers,
> Dinko
>
> On 5 May 2015 at 19:41, Schalk Cronjé <ys...@gmail.com> wrote:
>> I did this for the time being. It works, but I'm not happy about it.
>>
>> https://github.com/ysb33r/gradleTest/blob/master/src/main/groovy/org/ysb33r/gradle/gradletest/internal/Unpacker.groovy#L37
>>
>> Lines 37-43.
>>
>>
>> On 03/05/2015 15:56, Dinko Srkoč wrote:
>>> On 1 May 2015 at 16:48, Schalk Cronjé <ys...@gmail.com> wrote:
>>>> I cannot figure out how to check for a constructor using respondsTo. If I
>>>> had the following
>>>>
>>>> class ABC {
>>>>     ABC(String s, File f) {}
>>>> }
>>>>
>>>> How can I use respondsTo to see if the constructor exists?
>>> It appears that you cannot. There's a
>>> `MetaClassImpl#retrieveConstructor(Class[])` that may help, or one can
>>> always fallback to Java reflection API.
>>>
>>> Cheers,
>>> Dinko
>>>
>>>> --
>>>> Schalk W. Cronjé
>>>> Twitter / Ello / Toeter : @ysb33r
>>
>>
>> --
>> Schalk W. Cronjé
>> Twitter / Ello / Toeter : @ysb33r
>>


-- 
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r


Re: respondsTo and constructors

Posted by Dinko Srkoč <di...@gmail.com>.
How about this:

    static IDownload createDownloader(Logger logger) {
        Download.constructors.findResult { ctor ->
            ctor.parameterTypes == [Logger, String, String] as Class[] ?
                ctor.newInstance(logger, 'GradleTestPlugin','1.0') :
                ctor.newInstance('GradleTestPlugin','1.0')
        }
    }

Cheers,
Dinko

On 5 May 2015 at 19:41, Schalk Cronjé <ys...@gmail.com> wrote:
> I did this for the time being. It works, but I'm not happy about it.
>
> https://github.com/ysb33r/gradleTest/blob/master/src/main/groovy/org/ysb33r/gradle/gradletest/internal/Unpacker.groovy#L37
>
> Lines 37-43.
>
>
> On 03/05/2015 15:56, Dinko Srkoč wrote:
>>
>> On 1 May 2015 at 16:48, Schalk Cronjé <ys...@gmail.com> wrote:
>>>
>>> I cannot figure out how to check for a constructor using respondsTo. If I
>>> had the following
>>>
>>> class ABC {
>>>    ABC(String s, File f) {}
>>> }
>>>
>>> How can I use respondsTo to see if the constructor exists?
>>
>> It appears that you cannot. There's a
>> `MetaClassImpl#retrieveConstructor(Class[])` that may help, or one can
>> always fallback to Java reflection API.
>>
>> Cheers,
>> Dinko
>>
>>> --
>>> Schalk W. Cronjé
>>> Twitter / Ello / Toeter : @ysb33r
>
>
>
> --
> Schalk W. Cronjé
> Twitter / Ello / Toeter : @ysb33r
>

Re: respondsTo and constructors

Posted by Schalk Cronjé <ys...@gmail.com>.
I did this for the time being. It works, but I'm not happy about it.

https://github.com/ysb33r/gradleTest/blob/master/src/main/groovy/org/ysb33r/gradle/gradletest/internal/Unpacker.groovy#L37

Lines 37-43.

On 03/05/2015 15:56, Dinko Srkoč wrote:
> On 1 May 2015 at 16:48, Schalk Cronjé <ys...@gmail.com> wrote:
>> I cannot figure out how to check for a constructor using respondsTo. If I
>> had the following
>>
>> class ABC {
>>    ABC(String s, File f) {}
>> }
>>
>> How can I use respondsTo to see if the constructor exists?
> It appears that you cannot. There's a
> `MetaClassImpl#retrieveConstructor(Class[])` that may help, or one can
> always fallback to Java reflection API.
>
> Cheers,
> Dinko
>
>> --
>> Schalk W. Cronjé
>> Twitter / Ello / Toeter : @ysb33r


-- 
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r


Re: respondsTo and constructors

Posted by Dinko Srkoč <di...@gmail.com>.
On 1 May 2015 at 16:48, Schalk Cronjé <ys...@gmail.com> wrote:
> I cannot figure out how to check for a constructor using respondsTo. If I
> had the following
>
> class ABC {
>   ABC(String s, File f) {}
> }
>
> How can I use respondsTo to see if the constructor exists?

It appears that you cannot. There's a
`MetaClassImpl#retrieveConstructor(Class[])` that may help, or one can
always fallback to Java reflection API.

Cheers,
Dinko

>
> --
> Schalk W. Cronjé
> Twitter / Ello / Toeter : @ysb33r