Browse Source

Parameterized/generified static methods in BeanUtils.

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1681 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Sam Brannen 17 years ago
parent
commit
68d07767ec
  1. 45
      org.springframework.beans/src/main/java/org/springframework/beans/BeanUtils.java

45
org.springframework.beans/src/main/java/org/springframework/beans/BeanUtils.java

@ -51,13 +51,14 @@ import org.springframework.util.StringUtils;
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
* @author Sam Brannen
*/ */
public abstract class BeanUtils { public abstract class BeanUtils {
private static final Log logger = LogFactory.getLog(BeanUtils.class); private static final Log logger = LogFactory.getLog(BeanUtils.class);
private static final Map<Class, Boolean> unknownEditorTypes = private static final Map<Class<?>, Boolean> unknownEditorTypes =
Collections.synchronizedMap(new WeakHashMap<Class, Boolean>()); Collections.synchronizedMap(new WeakHashMap<Class<?>, Boolean>());
/** /**
@ -96,7 +97,7 @@ public abstract class BeanUtils {
* @return the new instance * @return the new instance
* @throws BeanInstantiationException if the bean cannot be instantiated * @throws BeanInstantiationException if the bean cannot be instantiated
*/ */
public static Object instantiateClass(Class clazz) throws BeanInstantiationException { public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
if (clazz.isInterface()) { if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface"); throw new BeanInstantiationException(clazz, "Specified class is an interface");
@ -120,7 +121,7 @@ public abstract class BeanUtils {
* @return the new instance * @return the new instance
* @throws BeanInstantiationException if the bean cannot be instantiated * @throws BeanInstantiationException if the bean cannot be instantiated
*/ */
public static Object instantiateClass(Constructor ctor, Object... args) throws BeanInstantiationException { public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
Assert.notNull(ctor, "Constructor must not be null"); Assert.notNull(ctor, "Constructor must not be null");
try { try {
ReflectionUtils.makeAccessible(ctor); ReflectionUtils.makeAccessible(ctor);
@ -158,7 +159,7 @@ public abstract class BeanUtils {
* @see java.lang.Class#getMethod * @see java.lang.Class#getMethod
* @see #findDeclaredMethod * @see #findDeclaredMethod
*/ */
public static Method findMethod(Class clazz, String methodName, Class... paramTypes) { public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
try { try {
return clazz.getMethod(methodName, paramTypes); return clazz.getMethod(methodName, paramTypes);
} }
@ -178,7 +179,7 @@ public abstract class BeanUtils {
* @return the Method object, or <code>null</code> if not found * @return the Method object, or <code>null</code> if not found
* @see java.lang.Class#getDeclaredMethod * @see java.lang.Class#getDeclaredMethod
*/ */
public static Method findDeclaredMethod(Class clazz, String methodName, Class[] paramTypes) { public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
try { try {
return clazz.getDeclaredMethod(methodName, paramTypes); return clazz.getDeclaredMethod(methodName, paramTypes);
} }
@ -205,7 +206,7 @@ public abstract class BeanUtils {
* @see java.lang.Class#getMethods * @see java.lang.Class#getMethods
* @see #findDeclaredMethodWithMinimalParameters * @see #findDeclaredMethodWithMinimalParameters
*/ */
public static Method findMethodWithMinimalParameters(Class clazz, String methodName) public static Method findMethodWithMinimalParameters(Class<?> clazz, String methodName)
throws IllegalArgumentException { throws IllegalArgumentException {
Method targetMethod = findMethodWithMinimalParameters(clazz.getMethods(), methodName); Method targetMethod = findMethodWithMinimalParameters(clazz.getMethods(), methodName);
@ -227,7 +228,7 @@ public abstract class BeanUtils {
* could not be resolved to a unique method with minimal parameters * could not be resolved to a unique method with minimal parameters
* @see java.lang.Class#getDeclaredMethods * @see java.lang.Class#getDeclaredMethods
*/ */
public static Method findDeclaredMethodWithMinimalParameters(Class clazz, String methodName) public static Method findDeclaredMethodWithMinimalParameters(Class<?> clazz, String methodName)
throws IllegalArgumentException { throws IllegalArgumentException {
Method targetMethod = findMethodWithMinimalParameters(clazz.getDeclaredMethods(), methodName); Method targetMethod = findMethodWithMinimalParameters(clazz.getDeclaredMethods(), methodName);
@ -293,7 +294,7 @@ public abstract class BeanUtils {
* @see #findMethod * @see #findMethod
* @see #findMethodWithMinimalParameters * @see #findMethodWithMinimalParameters
*/ */
public static Method resolveSignature(String signature, Class clazz) { public static Method resolveSignature(String signature, Class<?> clazz) {
Assert.hasText(signature, "'signature' must not be empty"); Assert.hasText(signature, "'signature' must not be empty");
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
@ -315,7 +316,7 @@ public abstract class BeanUtils {
String methodName = signature.substring(0, firstParen); String methodName = signature.substring(0, firstParen);
String[] parameterTypeNames = String[] parameterTypeNames =
StringUtils.commaDelimitedListToStringArray(signature.substring(firstParen + 1, lastParen)); StringUtils.commaDelimitedListToStringArray(signature.substring(firstParen + 1, lastParen));
Class[] parameterTypes = new Class[parameterTypeNames.length]; Class<?>[] parameterTypes = new Class[parameterTypeNames.length];
for (int i = 0; i < parameterTypeNames.length; i++) { for (int i = 0; i < parameterTypeNames.length; i++) {
String parameterTypeName = parameterTypeNames[i].trim(); String parameterTypeName = parameterTypeNames[i].trim();
try { try {
@ -337,7 +338,7 @@ public abstract class BeanUtils {
* @return an array of <code>PropertyDescriptors</code> for the given class * @return an array of <code>PropertyDescriptors</code> for the given class
* @throws BeansException if PropertyDescriptor look fails * @throws BeansException if PropertyDescriptor look fails
*/ */
public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws BeansException { public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz); CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getBeanInfo().getPropertyDescriptors(); return cr.getBeanInfo().getPropertyDescriptors();
} }
@ -349,7 +350,7 @@ public abstract class BeanUtils {
* @return the corresponding PropertyDescriptor, or <code>null</code> if none * @return the corresponding PropertyDescriptor, or <code>null</code> if none
* @throws BeansException if PropertyDescriptor lookup fails * @throws BeansException if PropertyDescriptor lookup fails
*/ */
public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName)
throws BeansException { throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz); CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
@ -384,7 +385,7 @@ public abstract class BeanUtils {
* @param targetType the type to find an editor for * @param targetType the type to find an editor for
* @return the corresponding editor, or <code>null</code> if none found * @return the corresponding editor, or <code>null</code> if none found
*/ */
public static PropertyEditor findEditorByConvention(Class targetType) { public static PropertyEditor findEditorByConvention(Class<?> targetType) {
if (targetType == null || targetType.isArray() || unknownEditorTypes.containsKey(targetType)) { if (targetType == null || targetType.isArray() || unknownEditorTypes.containsKey(targetType)) {
return null; return null;
} }
@ -397,7 +398,7 @@ public abstract class BeanUtils {
} }
String editorName = targetType.getName() + "Editor"; String editorName = targetType.getName() + "Editor";
try { try {
Class editorClass = cl.loadClass(editorName); Class<?> editorClass = cl.loadClass(editorName);
if (!PropertyEditor.class.isAssignableFrom(editorClass)) { if (!PropertyEditor.class.isAssignableFrom(editorClass)) {
logger.warn("Editor class [" + editorName + logger.warn("Editor class [" + editorName +
"] does not implement [java.beans.PropertyEditor] interface"); "] does not implement [java.beans.PropertyEditor] interface");
@ -423,9 +424,9 @@ public abstract class BeanUtils {
* @param beanClasses the classes to check against * @param beanClasses the classes to check against
* @return the property type, or <code>Object.class</code> as fallback * @return the property type, or <code>Object.class</code> as fallback
*/ */
public static Class findPropertyType(String propertyName, Class[] beanClasses) { public static Class<?> findPropertyType(String propertyName, Class<?>[] beanClasses) {
if (beanClasses != null) { if (beanClasses != null) {
for (Class beanClass : beanClasses) { for (Class<?> beanClass : beanClasses) {
PropertyDescriptor pd = getPropertyDescriptor(beanClass, propertyName); PropertyDescriptor pd = getPropertyDescriptor(beanClass, propertyName);
if (pd != null) { if (pd != null) {
return pd.getPropertyType(); return pd.getPropertyType();
@ -461,7 +462,7 @@ public abstract class BeanUtils {
* @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE * @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE
* @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#checkDependencies * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#checkDependencies
*/ */
public static boolean isSimpleProperty(Class clazz) { public static boolean isSimpleProperty(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType())); return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType()));
} }
@ -473,7 +474,7 @@ public abstract class BeanUtils {
* @param clazz the type to check * @param clazz the type to check
* @return whether the given type represents a "simple" value type * @return whether the given type represents a "simple" value type
*/ */
public static boolean isSimpleValueType(Class clazz) { public static boolean isSimpleValueType(Class<?> clazz) {
return ClassUtils.isPrimitiveOrWrapper(clazz) || CharSequence.class.isAssignableFrom(clazz) || return ClassUtils.isPrimitiveOrWrapper(clazz) || CharSequence.class.isAssignableFrom(clazz) ||
Number.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) ||
clazz.equals(URI.class) || clazz.equals(URL.class) || clazz.equals(URI.class) || clazz.equals(URL.class) ||
@ -511,7 +512,7 @@ public abstract class BeanUtils {
* @throws BeansException if the copying failed * @throws BeansException if the copying failed
* @see BeanWrapper * @see BeanWrapper
*/ */
public static void copyProperties(Object source, Object target, Class editable) public static void copyProperties(Object source, Object target, Class<?> editable)
throws BeansException { throws BeansException {
copyProperties(source, target, editable, null); copyProperties(source, target, editable, null);
@ -549,13 +550,13 @@ public abstract class BeanUtils {
* @throws BeansException if the copying failed * @throws BeansException if the copying failed
* @see BeanWrapper * @see BeanWrapper
*/ */
private static void copyProperties(Object source, Object target, Class editable, String[] ignoreProperties) private static void copyProperties(Object source, Object target, Class<?> editable, String[] ignoreProperties)
throws BeansException { throws BeansException {
Assert.notNull(source, "Source must not be null"); Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null"); Assert.notNull(target, "Target must not be null");
Class actualEditable = target.getClass(); Class<?> actualEditable = target.getClass();
if (editable != null) { if (editable != null) {
if (!editable.isInstance(target)) { if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() + throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
@ -564,7 +565,7 @@ public abstract class BeanUtils {
actualEditable = editable; actualEditable = editable;
} }
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
for (PropertyDescriptor targetPd : targetPds) { for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null && if (targetPd.getWriteMethod() != null &&

Loading…
Cancel
Save