1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.beans;
  17. import java.beans.PropertyDescriptor;
  18. import java.lang.reflect.Constructor;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. /**
  23. * Static convenience methods for JavaBeans, for instantiating beans,
  24. * copying bean properties, etc.
  25. *
  26. * <p>Mainly for use within the framework, but to some degree also
  27. * useful for application classes.
  28. *
  29. * @author Rod Johnson
  30. * @author Juergen Hoeller
  31. * @version $Id: BeanUtils.java,v 1.15 2004/03/18 02:46:12 trisberg Exp $
  32. */
  33. public abstract class BeanUtils {
  34. /**
  35. * Convenience method to instantiate a class using its no-arg constructor.
  36. * As this method doesn't try to load classes by name, it should avoid class-loading issues.
  37. * @param clazz class to instantiate
  38. * @return the new instance
  39. */
  40. public static Object instantiateClass(Class clazz) throws BeansException {
  41. try {
  42. return clazz.newInstance();
  43. }
  44. catch (InstantiationException ex) {
  45. throw new FatalBeanException("Could not instantiate class [" + clazz.getName() +
  46. "]; Is it an interface or an abstract class? Does it have a no-arg constructor?", ex);
  47. }
  48. catch (IllegalAccessException ex) {
  49. throw new FatalBeanException("Could not instantiate class [" + clazz.getName() +
  50. "]; has class definition changed? Is there a public no-arg constructor?", ex);
  51. }
  52. }
  53. /**
  54. * Convenience method to instantiate a class using the given constructor.
  55. * As this method doesn't try to load classes by name, it should avoid class-loading issues.
  56. * @param constructor constructor to instantiate
  57. * @return the new instance
  58. */
  59. public static Object instantiateClass(Constructor constructor, Object[] arguments) throws BeansException {
  60. try {
  61. return constructor.newInstance(arguments);
  62. }
  63. catch (IllegalArgumentException ex) {
  64. throw new FatalBeanException("Illegal arguments when trying to instantiate constructor: " + constructor, ex);
  65. }
  66. catch (InstantiationException ex) {
  67. throw new FatalBeanException("Could not instantiate class [" + constructor.getDeclaringClass().getName() +
  68. "]; is it an interface or an abstract class?", ex);
  69. }
  70. catch (IllegalAccessException ex) {
  71. throw new FatalBeanException("Could not instantiate class [" + constructor.getDeclaringClass().getName() +
  72. "]; has class definition changed? Is there a public constructor?", ex);
  73. }
  74. catch (InvocationTargetException ex) {
  75. throw new FatalBeanException("Could not instantiate class [" + constructor.getDeclaringClass().getName() +
  76. "]; constructor threw exception", ex.getTargetException());
  77. }
  78. }
  79. /**
  80. * Determine if the given type is assignable from the given value,
  81. * assuming setting by reflection. Considers primitive wrapper classes
  82. * as assignable to the corresponding primitive types.
  83. * <p>For example used in a bean factory's constructor resolution.
  84. * @param type the target type
  85. * @param value the value that should be assigned to the type
  86. * @return if the type is assignable from the value
  87. */
  88. public static boolean isAssignable(Class type, Object value) {
  89. return (type.isInstance(value) ||
  90. (!type.isPrimitive() && value == null) ||
  91. (type.equals(boolean.class) && value instanceof Boolean) ||
  92. (type.equals(byte.class) && value instanceof Byte) ||
  93. (type.equals(char.class) && value instanceof Character) ||
  94. (type.equals(short.class) && value instanceof Short) ||
  95. (type.equals(int.class) && value instanceof Integer) ||
  96. (type.equals(long.class) && value instanceof Long) ||
  97. (type.equals(float.class) && value instanceof Float) ||
  98. (type.equals(double.class) && value instanceof Double));
  99. }
  100. /**
  101. * Check if the given class represents a "simple" property,
  102. * i.e. a primitive, a String, a Class, or a corresponding array.
  103. * Used to determine properties to check for a "simple" dependency-check.
  104. * @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE
  105. * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#dependencyCheck
  106. */
  107. public static boolean isSimpleProperty(Class clazz) {
  108. return clazz.isPrimitive() || isPrimitiveArray(clazz) || isPrimitiveWrapperArray(clazz) ||
  109. clazz.equals(String.class) || clazz.equals(String[].class) ||
  110. clazz.equals(Class.class) || clazz.equals(Class[].class);
  111. }
  112. /**
  113. * Check if the given class represents a primitive array,
  114. * i.e. boolean, byte, char, short, int, long, float, or double.
  115. */
  116. public static boolean isPrimitiveArray(Class clazz) {
  117. return boolean[].class.equals(clazz) || byte[].class.equals(clazz) || char[].class.equals(clazz) ||
  118. short[].class.equals(clazz) || int[].class.equals(clazz) || long[].class.equals(clazz) ||
  119. float[].class.equals(clazz) || double[].class.equals(clazz);
  120. }
  121. /**
  122. * Check if the given class represents an array of primitive wrappers,
  123. * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
  124. */
  125. public static boolean isPrimitiveWrapperArray(Class clazz) {
  126. return Boolean[].class.equals(clazz) || Byte[].class.equals(clazz) || Character[].class.equals(clazz) ||
  127. Short[].class.equals(clazz) || Integer[].class.equals(clazz) || Long[].class.equals(clazz) ||
  128. Float[].class.equals(clazz) || Double[].class.equals(clazz);
  129. }
  130. /**
  131. * Copy the property values of the given source bean into the target bean.
  132. * @param source source bean
  133. * @param target target bean
  134. * @throws IllegalArgumentException if the classes of source and target do not match
  135. */
  136. public static void copyProperties(Object source, Object target)
  137. throws IllegalArgumentException, BeansException {
  138. copyProperties(source, target, null);
  139. }
  140. /**
  141. * Copy the property values of the given source bean into the given target bean,
  142. * ignoring the given ignoreProperties.
  143. * @param source source bean
  144. * @param target target bean
  145. * @param ignoreProperties array of property names to ignore
  146. * @throws IllegalArgumentException if the classes of source and target do not match
  147. */
  148. public static void copyProperties(Object source, Object target, String[] ignoreProperties)
  149. throws IllegalArgumentException, BeansException {
  150. if (source == null || target == null || !source.getClass().isInstance(target)) {
  151. throw new IllegalArgumentException("Target must an instance of source");
  152. }
  153. List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
  154. BeanWrapper sourceBw = new BeanWrapperImpl(source);
  155. BeanWrapper targetBw = new BeanWrapperImpl(target);
  156. MutablePropertyValues values = new MutablePropertyValues();
  157. for (int i = 0; i < sourceBw.getPropertyDescriptors().length; i++) {
  158. PropertyDescriptor sourceDesc = sourceBw.getPropertyDescriptors()[i];
  159. String name = sourceDesc.getName();
  160. PropertyDescriptor targetDesc = targetBw.getPropertyDescriptor(name);
  161. if (targetDesc.getWriteMethod() != null && targetDesc.getReadMethod() != null &&
  162. (ignoreProperties == null || (!ignoreList.contains(name)))) {
  163. values.addPropertyValue(new PropertyValue(name, sourceBw.getPropertyValue(name)));
  164. }
  165. }
  166. targetBw.setPropertyValues(values);
  167. }
  168. }