[ 
https://issues.apache.org/jira/browse/GROOVY-12769?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Daniel Sun reassigned GROOVY-12769:
-----------------------------------

    Assignee: Daniel Sun

> STC: a Class of a type-variable value is accepted as Class<? extends U> 
> through a generic method call
> -----------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12769
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12769
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Assignee: Daniel Sun
>            Priority: Major
>
> Under {{@CompileStatic}} or {{@TypeChecked}}, {{u.getClass()}} for a {{U u}} 
> is a {{Class<? extends Object>}}: {{U}} may be a parameterized type, such as 
> {{List<String>}}, and a class has no type arguments. Assigned directly to a 
> {{Class<? extends U>}} it is rejected, as in Java, but passed through a 
> generic method such as {{Collections.singletonList}} it is accepted:
> {code:groovy}
> import groovy.transform.CompileStatic
> @CompileStatic
> class Main {
>     static <U> Class<? extends U> classOf(U u) {
>         List<Class<? extends U>> list = 
> Collections.singletonList(u.getClass())
>         list[0]
>     }
>     static void main(String[] args) {
>         Class<? extends List<String>> type = classOf(new ArrayList<String>())
>         List<String> strings = type.cast(new ArrayList<Integer>([1]))
>         println strings[0].length()
>     }
> }
> {code}
> This compiles with no cast or warning and fails when run (4.0.33, 5.1.3, 
> 6.0.0):
> {noformat}
> java.lang.ClassCastException: class java.lang.Integer cannot be cast to class 
> java.lang.String
>         at Main.main(...:13)
> {noformat}
> javac rejects the equivalent Java code ("inference variable T has 
> incompatible bounds"). The assignment should be a compile error. A 
> not-yet-implemented test, 
> {{GenericsSTCTest#testClassOfTypeVariableIsNotTypeArgumentOfGenericCall}}, is 
> added with GROOVY-12755.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to