[ https://issues.apache.org/jira/browse/GROOVY-9998?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles updated GROOVY-9998: -------------------------------- Fix Version/s: 3.0.20 > Wildcards with a lower bound fail static type checking > ------------------------------------------------------ > > Key: GROOVY-9998 > URL: https://issues.apache.org/jira/browse/GROOVY-9998 > Project: Groovy > Issue Type: Bug > Components: Static compilation, Static Type Checker > Affects Versions: 3.0.7 > Reporter: Lyuben Atanasov > Assignee: Eric Milles > Priority: Major > Fix For: 4.0.0-alpha-3, 3.0.20 > > > The following code fails to compile when using {{@CompileStatic}}: > {code} > class A { > public final int order; > > public A(int order) { > this.order = order; > } > } > class B extends A { > public B(int order) { > super(order); > } > } > class Test { > public void test() { > Comparator<? super A> comparator = (a1, a2) -> > Integer.compare(a1.order, a2.order); > List<B> list = [new B(2), new B(3), new B(1), new B(0)]; > list.stream().sorted(comparator); > } > } > {code} > The error is: > {noformat} > Exception in thread "main" > org.codehaus.groovy.control.MultipleCompilationErrorsException: startup > failed: > Script_49a5c0348548765c6a0257437aa3b031.groovy: 19: [Static type checking] - > Cannot call java.util.stream.Stream <B>#sorted(java.util.Comparator <? super > B>) with arguments [java.util.Comparator <A>] > @ line 19, column 3. > list.stream().sorted(comparator); > ^ > {noformat} > For some reason the type checker is confused when we have a method with a > generic parameter that has a wildcard with a lower bound. The same code > compiles with javac if I move the A and B classes to separate files and > create the list java-style. > Curiously, if I change the comparator's type from {{Comparator<? super A>}} > to {{Comparator<? extends A>}}, the code compiles successfully in Groovy (and > it shouldn't!), but causes compilation failure in Java. -- This message was sent by Atlassian Jira (v8.20.10#820010)