Skip to content

Regression in support for property write method return type covariance [SPR-8432] #13078

Closed

Description

@spring-projects-issues

Stepan Koltsov opened SPR-8432 and commented

Regression in 3.1 M2. When subclass overrides getter with narrower return type, Spring fails to set property.

 
public class ReadWritePropertyErrorMain { 

    public static void main(String[] args) { 

        final String contextXml = 
            "<beans xmlns='http://www.springframework.org/schema/beans&#39;" + 
            " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance&#39;" + 
            " xsi:schemaLocation='" + 
            " http://www.springframework.org/schema/beans" + 
            " http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" + 
            " '" + 
            ">" + 
            " <bean id='t' class='" + MyTemplate5.class.getName() + "'/>" + 
            " <bean class='" + MyDaoSupport5.class.getName() + "'>" + 
            " <property name='myTemplate4' ref='t'/>" + 
            " </bean>" + 
            "</beans>"; 

        AbstractXmlApplicationContext applicationContext = new AbstractXmlApplicationContext() { 
            @Override 
            protected Resource[] getConfigResources() { 
                return new Resource[] { new ByteArrayResource(contextXml.getBytes()) }; 
            } 
        }; 

        applicationContext.refresh(); 

        System.out.println("$"); 
    } 
} 

public class MyDaoSupport4 { 

    private MyTemplate4 myTemplate4; 

    public void setMyTemplate4(MyTemplate4 jdbcTemplate4) { 
        this.myTemplate4 = jdbcTemplate4; 
    } 

    public MyTemplate4 getMyTemplate4() { 
        return myTemplate4; 
    } 

} 

public class MyDaoSupport5 extends MyDaoSupport4 { 

    @Override 
    public MyTemplate5 getMyTemplate4() { // <-- narrow subtype is here 
        return (MyTemplate5) super.getMyTemplate4(); 
    } 

} 

public class MyTemplate4 { } 

public class MyTemplate5 extends MyTemplate4 { } 

Error:

 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ru.yandex.commune.junk.stepancheg.spring.readWrite.MyDaoSupport5#0' defined in resource loaded from byte array: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Read and write parameter types are not the same 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
at ru.yandex.commune.junk.stepancheg.spring.readWrite.ReadWriteProperty.main(ReadWriteProperty.java:36) 
Caused by: java.lang.IllegalStateException: Read and write parameter types are not the same 
at org.springframework.core.convert.Property.resolveMethodParameter(Property.java:137) 
at org.springframework.core.convert.Property.<init>(Property.java:56) 
at org.springframework.beans.BeanWrapperImpl.property(BeanWrapperImpl.java:1174) 
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:497) 
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:490) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1371) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1330)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
... 9 more 

Affects: 3.1 M2

Attachments:

Issue Links:

Referenced from: commits 4a8be69, 0ee1256, 5dc2d56

Activity

  1. spring-projects-issues commented on Jun 10, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Stepan Koltsov commented

    This worked in 3.1 M1.

  2. spring-projects-issues commented on Jun 12, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    This very likely has to do with the introduction of ExtendedBeanInfo in 3.1 M2

  3. spring-projects-issues commented on Jun 16, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Thanks for catching this, Stepan.

    In fact, it did not have to do with the introduction of ExtendedBeanInfo in Spring 3.1 M2, but rather a different new class, see below.

    commit 4551864289d5ef0c6b0f31d606ffc6129e735401 (HEAD, SPR-8432)
    Author: Chris Beams <[email protected]>
    Date:   Thu Jun 16 15:40:24 2011 +0800
    
        Fix regression with covariant property return types
        
        Prior to this change, the Property class introduced in 3.1 M2 validated
        read/write property method pairs based on whether their parameter/return
        types were equal to one another.  This precluded the valid possibility
        of read method that returns a subtype of the write method's parameter
        type, and represented a regression against 3.1 M1 and earlier versions.
        
        The implementation now uses isAssignableFrom rather than a straight
        equals check against the types.
        
        Issue: SPR-8432
    
  4. spring-projects-issues commented on Nov 25, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    A general note to watchers of ExtendedBeanInfo-related issues: #14663 is a major refactoring of ExtendedBeanInfo and overall support for non-void returning setter methods.

    If you have submitted a reproduction project with this issue, we have run it through its paces against these new changes, but we would like to ask you to do the same against your actual applications.

    Please consider updating your dev or test builds to work against 3.1.4.BUILD-SNAPSHOT and/or 3.2.0.BUILD-SNAPSHOT to verify, and we would appreciate any feedback, even if it's to let us know that all is well. So that we can consolidate feedback, please add your comments to #14663, and mention the original issue(s) that you were watching.

    Thanks!

    Note also that testing against 3.1.4 is preferable to 3.2.0 because ExtendedBeanInfo is always in the code path in the latter, while in 3.2.0 we've optimized things such that ExtendedBeanInfo is only in play for bean classes that have one or more non-void returning setter methods.

  5. added
    type: regression
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coretype: regression

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions