[
https://issues.apache.org/jira/browse/GROOVY-12623?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Scott Murphy Heiberg updated GROOVY-12623:
------------------------------------------
Description:
{code:groovy}
import java.util.function.Predicate
@groovy.transform.CompileStatic
class C {
static <T> T id(T t) { t }
static void main(args) {
Predicate<String> p = C::id
assert [p.test(''), p.test('x')] == [false, true]
}
}
{code}
Actual: {{java.lang.ClassCastException: class java.lang.String cannot be cast
to class java.lang.Boolean}} at {{p.test('x')}}. The same happens for the get
method of a Map of String to String used as a Predicate (bound or unbound),
List::first, an Optional's get as a BooleanSupplier, and generic instance
methods. For numeric SAMs, a ToIntFunction of String or Character with
{{C::id}} gives "cannot be cast to class java.lang.Number".
Expected: [false, true], as in dynamic Groovy and under @TypeChecked; 120 and
65 for the numeric cases.
When the referenced method returns a type parameter, the static compiler sets
the instantiated return type to the SAM's wrapper type (GROOVY-10975) and
leaves the conversion to LambdaMetafactory, which only casts and unboxes.
Seen in 4.0.33, 5.0.8 and 6.0.0.
PR: https://github.com/apache/groovy/pull/3149
was:
{code:groovy}
import java.util.function.Predicate
@groovy.transform.CompileStatic
class C {
static <T> T id(T t) { t }
static void main(args) {
Predicate<String> p = C::id
assert [p.test(''), p.test('x')] == [false, true]
}
}
{code}
Actual: {{java.lang.ClassCastException: class java.lang.String cannot be cast
to class java.lang.Boolean}} at {{p.test('x')}}. The same happens for the get
method of a Map of String to String used as a Predicate (bound or unbound),
List::first, an Optional's get as a BooleanSupplier, and generic instance
methods. For numeric SAMs, a ToIntFunction of String or Character with
{{C::id}} gives "cannot be cast to class java.lang.Number".
Expected: [false, true], as in dynamic Groovy and under @TypeChecked; 120 and
65 for the numeric cases.
When the referenced method returns a type parameter, the static compiler sets
the instantiated return type to the SAM's wrapper type (GROOVY-10975) and
leaves the conversion to LambdaMetafactory, which only casts and unboxes.
Seen in 4.0.33, 5.0.8 and 6.0.0.
> SC: a method reference to a generic method used for a primitive SAM return
> type (e.g. Predicate) throws ClassCastException instead of applying Groovy
> truth
> -----------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12623
> URL: https://issues.apache.org/jira/browse/GROOVY-12623
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> import java.util.function.Predicate
> @groovy.transform.CompileStatic
> class C {
> static <T> T id(T t) { t }
> static void main(args) {
> Predicate<String> p = C::id
> assert [p.test(''), p.test('x')] == [false, true]
> }
> }
> {code}
> Actual: {{java.lang.ClassCastException: class java.lang.String cannot be cast
> to class java.lang.Boolean}} at {{p.test('x')}}. The same happens for the get
> method of a Map of String to String used as a Predicate (bound or unbound),
> List::first, an Optional's get as a BooleanSupplier, and generic instance
> methods. For numeric SAMs, a ToIntFunction of String or Character with
> {{C::id}} gives "cannot be cast to class java.lang.Number".
> Expected: [false, true], as in dynamic Groovy and under @TypeChecked; 120 and
> 65 for the numeric cases.
> When the referenced method returns a type parameter, the static compiler sets
> the instantiated return type to the SAM's wrapper type (GROOVY-10975) and
> leaves the conversion to LambdaMetafactory, which only casts and unboxes.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3149
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)