Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9653

SC: ClassCastException on delegate object property set

    XMLWordPrintableJSON

Details

    Description

      This started to fail in Groovy 2.5.13:

      import com.fasterxml.jackson.annotation.JsonInclude
      @Grab('com.fasterxml.jackson.core:jackson-databind:2.9.9')
      import com.fasterxml.jackson.databind.ObjectMapper
      @Grab('org.springframework:spring-web:4.3.14.RELEASE')
      import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
      
      @groovy.transform.CompileStatic
      final class MyObjectMapper extends ObjectMapper
      {
          MyObjectMapper()
          {
              super(mimicSpring())
          }
      
          private static ObjectMapper mimicSpring()
          {
              new Jackson2ObjectMapperFactoryBean().with {
                  serializationInclusion = JsonInclude.Include.NON_NULL // ClassCastException: org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean cannot be cast to com.fasterxml.jackson.databind.ObjectMapper
                  defaultViewInclusion = true // include w/o view tag
                  failOnUnknownProperties = false
                  autoDetectGettersSetters = true
                  autoDetectFields = false
                  afterPropertiesSet()
                  return object
              }
          }
      
          private static final long serialVersionUID = 2643043846851474223L
      }
      
      println new MyObjectMapper()
      

      Works fine with default (dynamic) compilation.

      In this case, the delegate (Jackson2ObjectMapperFactoryBean) and owner (MyObjectMapper) both supply a setter "setSerializationInclusion" and do not have a field of the same name. StaticCompilationVisitor/StaticTypeCheckingVisitor existsProperty fail to record the receiver type when visiting the setter.

      Attachments

        Activity

          People

            emilles Eric Milles
            emilles Eric Milles
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h