org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1975/10/29' with class 'java.lang.String' to class 'java.lang.Number'

Apache's JIRA Issue Tracker | Aaron Zeckoski | 7 years ago
  1. 0

    It does not seem possible to overload a setter with multiple methods. Groovy seems to randomly decide to fail with cast exceptions rather than actually calling the correct setter. Here is the code: public class Person { // id is added by grails String name String email Date birthdate int age = 0 PersonSex sex = PersonSex.unknown boolean active = true void setBirthdate(String date) { // try to convert from string def df = new SimpleDateFormat("yyyy/MM/dd") birthdate = df.parse(date); } void setBirthdate(Number date) { birthdate = new Date(date) } } I have a few tests to verify that I can create a user with a Date, String, or Long With the code above it is not possible to set the bithdate to a Date anymore as I always get a cast exception. If I change "Number" to "Long" or "long" then the String test passes but the number test fails. If I leave it as shown above then the String test fails with this exception: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1975/10/29' with class 'java.lang.String' to class 'java.lang.Number' at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:132) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:261) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2392) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3307) at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1506) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1483) at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1404) at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:775) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNewN(ScriptBytecodeAdapter.java:220) at PersonTest.testPersonStringDate(PersonTests.groovy:32) It seems like groovy is only able to work with a single setter method but it should be able to handle overloaded methods and properly call the one which matches the type.

    Apache's JIRA Issue Tracker | 7 years ago | Aaron Zeckoski
    org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1975/10/29' with class 'java.lang.String' to class 'java.lang.Number'
  2. 0

    Configuring TelluriumPowerUtilityDiagnose in project

    Google Groups | 7 years ago | Khushwinder Singh
    org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{}' with class 'groovy.util.ConfigObject' to class 'java.lang.Number'
  3. 0

    Uninitialized tariff at start of game

    GitHub | 6 years ago | jecollins
    org.quartz.JobExecutionException: Cannot cast object 'null' with class 'null' to class 'java.lang.Number' [See nested exception: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'java.lang.Number']
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Getting started app doesn't work for me with JSON

    Google Groups | 5 years ago | Brad Whitaker
    org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'works' with class 'java.lang.String' to class 'java.util.Map'
  6. 0

    Invalid Cast to BigDecimal

    GitHub | 2 years ago | polariss0i
    org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '30' with class 'java.lang.String' to class 'java.math.BigDecimal'

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.codehaus.groovy.runtime.typehandling.GroovyCastException

      Cannot cast object '1975/10/29' with class 'java.lang.String' to class 'java.lang.Number'

      at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber()
    2. Groovy
      ScriptBytecodeAdapter.invokeNewN
      1. org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:132)
      2. org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:261)
      3. groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2392)
      4. groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3307)
      5. groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1506)
      6. groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1483)
      7. groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1404)
      8. org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:775)
      9. org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNewN(ScriptBytecodeAdapter.java:220)
      9 frames
    3. Unknown
      PersonTest.testPersonStringDate
      1. PersonTest.testPersonStringDate(PersonTests.groovy:32)
      1 frame