groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Perkins <>
Subject [Bug] [2.5.2] groovy.cli.picocli.CliBuilder.parseFromSpec does not cast its result to the correct type
Date Wed, 12 Sep 2018 03:27:48 GMT

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

View raw message