[ https://issues.apache.org/jira/browse/GROOVY-11410?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17856356#comment-17856356 ]
Eric Milles edited comment on GROOVY-11410 at 6/20/24 2:09 PM: --------------------------------------------------------------- There is an implicit conversion to Class. I think it takes the string value of the right expression. I never quite understood why the right could be anything besides a string. STC does present an opportunity to tighten this up. {code:java} public class ShortTypeHandling { public static Class castToClass(Object object) { if (object==null) return null; if (object instanceof Class) return (Class) object; try { return Class.forName(object.toString()); } catch (Exception e) { throw new GroovyCastException(object, Class.class, e); } } {code} was (Author: emilles): There is an implicit conversion to Class. I think it takes the string value of the right expression. I never quite understood why the right could be anything besides a string. STC does present an opportunity to tighten this up. > type mismatch when the expected type is java.lang.Class > ------------------------------------------------------- > > Key: GROOVY-11410 > URL: https://issues.apache.org/jira/browse/GROOVY-11410 > Project: Groovy > Issue Type: Bug > Components: Static Type Checker > Reporter: Thodoris Sotiropoulos > Priority: Minor > > I have the following program > {code:java} > class Test { > public static void main(String [] args) { > java.lang.Class<String> lapse = 1; > } > } {code} > h3. Actual behavior > The code compiles, but I get the following runtime error: > {code:java} > Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot > cast object '1' with class 'java.lang.Integer' to class 'java.lang.Class' due > to: java.lang.ClassNotFoundException: 1 > org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast > object '1' with class 'java.lang.Integer' to class 'java.lang.Class' due to: > java.lang.ClassNotFoundException: 1 > at Test.main(test.groovy:3){code} > h3. Expected behavior > The code should have been rejected > Tested against master (commit: e6b9133726af9cdea2bc6db7b55523cd88f7190e) -- This message was sent by Atlassian Jira (v8.20.10#820010)