java反射转化javabean,map,list(根据前段的传值来给对象赋值)——代码碎片

package com.xinghe.xbx.constant;


/** 
* @ClassName: BeanRefUtil 
* @Description: TODO(自己看代码) 
* @author (唐生肉肉)  
* @date 2018年4月18日 下午6:03:37 
* @version V1.0 
*/
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;


public class RefJavaBeanUtil {


public static Map<String, String> getFieldValueMap(Object bean) {
Class<?> cls = bean.getClass();
Map<String, String> valueMap = new HashMap<String, String>();
Method[] methods = cls.getDeclaredMethods();
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
try {
String fieldType = field.getType().getSimpleName();
String fieldGetName = gainGetName(field.getName());
if (!testGetMet(methods, fieldGetName)) {
continue;
}
Method fieldGetMet = cls.getMethod(fieldGetName, new Class[] {});
Object fieldVal = fieldGetMet.invoke(bean, new Object[] {});
String result = null;
if ("Date".equals(fieldType)) {
result = dfmtDate((Date) fieldVal);
} else {
if (null != fieldVal) {
result = String.valueOf(fieldVal);
}
}
valueMap.put(field.getName(), result);
} catch (Exception e) {
continue;
}
}
return valueMap;
}


public static void setFieldValue(Object bean, Map<String, String> valMap) {
Class<?> cls = bean.getClass();
Method[] methods = cls.getDeclaredMethods();
Field[] fields = cls.getDeclaredFields();


for (Field field : fields) {
try {
String fieldSetName = gainSetName(field.getName());
if (!testSetMet(methods, fieldSetName)) {
continue;
}
Method fieldSetMet = cls.getMethod(fieldSetName, field.getType());
String fieldKeyName = field.getName();
String value = valMap.get(fieldKeyName);
if (null != value && !"".equals(value)) {
String fieldType = field.getType().getSimpleName();
if ("String".equals(fieldType)) {
fieldSetMet.invoke(bean, value);
} else if ("Date".equals(fieldType)) {
Date temp = parseDate(value);
fieldSetMet.invoke(bean, temp);
} else if ("Integer".equals(fieldType) || "int".equals(fieldType)) {
Integer intval = Integer.parseInt(value);
fieldSetMet.invoke(bean, intval);
} else if ("Long".equalsIgnoreCase(fieldType) || "long".equals(fieldType)) {
Long temp = Long.parseLong(value);
fieldSetMet.invoke(bean, temp);
} else if ("Double".equalsIgnoreCase(fieldType) || "double".equals(fieldType)) {
Double temp = Double.parseDouble(value);
fieldSetMet.invoke(bean, temp);
} else if ("float".equalsIgnoreCase(fieldType) || "Float".equals(fieldType)) {
Float temp = Float.parseFloat(value);
fieldSetMet.invoke(bean, temp);
} else if ("Boolean".equalsIgnoreCase(fieldType) || "boolean".equals(fieldType)) {
Boolean temp = Boolean.parseBoolean(value);
fieldSetMet.invoke(bean, temp);
} else {
throw new RuntimeException("你穿的类型不存在");
}
}
} catch (Exception e) {
continue;
}
}
}


public static Date parseDate(String datestr) {
if (null == datestr || "".equals(datestr)) {
return null;
}
try {
String fmtstr = null;
if (datestr.indexOf(':') > 0) {
fmtstr = "yyyy-MM-dd HH:mm:ss";
} else {
fmtstr = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(fmtstr);
return sdf.parse(datestr);
} catch (Exception e) {
return null;
}
}


public static String dfmtDate(Date date) {
if (null == date) {
return null;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
return sdf.format(date);
} catch (Exception e) {
return null;
}
}


public static boolean testSetMet(Method[] methods, String fieldSetMet) {
for (Method met : methods) {
if (fieldSetMet.equals(met.getName())) {
return true;
}
}
return false;
}


public static boolean testGetMet(Method[] methods, String fieldGetMet) {
for (Method met : methods) {
if (fieldGetMet.equals(met.getName())) {
return true;
}
}
return false;
}


public static String gainGetName(String fieldName) {
if (null == fieldName || "".equals(fieldName)) {
return null;
}
int startIndex = 0;
if (fieldName.charAt(0) == '_')
startIndex = 1;
return "get" + fieldName.substring(startIndex, startIndex + 1).toUpperCase()
+ fieldName.substring(startIndex + 1);
}


public static String gainSetName(String fieldName) {
if (null == fieldName || "".equals(fieldName)) {
return null;
}
int startIndex = 0;
if (fieldName.charAt(0) == '_')
startIndex = 1;
return "set" + fieldName.substring(startIndex, startIndex + 1).toUpperCase()
+ fieldName.substring(startIndex + 1);
}






}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值