You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2014/01/30 13:59:21 UTC
svn commit: r1562803 - in /syncope/trunk: ./
common/src/main/java/org/apache/syncope/common/util/BeanUtils.java pom.xml
Author: ilgrosso
Date: Thu Jan 30 12:59:20 2014
New Revision: 1562803
URL: http://svn.apache.org/r1562803
Log:
[SYNCOPE-462] Update to Spring 3.2.7, Spring Security 3.2.0 + updating local BeanUtils
Modified:
syncope/trunk/ (props changed)
syncope/trunk/common/src/main/java/org/apache/syncope/common/util/BeanUtils.java
syncope/trunk/pom.xml
Propchange: syncope/trunk/
------------------------------------------------------------------------------
Merged /syncope/branches/1_1_X:r1558004-1562787
Modified: syncope/trunk/common/src/main/java/org/apache/syncope/common/util/BeanUtils.java
URL: http://svn.apache.org/viewvc/syncope/trunk/common/src/main/java/org/apache/syncope/common/util/BeanUtils.java?rev=1562803&r1=1562802&r2=1562803&view=diff
==============================================================================
--- syncope/trunk/common/src/main/java/org/apache/syncope/common/util/BeanUtils.java (original)
+++ syncope/trunk/common/src/main/java/org/apache/syncope/common/util/BeanUtils.java Thu Jan 30 12:59:20 2014
@@ -24,12 +24,15 @@ import static org.springframework.beans.
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
import java.util.Map;
-import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
/**
* Overrides Spring's BeanUtils not using collection setters but instead getters + addAll() / putAll(),
@@ -52,7 +55,8 @@ public final class BeanUtils {
* from each other, as long as the properties match. Any bean properties that the
* source bean exposes but the target bean does not will silently be ignored.
* <br/>
- * This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.
+ * This is just a convenience method. For more complex transfer needs,
+ * consider using a full BeanWrapper.
*
* @param source the source bean
* @param target the target bean
@@ -60,7 +64,53 @@ public final class BeanUtils {
* @see org.springframework.beans.BeanWrapper
*/
public static void copyProperties(final Object source, final Object target) throws BeansException {
- copyProperties(source, target, null);
+ copyProperties(source, target, null, (String[]) null);
+ }
+
+ /**
+ * Copy the property values of the given source bean into the given target bean,
+ * only setting properties defined in the given "editable" class (or interface).
+ * <br/>
+ * Note: The source and target classes do not have to match or even be derived
+ * from each other, as long as the properties match. Any bean properties that the
+ * source bean exposes but the target bean does not will silently be ignored.
+ * <br/>
+ * This is just a convenience method. For more complex transfer needs,
+ * consider using a full BeanWrapper.
+ *
+ * @param source the source bean
+ * @param target the target bean
+ * @param editable the class (or interface) to restrict property setting to
+ * @throws BeansException if the copying failed
+ * @see org.springframework.beans.BeanWrapper
+ */
+ public static void copyProperties(final Object source, final Object target, final Class<?> editable)
+ throws BeansException {
+
+ copyProperties(source, target, editable, (String[]) null);
+ }
+
+ /**
+ * Copy the property values of the given source bean into the given target bean,
+ * ignoring the given "ignoreProperties".
+ * <br/>
+ * Note: The source and target classes do not have to match or even be derived
+ * from each other, as long as the properties match. Any bean properties that the
+ * source bean exposes but the target bean does not will silently be ignored.
+ * <br/>
+ * This is just a convenience method. For more complex transfer needs,
+ * consider using a full BeanWrapper.
+ *
+ * @param source the source bean
+ * @param target the target bean
+ * @param ignoreProperties array of property names to ignore
+ * @throws BeansException if the copying failed
+ * @see org.springframework.beans.BeanWrapper
+ */
+ public static void copyProperties(final Object source, final Object target, final String... ignoreProperties)
+ throws BeansException {
+
+ copyProperties(source, target, null, ignoreProperties);
}
/**
@@ -72,55 +122,74 @@ public final class BeanUtils {
*
* @param source the source bean
* @param target the target bean
+ * @param editable the class (or interface) to restrict property setting to
* @param ignoreProperties array of property names to ignore
* @throws BeansException if the copying failed
* @see org.springframework.beans.BeanWrapper
*/
@SuppressWarnings("unchecked")
- public static void copyProperties(final Object source, final Object target, final String[] ignoreProperties)
- throws BeansException {
+ private static void copyProperties(final Object source, final Object target, final Class<?> editable,
+ final String... ignoreProperties) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
- for (PropertyDescriptor targetPd : getPropertyDescriptors(target.getClass())) {
- if (!ArrayUtils.contains(ignoreProperties, targetPd.getName())) {
- PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
- if (sourcePd != null && sourcePd.getReadMethod() != null) {
- try {
- Method readMethod = sourcePd.getReadMethod();
- if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
- readMethod.setAccessible(true);
- }
- Object value = readMethod.invoke(source);
+ Class<?> actualEditable = target.getClass();
+ if (editable != null) {
+ if (!editable.isInstance(target)) {
+ throw new IllegalArgumentException("Target class [" + target.getClass().getName()
+ + "] not assignable to Editable class [" + editable.getName() + "]");
+ }
+ actualEditable = editable;
+ }
+ PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
+ List<String> ignoreList = (ignoreProperties == null)
+ ? Collections.<String>emptyList() : Arrays.asList(ignoreProperties);
+ for (PropertyDescriptor targetPd : targetPds) {
+ if (ignoreProperties == null || (!ignoreList.contains(targetPd.getName()))) {
+ PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
+ if (sourcePd != null) {
+ Method readMethod = sourcePd.getReadMethod();
+ if (readMethod != null) {
Method writeMethod = targetPd.getWriteMethod();
- // Diverts from Spring's BeanUtils: if no write method is found and property is collection,
- // try to use addAll() / putAll().
- if (writeMethod == null) {
- Method targetReadMethod = targetPd.getReadMethod();
- if (targetReadMethod != null) {
- if (!Modifier.isPublic(targetReadMethod.getDeclaringClass().getModifiers())) {
- targetReadMethod.setAccessible(true);
+
+ try {
+ // Diverts from Spring's BeanUtils: if no write method is found and property is collection,
+ // try to use addAll() / putAll().
+ if (writeMethod == null) {
+ Object value = readMethod.invoke(source);
+ Method targetReadMethod = targetPd.getReadMethod();
+ if (targetReadMethod != null) {
+ if (!Modifier.isPublic(targetReadMethod.getDeclaringClass().getModifiers())) {
+ targetReadMethod.setAccessible(true);
+ }
+ Object destValue = targetReadMethod.invoke(target);
+
+ if (value instanceof Collection && destValue instanceof Collection) {
+ ((Collection) destValue).clear();
+ ((Collection) destValue).addAll((Collection) value);
+ } else if (value instanceof Map && destValue instanceof Map) {
+ ((Map) destValue).clear();
+ ((Map) destValue).putAll((Map) value);
+ }
}
- Object destValue = targetReadMethod.invoke(target);
+ } else if (ClassUtils.isAssignable(
+ writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
- if (value instanceof Collection && destValue instanceof Collection) {
- ((Collection) destValue).clear();
- ((Collection) destValue).addAll((Collection) value);
- } else if (value instanceof Map && destValue instanceof Map) {
- ((Map) destValue).clear();
- ((Map) destValue).putAll((Map) value);
+ if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
+ readMethod.setAccessible(true);
}
+ Object value = readMethod.invoke(source);
+ if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
+ writeMethod.setAccessible(true);
+ }
+ writeMethod.invoke(target, value);
}
- } else {
- if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
- writeMethod.setAccessible(true);
- }
- writeMethod.invoke(target, value);
+ } catch (Throwable ex) {
+ throw new FatalBeanException(
+ "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
- } catch (Throwable ex) {
- throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
Modified: syncope/trunk/pom.xml
URL: http://svn.apache.org/viewvc/syncope/trunk/pom.xml?rev=1562803&r1=1562802&r2=1562803&view=diff
==============================================================================
--- syncope/trunk/pom.xml (original)
+++ syncope/trunk/pom.xml Thu Jan 30 12:59:20 2014
@@ -317,8 +317,8 @@ under the License.
<aspectj.version>1.7.4</aspectj.version>
<cxf.version>3.0.0-SNAPSHOT</cxf.version>
- <spring.version>3.2.6.RELEASE</spring.version>
- <spring-security.version>3.1.4.RELEASE</spring-security.version>
+ <spring.version>3.2.7.RELEASE</spring.version>
+ <spring-security.version>3.2.0.RELEASE</spring-security.version>
<jackson.version>2.3.1</jackson.version>
<xstream.version>1.4.6</xstream.version>
<velocity.version>1.7</velocity.version>