commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Commented: (BEANUTILS-281) BeanUtils.cloneBean and Covariant (Overriding) return types
Date Thu, 12 Jul 2007 21:36:04 GMT

    [ https://issues.apache.org/jira/browse/BEANUTILS-281?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12512268
] 

Henri Yandell commented on BEANUTILS-281:
-----------------------------------------

Just to follow up more on this issue, the type of which I've seen crop up in Spring and Hibernate
too:

http://opensource.atlassian.com/projects/spring/browse/SPR-2727
http://opensource.atlassian.com/projects/hibernate/browse/HHH-442
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2268
http://opensource.atlassian.com/projects/spring/browse/SPR-2968

The real bug is down in PropertyUtils.getPropertyDescriptors(Class). If you evoke that on
Car.class, it considers that the AbstractVehicle.getField();Serializable class it the read
method [wrong], and that there is no write method [wrong]. Basically it gives the result you
would expect from evoking it on AbtractVehicle.class.

This is due to errors in the data returned from Introspector.getBeanInfo(Car.class).getPropertyDescriptors().

The solution is most likely to write our own version of the Introspector's getBeanInfo method:

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html#getBeanInfo(java.lang.Class)

> BeanUtils.cloneBean and Covariant (Overriding) return types
> -----------------------------------------------------------
>
>                 Key: BEANUTILS-281
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-281
>             Project: Commons BeanUtils
>          Issue Type: Bug
>          Components: Bean / Property Utils
>         Environment: JDK1.5
>            Reporter: Onur Kutlu GAGO
>             Fix For: LATER THAN 1.8.0
>
>
> BeanUtils.cloneBean(Object) method does not copy the fields that are overriden by the
subclasses. For example, consider an abstract class(AbstractVehicle) where you define an abstract
getter for a field. 
> **************************
> public abstract class AbstractVehicle {
> 	public abstract Serializable getField();
> }
> ***************************
> In a class (Car) that extends this abstract class (AbstractVehicle) you define the field
itself and override the return type of the getter method (from Serializable to Integer):
> ***************************
> public class Car extends AbstractVehicle {
> 	private Integer field = null;
> 	@Override
> 	public Integer getField() {
> 		return field;
> 	}
> 	public void setField(Integer field) {
> 		this.field = field;
> 	}
> }
> ***************************
> When you clone such objects (Car) this field is not copied! The following code prints
'null' instead of 5!
> ***************************
> public class CopyTestMain {
> 	public static void main(String[] args) throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
> 		final Car aCar = new Car();
> 		aCar.setField(5);
> 		final Car copyCar = (Car) BeanUtils.cloneBean(aCar);
> 		System.out.println("Field = " + copyCar.getField());
> 	}
> }
> ***************************

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message