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

Scott Murphy Heiberg updated GROOVY-12756:
------------------------------------------
    Description: 
Anything can be assigned to a variable of type {{Class}}, {{String}} or 
{{Boolean}}: the value is converted at run time, so {{Class c = 
'java.lang.String'}} loads the class. That still compiles for a raw {{Class}}. 
Since GROOVY-12714 (GROOVY_6_0_X and master) static type checking rejects it 
when the variable's type has a type argument, even {{Class<?>}}:

{code:groovy}
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

@groovy.transform.CompileStatic
abstract class Editor<T> {
    final Class<T> targetType
    Editor() {
        Type[] types = ((ParameterizedType) 
getClass().genericSuperclass).actualTypeArguments
        targetType = types[0]
    }
}

@groovy.transform.CompileStatic
void test(Type type, Object object) {
    Class raw = type                          // compiles
    Class<?> any = type                       // error
    Class<String> string = 'java.lang.String' // error
    Class<? extends Number> number = object   // error
}
{code}
{noformat}
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.reflect.Type to: java.lang.Class<T>
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.reflect.Type to: java.lang.Class<?>
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.String to: java.lang.Class<java.lang.String>
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.Object to: java.lang.Class<? extends java.lang.Number>
{noformat}

All of these compiled on GROOVY_6_0_X before GROOVY-12714 (a5d10dd198). The 
first is {{AbstractStructuredBindingEditor}} in Grails, which no longer 
compiles against GROOVY_6_0_X.

For an assignment to a {{Class}}, {{String}} or {{Boolean}} variable, 
{{getResultType}} returned the variable's type. GROOVY-12714 made it keep the 
type of the value when the variable is a parameterized {{Class}}, so that 
{{Class<?>}} is no longer accepted for {{Class<? extends CharSequence>}}. That 
is right when the value is a {{Class}}. When it is not, there are no type 
arguments to compare, and {{checkTypeGenerics}} reports the value's type as an 
incompatible generic argument.

The value's type should be kept only when it is a {{Class}}.

Pull request: https://github.com/apache/groovy/pull/3289

  was:
Anything can be assigned to a variable of type {{Class}}, {{String}} or 
{{Boolean}}: the value is converted at run time, so {{Class c = 
'java.lang.String'}} loads the class. That still compiles for a raw {{Class}}. 
Since GROOVY-12714 (GROOVY_6_0_X and master) static type checking rejects it 
when the variable's type has a type argument, even {{Class<?>}}:

{code:groovy}
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

@groovy.transform.CompileStatic
abstract class Editor<T> {
    final Class<T> targetType
    Editor() {
        Type[] types = ((ParameterizedType) 
getClass().genericSuperclass).actualTypeArguments
        targetType = types[0]
    }
}

@groovy.transform.CompileStatic
void test(Type type, Object object) {
    Class raw = type                          // compiles
    Class<?> any = type                       // error
    Class<String> string = 'java.lang.String' // error
    Class<? extends Number> number = object   // error
}
{code}
{noformat}
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.reflect.Type to: java.lang.Class<T>
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.reflect.Type to: java.lang.Class<?>
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.String to: java.lang.Class<java.lang.String>
[Static type checking] - Incompatible generic argument types. Cannot assign 
java.lang.Object to: java.lang.Class<? extends java.lang.Number>
{noformat}

All of these compiled on GROOVY_6_0_X before GROOVY-12714 (a5d10dd198). The 
first is {{AbstractStructuredBindingEditor}} in Grails, which no longer 
compiles against GROOVY_6_0_X.

For an assignment to a {{Class}}, {{String}} or {{Boolean}} variable, 
{{getResultType}} returned the variable's type. GROOVY-12714 made it keep the 
type of the value when the variable is a parameterized {{Class}}, so that 
{{Class<?>}} is no longer accepted for {{Class<? extends CharSequence>}}. That 
is right when the value is a {{Class}}. When it is not, there are no type 
arguments to compare, and {{checkTypeGenerics}} reports the value's type as an 
incompatible generic argument.

The value's type should be kept only when it is a {{Class}}.


> STC: since GROOVY-12714 a value that is not a Class cannot be assigned to a 
> parameterized Class variable
> --------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12756
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12756
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 7.0.0-alpha-1, 6.0.1
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> Anything can be assigned to a variable of type {{Class}}, {{String}} or 
> {{Boolean}}: the value is converted at run time, so {{Class c = 
> 'java.lang.String'}} loads the class. That still compiles for a raw 
> {{Class}}. Since GROOVY-12714 (GROOVY_6_0_X and master) static type checking 
> rejects it when the variable's type has a type argument, even {{Class<?>}}:
> {code:groovy}
> import java.lang.reflect.ParameterizedType
> import java.lang.reflect.Type
> @groovy.transform.CompileStatic
> abstract class Editor<T> {
>     final Class<T> targetType
>     Editor() {
>         Type[] types = ((ParameterizedType) 
> getClass().genericSuperclass).actualTypeArguments
>         targetType = types[0]
>     }
> }
> @groovy.transform.CompileStatic
> void test(Type type, Object object) {
>     Class raw = type                          // compiles
>     Class<?> any = type                       // error
>     Class<String> string = 'java.lang.String' // error
>     Class<? extends Number> number = object   // error
> }
> {code}
> {noformat}
> [Static type checking] - Incompatible generic argument types. Cannot assign 
> java.lang.reflect.Type to: java.lang.Class<T>
> [Static type checking] - Incompatible generic argument types. Cannot assign 
> java.lang.reflect.Type to: java.lang.Class<?>
> [Static type checking] - Incompatible generic argument types. Cannot assign 
> java.lang.String to: java.lang.Class<java.lang.String>
> [Static type checking] - Incompatible generic argument types. Cannot assign 
> java.lang.Object to: java.lang.Class<? extends java.lang.Number>
> {noformat}
> All of these compiled on GROOVY_6_0_X before GROOVY-12714 (a5d10dd198). The 
> first is {{AbstractStructuredBindingEditor}} in Grails, which no longer 
> compiles against GROOVY_6_0_X.
> For an assignment to a {{Class}}, {{String}} or {{Boolean}} variable, 
> {{getResultType}} returned the variable's type. GROOVY-12714 made it keep the 
> type of the value when the variable is a parameterized {{Class}}, so that 
> {{Class<?>}} is no longer accepted for {{Class<? extends CharSequence>}}. 
> That is right when the value is a {{Class}}. When it is not, there are no 
> type arguments to compare, and {{checkTypeGenerics}} reports the value's type 
> as an incompatible generic argument.
> The value's type should be kept only when it is a {{Class}}.
> Pull request: https://github.com/apache/groovy/pull/3289



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

Reply via email to