Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-281

BeanUtils.cloneBean and Covariant (Overriding) return types

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • None
    • 1.9.5
    • Bean / Property Utils
    • None
    • JDK1.5

    Description

      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()); }

      }
      ***************************

      Attachments

        Activity

          People

            Unassigned Unassigned
            okgago Onur Kutlu GAGO
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: