日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用反射给JAVABEAN实例赋值

發布時間:2025/4/5 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用反射给JAVABEAN实例赋值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為簡化和統一,需要給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;} }

?

轉載于:https://www.cnblogs.com/davidwang456/p/8891013.html

總結

以上是生活随笔為你收集整理的利用反射给JAVABEAN实例赋值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。