All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
gu.sql2java.bean.BeanPropertyUtils Maven / Gradle / Ivy
package gu.sql2java.bean;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;
public class BeanPropertyUtils {
private static final boolean hasReadMethod(PropertyDescriptor propertyDescriptor) {
Method m = propertyDescriptor.getReadMethod();
// 过滤掉Object中的get方法
if (m != null && m.getDeclaringClass() != Object.class) {
return true;
}
return false;
}
private static final boolean hasWriteMethod(PropertyDescriptor propertyDescriptor) {
Method m = propertyDescriptor.getWriteMethod();
// 过滤掉Object中的get方法
if (m != null && m.getDeclaringClass() != Object.class) {
return true;
}
return false;
}
/**
* 获取beanClass中所有具有指定读写类型(rw)的属性
* @param beanClass
* @param rw 属性类型标记
* 0 所有属性
* 1 读属性
* 2 写属性
* 3 读写属性
* @param lenient 是否为宽容模式---允许返回类型不为void的setter方法
* @return 属性名与PropertyDescriptor映射的Map对象
*/
public static final Map getProperties(Class> beanClass, int rw,boolean lenient) {
try {
Map properties = new HashMap();
if (beanClass != null) {
BeanUtilsBean beanUtils = BeanUtilsBean.getInstance();
PropertyUtilsBean propertyUtils = beanUtils.getPropertyUtils();
PropertyDescriptor[] origDescriptors = propertyUtils.getPropertyDescriptors(beanClass);
Boolean put;
for (PropertyDescriptor pd : origDescriptors) {
if(lenient){
pd = LenientDecoratorOfDescriptor.toDecorator(pd);
}
put = false;
switch (rw &= 3) {
case 0:
put = hasReadMethod(pd) || hasWriteMethod(pd);
break;
case 1:
put = hasWriteMethod(pd);
break;
case 2:
put = hasReadMethod(pd);
break;
case 3:
put = hasReadMethod(pd) && hasWriteMethod(pd);
break;
}
if (put) {
properties.put(pd.getName(), pd);
}
}
}
return properties;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获取beanClass中所有具有指定读写类型(rw)的属性
* @param beanClass
* @param rw 属性类型标记
* 0 所有属性
* 1 读属性
* 2 写属性
* 3 读写属性
* @return 属性名与PropertyDescriptor映射的Map对象
*/
public static final Map getProperties(Class> beanClass, int rw) {
return getProperties(beanClass, rw, false);
}
public static final T copy(T from,T to){
if(null==from||null==to)
throw new NullPointerException();
PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
try {
propertyUtils.copyProperties(to, from);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}catch (InvocationTargetException e) {
throw new RuntimeException(e);
}catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
return to;
}
/**
* 从{@code from}复制共有的(可读写字段)字段到{@code to},{@code from}和{@code to}可以是两个互不继承的类型
* 要求两个类型共有的字段类型必须一致,否则抛出异常
* @param from Java Bean
* @param to Java Bean
* @param ignoreNull 为{@code true}忽略为{@code null}的字段
* @param ignoreEmpty 为{@code true}忽略为空的String类型字段或
* {@link Collection},{@link Iterable},{@link Iterator},{@link Map},数组类型字段,
* {@code ignoreNull}为{@code true}时有效
*/
public static final void copyBean(Object from,Object to, boolean ignoreNull, boolean ignoreEmpty){
if(null==from || null == to)
throw new NullPointerException("from or to is null");
Map fromProps = getProperties(from.getClass(),1);
Map toProps = getProperties(to.getClass(),2);
// 共同有的字段集合
HashSet intersection = new HashSet(fromProps.keySet());
intersection.retainAll(toProps.keySet());
PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
try {
// 遍历所有共有字段
for(String fieldName : intersection){
// 从from中读取字段值
Object value = propertyUtils.getProperty(from, fieldName);
if(!ignoreNull || null != value){
if(!ignoreEmpty || !isEmpty(value)){
// 向 to 写入字段字段值
propertyUtils.setProperty(to,fieldName,value);
}
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}catch (InvocationTargetException e) {
throw new RuntimeException(e);
}catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
/**
* 判断输入参数是否为{@code null}或空
* 如果输入参数为@{@link String},{@link Collection},{@link Iterable},{@link Iterator},{@link Map},数组类型则返回其是否为空,
* 否则返回{@code false}
* @param value 为{@code null}返回{@code true}
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object value){
if(null == value){
return true;
}else if(value instanceof String){
return ((String)value).isEmpty();
}else if (value instanceof Collection) {
return ((Collection)value).isEmpty();
}else if (value instanceof Iterator) {
return !((Iterator)value).hasNext();
}else if (value instanceof Iterable) {
return !((Iterable)value).iterator().hasNext();
}else if (value instanceof Map) {
return ((Map)value).isEmpty();
}else if (value.getClass().isArray()) {
return Array.getLength(value)==0;
}
return false;
}
/**
* 判断输入参数是否都为空或有元素为空
* 如果{@code all}为{@code true}判断都为空,即{@code values}中的所有元素都为空时返回{@code true}
* 否则判断有空,即 {@code values}中的任一元素为空时就返回{@code true}
* @param all 为{@code true}要求{@code values} 所有元素为空,为{@code false}则只要{@code values}中元素为空即返回{@code true}
* @param values 待判空元素迭代器对象
* @since 3.25.0
* @see #isEmpty(Object)
*/
public static boolean isEmpty(boolean all,Iterable values){
if(!isEmpty(values)) {
if(all) {
for(Iterator itor=values.iterator();itor.hasNext();) {
if(!isEmpty(itor.next())) {
return false;
}
}
return true;
}else {
for(Iterator itor=values.iterator();itor.hasNext();) {
if(isEmpty(itor.next())) {
return true;
}
}
return false;
}
}
return true;
}
/**
* 判断输入参数是否都为空或有元素为空
* 如果{@code all}为{@code true}判断都为空,即{@code values}中的所有元素都为空时返回{@code true}
* 否则判断有空,即 {@code values}中的任一元素为空时就返回{@code true}
* @param all 为{@code true}要求{@code values} 所有元素为空,为{@code false}则只要{@code values}中元素为空即返回{@code true}
* @param values 待判空元素数组
* @since 3.25.0
* @see #isEmpty(Object)
*/
public static boolean isEmpty(boolean all,Object ...values){
if(!isEmpty(values)) {
return isEmpty(all, Arrays.asList(values));
}
return true;
}
/**
* 判断输入参数是否都为空
* @param values 待判空元素迭代器对象
* @since 3.25.0
* @see #isEmpty(boolean, Iterable)
*/
public static boolean allEmpty(Iterable values){
return isEmpty(true,values);
}
/**
* 判断输入参数是否都为空
* @param values 待判空元素数组
* @since 3.25.0
* @see #isEmpty(boolean, Object...)
*/
public static boolean allEmpty(Object... values){
return isEmpty(true,values);
}
/**
* 判断输入参数是否有为空的元素
* @param values 待判空元素迭代器对象
* @since 3.25.0
* @see #isEmpty(boolean, Iterable)
*/
public static boolean hasEmpty(Iterable values){
return isEmpty(false,values);
}
/**
* 判断输入参数是否有为空的元素
* @param values 待判空元素数组
* @since 3.25.0
* @see #isEmpty(boolean, Object...)
*/
public static boolean hasEmpty(Object... values){
return isEmpty(false,values);
}
}