Refactored CloneableBean

Added slf4j-simple for tests
This commit is contained in:
Patrick Gotthard 2013-11-15 22:24:57 +01:00
parent 4315a3aba2
commit 25aba9758e
2 changed files with 42 additions and 38 deletions

View file

@ -203,6 +203,12 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.7.5</version> <version>1.7.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View file

@ -155,55 +155,53 @@ public class CloneableBean implements Serializable, Cloneable {
* *
*/ */
public Object beanClone() throws CloneNotSupportedException { public Object beanClone() throws CloneNotSupportedException {
Object clonedBean;
final Class<? extends Object> clazz = obj.getClass();
try { try {
clonedBean = obj.getClass().newInstance();
final PropertyDescriptor[] pds = BeanIntrospector.getPropertyDescriptors(obj.getClass()); final Object clonedBean = clazz.newInstance();
if (pds != null) {
for (int i = 0; i < pds.length; i++) { final PropertyDescriptor[] propertyDescriptors = BeanIntrospector.getPropertyDescriptors(clazz);
final Method pReadMethod = pds[i].getReadMethod(); if (propertyDescriptors != null) {
final Method pWriteMethod = pds[i].getWriteMethod(); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (pReadMethod != null && pWriteMethod != null && // ensure
// it has final Method getter = propertyDescriptor.getReadMethod();
// getter final Method setter = propertyDescriptor.getWriteMethod();
// and final String propertyName = propertyDescriptor.getName();
// setter
// methods final boolean getterExists = getter != null;
!ignoreProperties.contains(pds[i].getName()) && // is final boolean setterExists = setter != null;
// not final boolean ignoredProperty = ignoreProperties.contains(propertyName);
// in
// the if (getterExists && setterExists && !ignoredProperty) {
// list
// of final boolean getterFromObject = getter.getDeclaringClass() == Object.class;
// properties final boolean getterWithoutParams = getter.getParameterTypes().length == 0;
// to
// ignore if (!getterFromObject && getterWithoutParams) {
pReadMethod.getDeclaringClass() != Object.class && // filter Object value = getter.invoke(obj, NO_PARAMS);
// Object.class
// getter
// methods
pReadMethod.getParameterTypes().length == 0) { // filter
// getter
// methods
// that
// take
// parameters
Object value = pReadMethod.invoke(obj, NO_PARAMS);
if (value != null) { if (value != null) {
value = doClone(value); value = doClone(value);
pWriteMethod.invoke(clonedBean, new Object[] { value }); setter.invoke(clonedBean, new Object[] { value });
} }
} }
}
} }
} }
return clonedBean;
} catch (final CloneNotSupportedException e) { } catch (final CloneNotSupportedException e) {
LOG.error("Error while cloning bean", e); LOG.error("Error while cloning bean", e);
throw e; throw e;
} catch (final Exception e) { } catch (final Exception e) {
LOG.error("Error while cloning bean", e); LOG.error("Error while cloning bean", e);
throw new CloneNotSupportedException("Cannot clone a " + obj.getClass() + " object"); throw new CloneNotSupportedException("Cannot clone a " + clazz + " object");
} }
return clonedBean;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")