📄 beanutils.java
字号:
package org.conference.pagination;
import java.util.List;
import java.util.Arrays;
import java.beans.PropertyDescriptor;
import org.javawing.component.logging.Log;
import org.javawing.component.logging.LogFactory;
/**
* <p><strong>BeanUtils</strong>,
* A utility class that provides methods that are useful for dealing with
* Java Beans.
*
* @author ZXM
* @package gov.cd.gzcx.pagination
* @since 2.0
*/
public abstract class BeanUtils {
private static final Log logger = LogFactory.getLog(BeanUtils.class);
/**
* Copy the property values of the given source bean into the given target
* bean, ignoring the given ignoreProperties.
*
* @param source the source bean
* @param target the target bean
* @param ignoreProperties array of property names to ignore
* @throws IllegalArgumentException if the classes of source and target do
* not match
* @throws BeansException
*/
public static void copyProperties(Object source, Object target,
String[] ignoreProperties ) throws
IllegalArgumentException, BeansException {
// Assert.notNull(source, "source must not be null");
// Assert.notNull(target, "source must not be null");
List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
PropertyDescriptor[] sourceDescs = getPropertyDescriptors(source.getClass());
try {
for (int i=0;i<sourceDescs.length;i++) {
PropertyDescriptor sourceDesc =sourceDescs[i];
String name = sourceDesc.getName();
if (ignoreProperties == null || (!ignoreList.contains(name))) {
/**
* Create a property descriptor for the named property. If
* the target bean doesn't have the named property, an
* IntrospectionException will be thrown.
*/
PropertyDescriptor targetDesc = getPropertyDescriptor(name, target.getClass());
if (sourceDesc.getReadMethod() != null &&
targetDesc.getWriteMethod() != null &&
targetDesc.getReadMethod() != null) {
/** Get the value from source bean and set it to the target bean. */
Object value = sourceDesc.getReadMethod().invoke(source,(Object[])null);
if (logger.isDebugEnabled()) {
logger.debug("Copying property{name=" + name + ", value=" +
value + "} to target " + target);
}
targetDesc.getWriteMethod().invoke(target, new Object[]{value});
} else {
if (logger.isInfoEnabled()) {
logger.info("Could not copy property{name=" + name + "}");
}
}
} else {
if(logger.isDebugEnabled()){
logger.debug("Ignored property{name=" + name + "}");
}
}
}
}
catch (BeansException ex){
throw ex;
}
catch (Exception ex) {
throw new FatalBeanException(ex);
}
}
/**
* Retrieve the JavaBeans <code>PropertyDescriptor</code> of a given class and property name.
* @param propertyName the name of the property to retrieve the propertyDescriptor for
* @param clazz the Class to retrieve the PropertyDescriptor for
* @return a <code>PropertyDescriptor</code> for the given class and property name.
* @throws BeansException if PropertyDescriptor look fails
*/
public static PropertyDescriptor getPropertyDescriptor(String propertyName,Class clazz) throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getPropertyDescriptor(propertyName);
}
/**
* Retrieve the JavaBeans <code>PropertyDescriptor</code>s of a given class.
* @param clazz the Class to retrieve the PropertyDescriptors for
* @return an array of <code>PropertyDescriptors</code> for the given class
* @throws BeansException if PropertyDescriptor look fails
*/
public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws
BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getBeanInfo().getPropertyDescriptors();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -