Skip to content

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

Closed
@spring-projects-issues

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: regressionA bug that is also a regression

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions