You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Kevin Perkins <Ke...@kp.org> on 2018/09/12 03:27:48 UTC

[Bug] [2.5.2] groovy.cli.picocli.CliBuilder.parseFromSpec does not cast its result to the correct type

Hello,

I think I've found a bug with groovy.cli.picocli.CliBuilder as of Groovy 2.5.2. I didn't find this particular issue in JIRA. I figured I'd ask here. The parseFromSpec method of groovy.cli.picocli.CliBuilder seems to always return a LinkedHashMap instead of the expected type (the class given as the first argument). The source my IDE shows me for this method has "cliOptions as T" as the last line in the method. I think that the problem is T is erased when the method is compiled and so the "as T" does (effectively) nothing. I think the correct code would be "cliOptions.asType(optionsClass)".

Environment information:
/* -------------- groovy --version ------------------------------------ */
Groovy Version: 2.5.2 JVM: 1.8.0_172 Vendor: Oracle Corporation OS: Windows 7

/* --------------- IDE version info ------------------------------------- */
IntelliJ IDEA 2018.2.3 (Ultimate Edition)
Build #IU-182.4323.46, built on September 3, 2018
JRE: 1.8.0_152-release-1248-b8 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

The following code demonstrates the issue:
/* ------------------------ Begin Example Code ------------------------ */
import groovy.cli.Option
import groovy.cli.picocli.CliBuilder

class CliBuilderBug {
    interface CliArguments {
        @Option(longName='dummy') int dummy()
    }

    static void main(String[] args) {
        CliBuilder builder = new CliBuilder()

        def ok = builder.parseFromSpec(CliArguments, args) as CliArguments
        assert ok instanceof CliArguments // ok
        println "Using 'as' works."

        def notOk = builder.parseFromSpec(CliArguments, args)
        assert notOk instanceof LinkedHashMap // this assertion is true
        assert notOk instanceof CliArguments // fails
        println 'We never get here.'
    }
}
/* ------------------------ End Example Code ------------------------ */

I hope this is helpful, and thank you for your time.
- Kevin Perkins
NOTICE TO RECIPIENT:  If you are not the intended recipient of this e-mail, you are prohibited from sharing, copying, or otherwise using or disclosing its contents.  If you have received this e-mail in error, please notify the sender immediately by reply e-mail and permanently delete this e-mail and any attachments without reading, forwarding or saving them.  Thank you.


Re: [Bug] [2.5.2] groovy.cli.picocli.CliBuilder.parseFromSpec does not cast its result to the correct type

Posted by Remko Popma <re...@gmail.com>.
I do remember experiencing a similar problem at some point while working on the unit tests. 

I don’t remember how it got resolved. Could it be an environmental issue?

Maybe other people on this list have also seen this issue and can shed some light? (I think it’s a general Groovy question, not something particular to CliBuilder.)

Remko

(Shameless plug) Every java main() method deserves http://picocli.info

> On Sep 12, 2018, at 13:40, Remko Popma <re...@gmail.com> wrote:
> 
> Kevin, that’s strange, there are working unit tests that do exactly the same...
> For example:
> 
> https://github.com/apache/groovy/blob/GROOVY_2_5_X/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy#L794
> 
> 
>> On Wed, Sep 12, 2018 at 12:52 Kevin Perkins <Ke...@kp.org> wrote:
>> Hello,
>> 
>> I think I've found a bug with groovy.cli.picocli.CliBuilder as of Groovy 2.5.2. I didn't find this particular issue in JIRA. I figured I'd ask here. The parseFromSpec method of groovy.cli.picocli.CliBuilder seems to always return a LinkedHashMap instead of the expected type (the class given as the first argument). The source my IDE shows me for this method has "cliOptions as T" as the last line in the method. I think that the problem is T is erased when the method is compiled and so the "as T" does (effectively) nothing. I think the correct code would be "cliOptions.asType(optionsClass)".
>> 
>> Environment information:
>> /* -------------- groovy --version ------------------------------------ */
>> Groovy Version: 2.5.2 JVM: 1.8.0_172 Vendor: Oracle Corporation OS: Windows 7
>> 
>> /* --------------- IDE version info ------------------------------------- */
>> IntelliJ IDEA 2018.2.3 (Ultimate Edition)
>> Build #IU-182.4323.46, built on September 3, 2018
>> JRE: 1.8.0_152-release-1248-b8 amd64
>> JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
>> Windows 7 6.1
>> 
>> The following code demonstrates the issue:
>> /* ------------------------ Begin Example Code ------------------------ */
>> import groovy.cli.Option
>> import groovy.cli.picocli.CliBuilder
>> 
>> class CliBuilderBug {
>>     interface CliArguments {
>>         @Option(longName='dummy') int dummy()
>>     }
>> 
>>     static void main(String[] args) {
>>         CliBuilder builder = new CliBuilder()
>> 
>>         def ok = builder.parseFromSpec(CliArguments, args) as CliArguments
>>         assert ok instanceof CliArguments // ok
>>         println "Using 'as' works."
>> 
>>         def notOk = builder.parseFromSpec(CliArguments, args)
>>         assert notOk instanceof LinkedHashMap // this assertion is true
>>         assert notOk instanceof CliArguments // fails
>>         println 'We never get here.'
>>     }
>> }
>> /* ------------------------ End Example Code ------------------------ */
>> 
>> I hope this is helpful, and thank you for your time.
>> - Kevin Perkins
>> NOTICE TO RECIPIENT:  If you are not the intended recipient of this e-mail, you are prohibited from sharing, copying, or otherwise using or disclosing its contents.  If you have received this e-mail in error, please notify the sender immediately by reply e-mail and permanently delete this e-mail and any attachments without reading, forwarding or saving them.  Thank you.
>> 

Re: [Bug] [2.5.2] groovy.cli.picocli.CliBuilder.parseFromSpec does not cast its result to the correct type

Posted by Remko Popma <re...@gmail.com>.
Kevin, that’s strange, there are working unit tests that do exactly the
same...
For example:

https://github.com/apache/groovy/blob/GROOVY_2_5_X/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy#L794


On Wed, Sep 12, 2018 at 12:52 Kevin Perkins <Ke...@kp.org> wrote:

> Hello,
>
> I think I've found a bug with groovy.cli.picocli.CliBuilder as of Groovy
> 2.5.2. I didn't find this particular issue in JIRA. I figured I'd ask here.
> The parseFromSpec method of groovy.cli.picocli.CliBuilder seems to always
> return a LinkedHashMap instead of the expected type (the class given as the
> first argument). The source my IDE shows me for this method has "cliOptions
> as T" as the last line in the method. I think that the problem is T is
> erased when the method is compiled and so the "as T" does (effectively)
> nothing. I think the correct code would be
> "cliOptions.asType(optionsClass)".
>
> Environment information:
> /* -------------- groovy --version ------------------------------------ */
> Groovy Version: 2.5.2 JVM: 1.8.0_172 Vendor: Oracle Corporation OS:
> Windows 7
>
> /* --------------- IDE version info -------------------------------------
> */
> IntelliJ IDEA 2018.2.3 (Ultimate Edition)
> Build #IU-182.4323.46, built on September 3, 2018
> JRE: 1.8.0_152-release-1248-b8 amd64
> JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
> Windows 7 6.1
>
> The following code demonstrates the issue:
> /* ------------------------ Begin Example Code ------------------------ */
> import groovy.cli.Option
> import groovy.cli.picocli.CliBuilder
>
> class CliBuilderBug {
>     interface CliArguments {
>         @Option(longName='dummy') int dummy()
>     }
>
>     static void main(String[] args) {
>         CliBuilder builder = new CliBuilder()
>
>         def ok = builder.parseFromSpec(CliArguments, args) as CliArguments
>         assert ok instanceof CliArguments // ok
>         println "Using 'as' works."
>
>         def notOk = builder.parseFromSpec(CliArguments, args)
>         assert notOk instanceof LinkedHashMap // this assertion is true
>         assert notOk instanceof CliArguments // fails
>         println 'We never get here.'
>     }
> }
> /* ------------------------ End Example Code ------------------------ */
>
> I hope this is helpful, and thank you for your time.
> - Kevin Perkins
> NOTICE TO RECIPIENT:  If you are not the intended recipient of this
> e-mail, you are prohibited from sharing, copying, or otherwise using or
> disclosing its contents.  If you have received this e-mail in error, please
> notify the sender immediately by reply e-mail and permanently delete this
> e-mail and any attachments without reading, forwarding or saving them.
> Thank you.
>
>