客户体征相关
This commit is contained in:
@ -0,0 +1,78 @@
|
||||
package com.stdiet.common.utils.bean;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class BeanHelper{
|
||||
/**
|
||||
* 去掉bean中所有属性为字符串的前后空格
|
||||
* @param bean
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void beanAttributeValueTrim(Object bean) throws Exception {
|
||||
if(bean!=null){
|
||||
//获取所有的字段包括public,private,protected,private
|
||||
Field[] fields = bean.getClass().getDeclaredFields();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
Field f = fields[i];
|
||||
if (f.getType().getName().equals("java.lang.String")) {
|
||||
String key = f.getName();//获取字段名
|
||||
Object value = getFieldValue(bean, key);
|
||||
|
||||
if (value == null)
|
||||
continue;
|
||||
|
||||
setFieldValue(bean, key, value.toString().trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用反射通过get方法获取bean中字段fieldName的值
|
||||
* @param bean
|
||||
* @param fieldName
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static Object getFieldValue(Object bean, String fieldName)
|
||||
throws Exception {
|
||||
StringBuffer result = new StringBuffer();
|
||||
String methodName = result.append("get")
|
||||
.append(fieldName.substring(0, 1).toUpperCase())
|
||||
.append(fieldName.substring(1)).toString();
|
||||
|
||||
Object rObject = null;
|
||||
Method method = null;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class[] classArr = new Class[0];
|
||||
method = bean.getClass().getMethod(methodName, classArr);
|
||||
rObject = method.invoke(bean, new Object[0]);
|
||||
|
||||
return rObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用发射调用bean.set方法将value设置到字段
|
||||
* @param bean
|
||||
* @param fieldName
|
||||
* @param value
|
||||
* @throws Exception
|
||||
*/
|
||||
private static void setFieldValue(Object bean, String fieldName, Object value)
|
||||
throws Exception {
|
||||
StringBuffer result = new StringBuffer();
|
||||
String methodName = result.append("set")
|
||||
.append(fieldName.substring(0, 1).toUpperCase())
|
||||
.append(fieldName.substring(1)).toString();
|
||||
|
||||
/**
|
||||
* 利用发射调用bean.set方法将value设置到字段
|
||||
*/
|
||||
Class[] classArr = new Class[1];
|
||||
classArr[0]="java.lang.String".getClass();
|
||||
Method method=bean.getClass().getMethod(methodName,classArr);
|
||||
method.invoke(bean,value);
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.stdiet.common.utils.bean;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 对象相关工具类
|
||||
* </p>
|
||||
*
|
||||
* @author xzj
|
||||
* @since 2020-11-27
|
||||
*/
|
||||
public class ObjectUtils {
|
||||
|
||||
/**
|
||||
* @Author xzj
|
||||
* @Description 利用反射机制将一个对象中的所有属性值赋值给另一个对象
|
||||
* @Date 2020/12/3 18:17
|
||||
* @Param object: 原对象
|
||||
* @Param clazz: 目标对象对应Class
|
||||
* @Return T 目标对象
|
||||
**/
|
||||
public static <T> T getObjectByObject(Object object, Class<T> clazz) throws Exception {
|
||||
return getObjectByObject(object, clazz, new ArrayList<String>(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @Author xzj
|
||||
* @Description 利用反射机制将一个对象中的所有属性值赋值给另一个对象
|
||||
* @Date 2020/12/3 18:17
|
||||
* @Param object: 原对象
|
||||
* @Param clazz: 目标对象对应Class
|
||||
* @Param filterField: 需要过滤掉的属性名称集合
|
||||
* @Return T 目标对象
|
||||
**/
|
||||
public static <T> T getObjectByObject(Object object, Class<T> clazz, List<String> filterField) throws Exception {
|
||||
T t = null;
|
||||
if (clazz != null && object != null) {
|
||||
t = clazz.newInstance();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
//属性名
|
||||
String key = field.getName();
|
||||
//以整数形式返回由此 Field 对象表示的字段的 Java 语言修饰符
|
||||
int modifierValue = field.getModifiers();
|
||||
//PUBLIC: 1 PRIVATE: 2 PROTECTED: 4 只设置4以下修饰词属性,过滤掉filterField中的属性
|
||||
if(modifierValue > 4 || (filterField != null && filterField.contains(key)) ){
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Field objectField = object.getClass().getDeclaredField(key);
|
||||
objectField.setAccessible(true);
|
||||
//返回指定对象obj上此 Field 表示的字段的值
|
||||
Object val = objectField.get(object);
|
||||
//将指定对象变量上此 Field 对象表示的字段设置为指定的新值
|
||||
field.set(t, val);
|
||||
}catch (Exception e){
|
||||
System.out.println(object.getClass().getName() + "没有该属性: " + field.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user