[
https://issues.apache.org/jira/browse/GROOVY-12770?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Daniel Sun reassigned GROOVY-12770:
-----------------------------------
Assignee: Daniel Sun
> STC: after instanceof Class, a value is accepted as any parameterized Class
> ---------------------------------------------------------------------------
>
> Key: GROOVY-12770
> URL: https://issues.apache.org/jira/browse/GROOVY-12770
> Project: Groovy
> Issue Type: Bug
> Reporter: Paul King
> Assignee: Daniel Sun
> Priority: Major
>
> Under {{@CompileStatic}} or {{@TypeChecked}}, after {{s instanceof Class}}
> the value is inferred as the raw type {{Class}}, so it is accepted for any
> {{Class<? extends X>}} without a cast:
> {code:groovy}
> import groovy.transform.CompileStatic
> @CompileStatic
> class Main {
> static Number create(Serializable s) {
> if (s instanceof Class) {
> Class<? extends Number> type = s
> return type.getConstructor(String).newInstance('1')
> }
> null
> }
> static void main(String[] args) {
> println create(String)
> }
> }
> {code}
> This compiles and fails when run (4.0.33, 5.1.3, 6.0.0):
> {noformat}
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
> object '1' with class 'java.lang.String' to class 'java.lang.Number'
> {noformat}
> javac rejects {{Class<? extends Number> type = s}} ({{s}} stays a
> {{Serializable}}). The test {{s instanceof Class}} only establishes a
> {{Class<?>}}, the type Java's {{s instanceof Class<?> c}} gives, so inferring
> {{Class<?>}} would make the assignment a compile error unless cast. A
> not-yet-implemented test,
> {{GenericsSTCTest#testClassAfterInstanceofIsCheckedAsClass}}, is added with
> GROOVY-12755.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)