首页 技术 正文
技术 2022年11月12日
0 收藏 471 点赞 4,713 浏览 3269 个字

为简化和统一,需要给javabean实例统一赋值,实现代码如下(已测试)

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;import com.xxx.entity.Call;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class ReflectUtils { @SuppressWarnings("rawtypes")
public static Map<String,Class> getPoJoFiled(Class cls){
Map<String,Class> names=new HashMap<>();
Field[] fileds=cls.getDeclaredFields();
for(Field filed:fileds){
names.put(filed.getName(), filed.getType());
}
return names;
} @SuppressWarnings("rawtypes")
public static List<String> getSetMethodName(Class cls){
List<String> methodNames=new ArrayList<>();
Method[] methods =cls.getDeclaredMethods();
for(Method method:methods){
if(method.getName().startsWith("set")){
methodNames.add(method.getName());
}
}
return methodNames;
}
private static String getMethodNameByField(List<String> methodNames, String filedName) {
for (String method : methodNames) {
if (method.toLowerCase().equalsIgnoreCase("set" + filedName)) {
return method;
}
}
return "";
}
    @SuppressWarnings({ "unchecked", "rawtypes" })
public static void initInstance(Object instance,String param) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
Class cls=instance.getClass();
Map<String,Class> fieldNames=ReflectUtils.getPoJoFiled(cls);
List<String> methodNames=ReflectUtils.getSetMethodName(cls);
for(Entry<String,Class> field:fieldNames.entrySet()){
String filedName=field.getKey();
String methodName=getMethodNameByField(methodNames,filedName);
Method setMethodName = cls.getMethod(methodName, fieldNames.get(filedName));
String type=fieldNames.get(filedName).getName();
if(type.equals("java.lang.String")) {
setMethodName.invoke(instance, new Object[] {param});
}
else if(type.equals("int") || type.equals("java.lang.Integer")) {
setMethodName.invoke(instance, new Object[] {new Integer(param)});
}
else if(type.equals("long") || type.equals("java.lang.Long")) {
setMethodName.invoke(instance, new Object[] {new Long(param)});
}else if (type.equals("float") || type.equals("java.lang.Float")) {
            setMethodName.invoke(instance, new Object[]{new Float(param)});

            } else if(type.equals(“boolean”) || type.equals(“java.lang.Boolean”)) {

                    setMethodName.invoke(instance, new Object[] { Boolean.valueOf(param) });
}
else if(type.equals("java.util.Date")) {
Date date = DateUtil.parseDateTime(param);
if(date!=null)
setMethodName.invoke(instance, new Object[] {date});
}
}
} public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
String param="20";
Call call=new Call();
initInstance(call,param);
System.out.println(call.toString());
}

需要用到的公共类:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateUtil {
/**
* 得到当前的时间,自定义时间格式 y 年 M 月 d 日 H 时 m 分 s 秒
* @param dateFormat 输出显示的时间格式
* @return
*/
public final static String defaultFormat = "yyyy-MM-dd HH:mm:ss"; public static String getCurrentDate(String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(new Date());
} public static Date parseDateTime(String date){
SimpleDateFormat formatter = new SimpleDateFormat(defaultFormat);
Date newDate = null;
try {
newDate = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return newDate;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845