1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -199,7 +199,7 @@ public static Method findMethod(Class<?> clazz, String methodName, Class<?>... p
199
199
* @return the Method object, or {@code null} if not found
200
200
* @see Class#getDeclaredMethod
201
201
*/
202
- public static Method findDeclaredMethod (Class <?> clazz , String methodName , Class <?>[] paramTypes ) {
202
+ public static Method findDeclaredMethod (Class <?> clazz , String methodName , Class <?>... paramTypes ) {
203
203
try {
204
204
return clazz .getDeclaredMethod (methodName , paramTypes );
205
205
}
@@ -455,7 +455,7 @@ public static PropertyEditor findEditorByConvention(Class<?> targetType) {
455
455
* @param beanClasses the classes to check against
456
456
* @return the property type, or {@code Object.class} as fallback
457
457
*/
458
- public static Class <?> findPropertyType (String propertyName , Class <?>[] beanClasses ) {
458
+ public static Class <?> findPropertyType (String propertyName , Class <?>... beanClasses ) {
459
459
if (beanClasses != null ) {
460
460
for (Class <?> beanClass : beanClasses ) {
461
461
PropertyDescriptor pd = getPropertyDescriptor (beanClass , propertyName );
@@ -528,7 +528,7 @@ public static boolean isSimpleValueType(Class<?> clazz) {
528
528
* @see BeanWrapper
529
529
*/
530
530
public static void copyProperties (Object source , Object target ) throws BeansException {
531
- copyProperties (source , target , null , null );
531
+ copyProperties (source , target , null , ( String []) null );
532
532
}
533
533
534
534
/**
@@ -548,7 +548,7 @@ public static void copyProperties(Object source, Object target) throws BeansExce
548
548
public static void copyProperties (Object source , Object target , Class <?> editable )
549
549
throws BeansException {
550
550
551
- copyProperties (source , target , editable , null );
551
+ copyProperties (source , target , editable , ( String []) null );
552
552
}
553
553
554
554
/**
@@ -565,7 +565,7 @@ public static void copyProperties(Object source, Object target, Class<?> editabl
565
565
* @throws BeansException if the copying failed
566
566
* @see BeanWrapper
567
567
*/
568
- public static void copyProperties (Object source , Object target , String [] ignoreProperties )
568
+ public static void copyProperties (Object source , Object target , String ... ignoreProperties )
569
569
throws BeansException {
570
570
571
571
copyProperties (source , target , null , ignoreProperties );
@@ -583,7 +583,7 @@ public static void copyProperties(Object source, Object target, String[] ignoreP
583
583
* @throws BeansException if the copying failed
584
584
* @see BeanWrapper
585
585
*/
586
- private static void copyProperties (Object source , Object target , Class <?> editable , String [] ignoreProperties )
586
+ private static void copyProperties (Object source , Object target , Class <?> editable , String ... ignoreProperties )
587
587
throws BeansException {
588
588
589
589
Assert .notNull (source , "Source must not be null" );
@@ -601,24 +601,27 @@ private static void copyProperties(Object source, Object target, Class<?> editab
601
601
List <String > ignoreList = (ignoreProperties != null ) ? Arrays .asList (ignoreProperties ) : null ;
602
602
603
603
for (PropertyDescriptor targetPd : targetPds ) {
604
- if ( targetPd .getWriteMethod () != null &&
605
- (ignoreProperties == null || (!ignoreList .contains (targetPd .getName ())))) {
604
+ Method writeMethod = targetPd .getWriteMethod ();
605
+ if ( writeMethod != null && (ignoreProperties == null || (!ignoreList .contains (targetPd .getName ())))) {
606
606
PropertyDescriptor sourcePd = getPropertyDescriptor (source .getClass (), targetPd .getName ());
607
- if (sourcePd != null && sourcePd .getReadMethod () != null ) {
608
- try {
609
- Method readMethod = sourcePd .getReadMethod ();
610
- if (!Modifier .isPublic (readMethod .getDeclaringClass ().getModifiers ())) {
611
- readMethod .setAccessible (true );
607
+ if (sourcePd != null ) {
608
+ Method readMethod = sourcePd .getReadMethod ();
609
+ if (readMethod != null &&
610
+ writeMethod .getParameterTypes ()[0 ].isAssignableFrom (readMethod .getReturnType ())) {
611
+ try {
612
+ if (!Modifier .isPublic (readMethod .getDeclaringClass ().getModifiers ())) {
613
+ readMethod .setAccessible (true );
614
+ }
615
+ Object value = readMethod .invoke (source );
616
+ if (!Modifier .isPublic (writeMethod .getDeclaringClass ().getModifiers ())) {
617
+ writeMethod .setAccessible (true );
618
+ }
619
+ writeMethod .invoke (target , value );
612
620
}
613
- Object value = readMethod .invoke (source );
614
- Method writeMethod = targetPd .getWriteMethod ();
615
- if (!Modifier .isPublic (writeMethod .getDeclaringClass ().getModifiers ())) {
616
- writeMethod .setAccessible (true );
621
+ catch (Throwable ex ) {
622
+ throw new FatalBeanException (
623
+ "Could not copy property '" + targetPd .getName () + "' from source to target" , ex );
617
624
}
618
- writeMethod .invoke (target , value );
619
- }
620
- catch (Throwable ex ) {
621
- throw new FatalBeanException ("Could not copy properties from source to target" , ex );
622
625
}
623
626
}
624
627
}
0 commit comments