commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.system.util.bean;
|
||||
|
||||
/**
|
||||
* Bean属性
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class BeanProperty {
|
||||
private String name;
|
||||
private Class<?> type;
|
||||
|
||||
public BeanProperty() {
|
||||
}
|
||||
|
||||
public BeanProperty(String name, Class<?> type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Class<?> getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
300
ndplannew/src/main/java/com/system/util/bean/BeanUtils.java
Normal file
300
ndplannew/src/main/java/com/system/util/bean/BeanUtils.java
Normal file
@@ -0,0 +1,300 @@
|
||||
package com.system.util.bean;
|
||||
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cglib.beans.BeanCopier;
|
||||
import org.springframework.cglib.beans.BeanGenerator;
|
||||
import org.springframework.cglib.beans.BeanMap;
|
||||
|
||||
/**
|
||||
* 基于CGlib
|
||||
* 实体工具类,目前copy不支持map、list
|
||||
*
|
||||
* @author L.cm
|
||||
* email: 596392912@qq.com
|
||||
* site:http://www.dreamlu.net
|
||||
* @date 2015年4月26日下午5:10:42
|
||||
*/
|
||||
public final class BeanUtils extends org.springframework.beans.BeanUtils {
|
||||
private BeanUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 实例化对象
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 对象
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T newInstance(Class<?> clazz) {
|
||||
return (T) instantiate(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实例化对象
|
||||
*
|
||||
* @param clazzStr 类名
|
||||
* @return 对象
|
||||
*/
|
||||
public static <T> T newInstance(String clazzStr) {
|
||||
try {
|
||||
Class<?> clazz = Class.forName(clazzStr);
|
||||
return newInstance(clazz);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Bean的属性
|
||||
*
|
||||
* @param bean bean
|
||||
* @param propertyName 属性名
|
||||
* @return 属性值
|
||||
*/
|
||||
public static Object getProperty(Object bean, String propertyName) {
|
||||
PropertyDescriptor pd = getPropertyDescriptor(bean.getClass(), propertyName);
|
||||
if (pd == null) {
|
||||
throw new RuntimeException("Could not read property '" + propertyName + "' from bean PropertyDescriptor is null");
|
||||
}
|
||||
Method readMethod = pd.getReadMethod();
|
||||
if (readMethod == null) {
|
||||
throw new RuntimeException("Could not read property '" + propertyName + "' from bean readMethod is null");
|
||||
}
|
||||
if (!readMethod.isAccessible()) {
|
||||
readMethod.setAccessible(true);
|
||||
}
|
||||
try {
|
||||
return readMethod.invoke(bean);
|
||||
} catch (Throwable ex) {
|
||||
throw new RuntimeException("Could not read property '" + propertyName + "' from bean", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Bean属性
|
||||
*
|
||||
* @param bean bean
|
||||
* @param propertyName 属性名
|
||||
* @param value 属性值
|
||||
*/
|
||||
public static void setProperty(Object bean, String propertyName, Object value) {
|
||||
PropertyDescriptor pd = getPropertyDescriptor(bean.getClass(), propertyName);
|
||||
if (pd == null) {
|
||||
throw new RuntimeException("Could not set property '" + propertyName + "' to bean PropertyDescriptor is null");
|
||||
}
|
||||
Method writeMethod = pd.getWriteMethod();
|
||||
if (writeMethod == null) {
|
||||
throw new RuntimeException("Could not set property '" + propertyName + "' to bean writeMethod is null");
|
||||
}
|
||||
if (!writeMethod.isAccessible()) {
|
||||
writeMethod.setAccessible(true);
|
||||
}
|
||||
try {
|
||||
writeMethod.invoke(bean, value);
|
||||
} catch (Throwable ex) {
|
||||
throw new RuntimeException("Could not set property '" + propertyName + "' to bean", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给一个Bean添加字段
|
||||
*
|
||||
* @param superBean 父级Bean
|
||||
* @param props 新增属性
|
||||
* @return {Object}
|
||||
*/
|
||||
public static Object generator(Object superBean, BeanProperty... props) {
|
||||
Class<?> superclass = superBean.getClass();
|
||||
Object genBean = generator(superclass, props);
|
||||
BeanUtils.copy(superBean, genBean);
|
||||
return genBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给一个class添加字段
|
||||
*
|
||||
* @param superclass 父级
|
||||
* @param props 新增属性
|
||||
* @return {Object}
|
||||
*/
|
||||
public static Object generator(Class<?> superclass, BeanProperty... props) {
|
||||
BeanGenerator generator = new BeanGenerator();
|
||||
generator.setSuperclass(superclass);
|
||||
generator.setUseCache(true);
|
||||
for (BeanProperty prop : props) {
|
||||
generator.addProperty(prop.getName(), prop.getType());
|
||||
}
|
||||
return generator.create();
|
||||
}
|
||||
|
||||
/**
|
||||
* copy 对象属性到另一个对象,默认不使用Convert
|
||||
*
|
||||
* @param src
|
||||
* @param clazz 类名
|
||||
* @return T
|
||||
*/
|
||||
public static <T> T copy(Object src, Class<T> clazz) {
|
||||
BeanCopier copier = BeanCopier.create(src.getClass(), clazz, false);
|
||||
|
||||
T to = newInstance(clazz);
|
||||
copier.copy(src, to, null);
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拷贝对象
|
||||
*
|
||||
* @param src 源对象
|
||||
* @param dist 需要赋值的对象
|
||||
*/
|
||||
public static void copy(Object src, Object dist) {
|
||||
BeanCopier copier = BeanCopier
|
||||
.create(src.getClass(), dist.getClass(), false);
|
||||
|
||||
copier.copy(src, dist, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象装成map形式
|
||||
*
|
||||
* @param src
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static BeanMap toMap(Object src) {
|
||||
return BeanMap.create(src);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将map 转为 bean
|
||||
*/
|
||||
public static <T> T toBean(Map<String, Object> beanMap, Class<T> valueType) {
|
||||
T bean = BeanUtils.newInstance(valueType);
|
||||
PropertyDescriptor[] beanPds = getPropertyDescriptors(valueType);
|
||||
for (PropertyDescriptor propDescriptor : beanPds) {
|
||||
String propName = propDescriptor.getName();
|
||||
// 过滤class属性
|
||||
if (propName.equals("class")) {
|
||||
continue;
|
||||
}
|
||||
if (beanMap.containsKey(propName)) {
|
||||
Method writeMethod = propDescriptor.getWriteMethod();
|
||||
if (null == writeMethod) {
|
||||
continue;
|
||||
}
|
||||
Object value = beanMap.get(propName);
|
||||
if (!writeMethod.isAccessible()) {
|
||||
writeMethod.setAccessible(true);
|
||||
}
|
||||
try {
|
||||
writeMethod.invoke(bean, value);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException("Could not set property '" + propName + "' to bean", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字段转换成字符串
|
||||
* @param filed
|
||||
* @return
|
||||
*/
|
||||
public static String convertToString(Object filed) {
|
||||
if(filed == null)
|
||||
return null;
|
||||
if(filed instanceof Integer) {
|
||||
return ((Integer) filed).toString();
|
||||
}else if(filed instanceof String) {
|
||||
return (String) filed;
|
||||
}else if(filed instanceof Double) {
|
||||
return ((Double) filed).toString();
|
||||
}else if(filed instanceof Float) {
|
||||
return ((Float) filed).toString();
|
||||
}else if(filed instanceof Long) {
|
||||
return ((Long) filed).toString();
|
||||
}else if(filed instanceof Boolean) {
|
||||
return ((Boolean) filed).toString();
|
||||
}else if(filed instanceof Date) {
|
||||
Date d = (Date) filed;
|
||||
return d.toString();
|
||||
}else {
|
||||
return filed.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将String转换成对象
|
||||
* @param filed
|
||||
* @return
|
||||
*/
|
||||
public static Object string2Object(String value, Class clazz) {
|
||||
if(clazz == Integer.class) {
|
||||
|
||||
return Integer.parseInt(value);
|
||||
|
||||
}else if(clazz == Float.class) {
|
||||
|
||||
return Float.parseFloat(value);
|
||||
|
||||
}else if(clazz == Double.class) {
|
||||
|
||||
return Double.parseDouble(value);
|
||||
|
||||
}else if(clazz == Integer.class) {
|
||||
|
||||
return Integer.parseInt(value);
|
||||
|
||||
}else if(clazz == Boolean.class) {
|
||||
|
||||
return Boolean.parseBoolean(value);
|
||||
|
||||
}else if(clazz == Short.class) {
|
||||
|
||||
return Short.parseShort(value);
|
||||
|
||||
}else {
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String toString(Object bean, String prefix, String suffix) {
|
||||
String str ="";
|
||||
if(bean == null) return str;
|
||||
Map<String, Object> map = BeanUtils.toMap(bean);
|
||||
for (String key: map.keySet()) {
|
||||
str += prefix + BeanUtils.convertToString(map.get(key))+ suffix;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
public static String toString(Map<String,String> bean, String prefix, String suffix) {
|
||||
String str ="";
|
||||
if(bean == null) return str;
|
||||
|
||||
for (String key: bean.keySet()) {
|
||||
str += prefix + bean.get(key)+ suffix;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static String merge( Object field, String prefix, String suffix) {
|
||||
String str ="";
|
||||
if(field == null) return str;
|
||||
str += prefix + BeanUtils.convertToString(field)+ suffix;
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.system.util.bean;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target( { java.lang.annotation.ElementType.FIELD })
|
||||
public @interface CopyAttribute {
|
||||
|
||||
}
|
||||
252
ndplannew/src/main/java/com/system/util/bean/CopyUtil.java
Normal file
252
ndplannew/src/main/java/com/system/util/bean/CopyUtil.java
Normal file
@@ -0,0 +1,252 @@
|
||||
package com.system.util.bean;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.system.util.AnnotationUtil;
|
||||
|
||||
public class CopyUtil {
|
||||
|
||||
/**
|
||||
* 获取一个对象的属性值为空的属性集合
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static Set<String> getNullPropertyNames (Object source) {
|
||||
final BeanWrapper src = new BeanWrapperImpl(source);
|
||||
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
|
||||
|
||||
Set<String> emptyNames = new HashSet<String>();
|
||||
for(java.beans.PropertyDescriptor pd : pds) {
|
||||
Object srcValue = src.getPropertyValue(pd.getName());
|
||||
if (srcValue == null) emptyNames.add(pd.getName());
|
||||
}
|
||||
String[] result = new String[emptyNames.size()];
|
||||
return emptyNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将object中只用了CopyAttribute注解的属性复制到target,
|
||||
* 如果target为空,创建一个新对象
|
||||
* @param Object
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
//@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T copyObject(T object, T target) {
|
||||
return copyObject(object,target, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 1.如果target为空,创建一个新对象
|
||||
* 2.将object中只用了CopyAttribute注解的属性复制到target
|
||||
* 3.默认是【非空复制】, 属性的值为null,就不复制,除非使用了CopyAttribute
|
||||
* 4.noIgnoreProperties 不可忽略的字段,null也要替换
|
||||
* @param Object ,如果target为空,创建一个新对象
|
||||
* @param target
|
||||
* @param noIgnoreProperties 不可忽略的字段,null也要替换
|
||||
* @return
|
||||
*/
|
||||
//@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T copyObject(T object, T target, Set<String> noIgnoreProperties) {
|
||||
if(object == null ) {
|
||||
return target;
|
||||
}
|
||||
if (target == null) {
|
||||
try {
|
||||
target = (T) object.getClass().newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 调用spring的复制代码,解决habernateDAO的问题
|
||||
try {
|
||||
|
||||
Set<String> ignoreProperties = new HashSet<>();
|
||||
|
||||
Field[] allFieldList = object.getClass().getDeclaredFields();
|
||||
List<Field> fieldList = AnnotationUtil.getAnnotationFileds(object.getClass(), CopyAttribute.class);
|
||||
// 有CopyAttribute注解的字段
|
||||
Set<String> fieldSet = new HashSet<>();
|
||||
|
||||
// 值为空的字段,获取对象中的空值字段,然后忽略掉字段
|
||||
Set<String> nullSet = getNullPropertyNames(object);
|
||||
if(!CollectionUtils.isEmpty(fieldList)) {
|
||||
for (int i = 0; i < allFieldList.length; i++) {
|
||||
//忽略所有字段
|
||||
ignoreProperties.add(allFieldList[i].getName());
|
||||
}
|
||||
}
|
||||
|
||||
if(!CollectionUtils.isEmpty(fieldList)) {
|
||||
for (int i = 0; i < fieldList.size(); i++) {
|
||||
fieldSet.add(fieldList.get(i).getName());
|
||||
}
|
||||
}
|
||||
|
||||
ignoreProperties.remove(fieldSet);
|
||||
nullSet.remove(fieldSet);
|
||||
ignoreProperties.addAll(nullSet);
|
||||
|
||||
// 指定的字段要进行复制
|
||||
if( ignoreProperties != null && ignoreProperties.size() > 0
|
||||
&& noIgnoreProperties != null && noIgnoreProperties.size() > 0 ) {
|
||||
ignoreProperties.removeAll(noIgnoreProperties);
|
||||
}
|
||||
|
||||
String[] array = new String[ignoreProperties.size()];
|
||||
int i = 0;
|
||||
for (String e : ignoreProperties) {
|
||||
array[i++] = e;
|
||||
}
|
||||
|
||||
BeanUtils.copyProperties(object, target,array);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 1.如果target为空,创建一个新对象
|
||||
* 2.将object中只用了CopyAttribute注解的属性复制到target
|
||||
* 3.默认是【全复制】, 属性的值为null,也要复制,除非字段是NoCopyAttribute
|
||||
* 4.noIgnoreProperties 不可忽略的字段,null也要替换
|
||||
* @param Object ,如果target为空,创建一个新对象
|
||||
* @param target
|
||||
* @param noIgnoreProperties 不可忽略的字段,null也要替换
|
||||
* @return
|
||||
*/
|
||||
//@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T copyAll(T object, T target, Set<String> noIgnoreProperties) {
|
||||
if(object == null ) {
|
||||
return target;
|
||||
}
|
||||
if (target == null) {
|
||||
try {
|
||||
target = (T) object.getClass().newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
List<Field> fieldList = AnnotationUtil.getAnnotationFileds(object.getClass(), NoCopyAttribute.class);
|
||||
|
||||
String[] array = new String[fieldList.size()];
|
||||
for (int i = 0; i < fieldList.size(); i++) {
|
||||
array[i] = fieldList.get(i).getName();
|
||||
}
|
||||
|
||||
BeanUtils.copyProperties(object, target,array);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> List<T> copyObjects(List<T> objects, List<T> targets) {
|
||||
if(objects == null || objects.size() == 0 ) {
|
||||
if(targets == null || targets.size() == 0 ) {
|
||||
return new ArrayList<T>();
|
||||
}else {
|
||||
return targets;
|
||||
}
|
||||
}
|
||||
if(targets == null || targets.size() == 0) {
|
||||
targets = new ArrayList<T>();
|
||||
}
|
||||
for (int i = 0; i < objects.size(); i++) {
|
||||
T item = objects.get(i);
|
||||
if( i < targets.size()) {
|
||||
targets.set(i, copyObject(item, targets.get(i)) );
|
||||
}else {
|
||||
targets.add(copyObject(item, null));
|
||||
}
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> List<T> copyObjects(List<T> objects, List<T> targets, String fieldName) {
|
||||
|
||||
|
||||
if(objects == null || objects.size() == 0 ) {
|
||||
if(targets == null || targets.size() == 0 ) {
|
||||
return new ArrayList<T>();
|
||||
}else {
|
||||
return targets;
|
||||
}
|
||||
}
|
||||
if(targets == null || targets.size() == 0) {
|
||||
targets = new ArrayList<T>();
|
||||
}
|
||||
|
||||
Field field= AnnotationUtil.getField(objects.get(0).getClass(), fieldName);
|
||||
if(field == null) {
|
||||
|
||||
}
|
||||
for (int i = 0; i < objects.size(); i++) {
|
||||
T item = objects.get(i);
|
||||
|
||||
for (int j = 0; j < targets.size(); i++) {
|
||||
T target = targets.get(j);
|
||||
|
||||
}
|
||||
|
||||
if( i < targets.size()) {
|
||||
targets.set(i, copyObject(item, targets.get(i)) );
|
||||
}else {
|
||||
targets.add(copyObject(item, null));
|
||||
}
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*CommonObject object = new CommonObject();
|
||||
object.setId(1L);
|
||||
object.setName("1");
|
||||
|
||||
CommonObject target = new CommonObject();
|
||||
target.setId(2L);
|
||||
target.setName("2");
|
||||
target = CopyUtil.copyObject(object, target);
|
||||
System.out.println(target.getId());
|
||||
System.out.println(target.getName());*/
|
||||
|
||||
|
||||
|
||||
/*List<CommonObject> objects = new ArrayList<CommonObject>();
|
||||
objects.add(object);
|
||||
System.out.println(copyObjects(objects, null));*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.system.util.bean;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target( { java.lang.annotation.ElementType.FIELD })
|
||||
public @interface NoCopyAttribute {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user