11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2013 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * 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
199199 * @return the Method object, or {@code null} if not found
200200 * @see Class#getDeclaredMethod
201201 */
202- public static Method findDeclaredMethod (Class <?> clazz , String methodName , Class <?>[] paramTypes ) {
202+ public static Method findDeclaredMethod (Class <?> clazz , String methodName , Class <?>... paramTypes ) {
203203 try {
204204 return clazz .getDeclaredMethod (methodName , paramTypes );
205205 }
@@ -455,7 +455,7 @@ public static PropertyEditor findEditorByConvention(Class<?> targetType) {
455455 * @param beanClasses the classes to check against
456456 * @return the property type, or {@code Object.class} as fallback
457457 */
458- public static Class <?> findPropertyType (String propertyName , Class <?>[] beanClasses ) {
458+ public static Class <?> findPropertyType (String propertyName , Class <?>... beanClasses ) {
459459 if (beanClasses != null ) {
460460 for (Class <?> beanClass : beanClasses ) {
461461 PropertyDescriptor pd = getPropertyDescriptor (beanClass , propertyName );
@@ -528,7 +528,7 @@ public static boolean isSimpleValueType(Class<?> clazz) {
528528 * @see BeanWrapper
529529 */
530530 public static void copyProperties (Object source , Object target ) throws BeansException {
531- copyProperties (source , target , null , null );
531+ copyProperties (source , target , null , ( String []) null );
532532 }
533533
534534 /**
@@ -548,7 +548,7 @@ public static void copyProperties(Object source, Object target) throws BeansExce
548548 public static void copyProperties (Object source , Object target , Class <?> editable )
549549 throws BeansException {
550550
551- copyProperties (source , target , editable , null );
551+ copyProperties (source , target , editable , ( String []) null );
552552 }
553553
554554 /**
@@ -565,7 +565,7 @@ public static void copyProperties(Object source, Object target, Class<?> editabl
565565 * @throws BeansException if the copying failed
566566 * @see BeanWrapper
567567 */
568- public static void copyProperties (Object source , Object target , String [] ignoreProperties )
568+ public static void copyProperties (Object source , Object target , String ... ignoreProperties )
569569 throws BeansException {
570570
571571 copyProperties (source , target , null , ignoreProperties );
@@ -583,7 +583,7 @@ public static void copyProperties(Object source, Object target, String[] ignoreP
583583 * @throws BeansException if the copying failed
584584 * @see BeanWrapper
585585 */
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 )
587587 throws BeansException {
588588
589589 Assert .notNull (source , "Source must not be null" );
@@ -601,24 +601,27 @@ private static void copyProperties(Object source, Object target, Class<?> editab
601601 List <String > ignoreList = (ignoreProperties != null ) ? Arrays .asList (ignoreProperties ) : null ;
602602
603603 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 ())))) {
606606 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 );
612620 }
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 );
617624 }
618- writeMethod .invoke (target , value );
619- }
620- catch (Throwable ex ) {
621- throw new FatalBeanException ("Could not copy properties from source to target" , ex );
622625 }
623626 }
624627 }
0 commit comments