java 工具类 ---反射字段值封装到对象中

在javaEE 开发中,经常需要将页面传过来的值封装到类的属性中。

以便传值或者持久化到数据库中。

可以使用这个简单封装类。


[java]  view plain copy
  1. package com.letv.uts2.utcServer.util;  
  2.   
  3.   
  4. import org.slf4j.Logger;  
  5. import org.slf4j.LoggerFactory;  
  6.   
  7.   
  8. import java.lang.reflect.Method;  
  9. import java.util.HashMap;  
  10. import java.util.Map;  
  11. import java.util.Set;  
  12.   
  13.   
  14. /** 
  15.  * Created by IntelliJ IDEA. 
  16.  * User: haoshihai 
  17.  * Date: 13-3-14 
  18.  * Time: 下午3:09 
  19.  * To change this template use File | Settings | File Templates. 
  20.  */  
  21. public class WrapperModel {  
  22.     private static final Logger log = LoggerFactory.getLogger(WrapperModel.class);  
  23.     String userName;  
  24.     String password;  
  25.   
  26.   
  27.     public String getUserName() {  
  28.         return userName;  
  29.     }  
  30.   
  31.   
  32.     public void setUserName(String userName) {  
  33.         this.userName = userName;  
  34.     }  
  35.   
  36.   
  37.     public String getPassword() {  
  38.         return password;  
  39.     }  
  40.   
  41.   
  42.     public void setPassword(String password) {  
  43.         this.password = password;  
  44.     }  
  45.   
  46.   
  47.     public  static <T> T doWrapper(Class c, Map<String, Object> map) throws Exception {  
  48.         T t = (T) c.newInstance();  
  49.         try {  
  50.             Set<Map.Entry<String, Object>> set = map.entrySet();  
  51.             for (Map.Entry<String, Object> entry : map.entrySet()) {  
  52.                 String fileName = entry.getKey();  
  53.                 Object value = entry.getValue();  
  54.                 log.info("fileName={},value={}"new Object[]{fileName, value});  
  55.                 Method get_Method = c.getMethod("get" + getMethodName(fileName));  //获取getMethod方法  
  56.                 Method set_Method = c.getMethod("set" + getMethodName(fileName), get_Method.getReturnType());//获得属性get方法  
  57.                 Class<?> clazz = get_Method.getReturnType();  
  58.                 String type = clazz.getName(); //获取返回值名称  
  59.                 if (type.equals("long"))  
  60.                     set_Method.invoke(t, Long.valueOf(value.toString()));  //对于类型 long  
  61.                 else if (type.equals("int") || type.equals("java.lang.Integer"))//对于int 类型  
  62.                     set_Method.invoke(t, Integer.valueOf(value.toString()));  
  63.                 else if ("java.lang.String".equals(type))  
  64.                     set_Method.invoke(t,value);  
  65.                 else set_Method.invoke(t, c.cast(value));//其他类型调用class.cast方法  
  66.             }  
  67.         } catch (Exception e) {  
  68.             log.equals("property is errorr!" + e.toString());  
  69.         }  
  70.         return t;  
  71.     }  
  72.   
  73.   
  74.     // 把一个字符串的第一个字母大写、效率是最高的、  
  75.   
  76.   
  77.     private static String getMethodName(String fildeName) {  
  78.         byte[] items = fildeName.getBytes();  
  79.         items[0] = (byte) ((char) items[0] - 'a' + 'A');  
  80.         return new String(items);  
  81.     }  
  82.   
  83.   
  84.   
  85.   
  86.     public static void main(String args[]) throws Exception {  
  87.         Map map = new HashMap();  
  88.         map.put("userName""jim");  
  89.         map.put("password""tom");  
  90.         WrapperModel w2 = (WrapperModel) WrapperModel.doWrapper(WrapperModel.class, map);  
  91.         System.out.print(w2.getPassword()+"----"+w2.getUserName());  
  92.     }  
  93. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值