`
rq2_79
  • 浏览: 235041 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Bean生成方法类

    博客分类:
  • java
阅读更多
java 代码
 
  1. import java.beans.BeanInfo;  
  2. import java.beans.IntrospectionException;  
  3. import java.beans.Introspector;  
  4. import java.beans.PropertyDescriptor;  
  5. import java.lang.reflect.Method;  
  6. import java.util.Date;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.log4j.Logger;  
  11.   
  12. import com.hintsoft.chain.adminmng.vo.Loguser;  
  13.   
  14. /** 
  15.  *  
  16.  * Bean生成方法类 
  17.  *  
  18.  * @author rq2. 
  19.  * @version 1.0.0, 2007-09-14 
  20.  * @see 
  21.  * @since 
  22.  *  
  23.  */  
  24. public class BeanClass {  
  25.   
  26.     private static Logger logger = Logger  
  27.             .getLogger("BeanClass");  
  28.   
  29.     /** 
  30.      * Map转换成Bean 
  31.      *  
  32.      * @param formName 
  33.      *            bean的文件名称 
  34.      * @param map 
  35.      * @return 
  36.      */  
  37.     public static Object map2class(String formName, Map map) {  
  38.         Object bean = null;  
  39.         try {  
  40.             Class classType = Class.forName(formName);  
  41.             bean = classType.newInstance();  
  42.             PropertyDescriptor props[] = propertyDescriptors(classType);  
  43.             for (int i = 0; i < props.length; i++) {  
  44.                 Object object = map.get(props[i].getName());  
  45.                 if (object != null) {  
  46.                     Method setter = props[i].getWriteMethod();  
  47.                     object = processType(object, props[i].getPropertyType());  
  48.                     setter.invoke(bean, new Object[] { object });  
  49.                 }  
  50.             }  
  51.         } catch (Exception e) {  
  52.             logger.error("map2class:", e);  
  53.         }  
  54.         return bean;  
  55.     }  
  56.   
  57.     /** 
  58.      * 类型匹配 
  59.      * @param object 
  60.      * @param propType 
  61.      * @return 
  62.      */  
  63.     private static Object processType(Object object, Class propType) {  
  64.         Object reobject = null;  
  65.         if (propType.equals(java.lang.String.class)) {  
  66.             reobject = (String) object;  
  67.         } else if (propType.equals(Integer.TYPE)  
  68.                 || propType.equals(java.lang.Integer.class)) {  
  69.             reobject = new Integer((String) object);  
  70.         } else if (propType.equals(Boolean.TYPE)  
  71.                 || propType.equals(java.lang.Boolean.class)) {  
  72.             reobject = new Boolean((String) object);  
  73.         } else if (propType.equals(Long.TYPE)  
  74.                 || propType.equals(java.lang.Long.class)) {  
  75.             reobject = new Long((String) object);  
  76.         } else if (propType.equals(Double.TYPE)  
  77.                 || propType.equals(java.lang.Double.class)) {  
  78.             reobject = new Double((String) object);  
  79.         } else if (propType.equals(Float.TYPE)  
  80.                 || propType.equals(java.lang.Float.class)) {  
  81.             reobject = new Float((String) object);  
  82.         } else if (propType.equals(Short.TYPE)  
  83.                 || propType.equals(java.lang.Short.class)) {  
  84.             reobject = new Short((String) object);  
  85.         } else if (propType.equals(Byte.TYPE)  
  86.                 || propType.equals(java.lang.Byte.class)) {  
  87.             reobject = new Byte((String) object);  
  88.         } else if (propType.equals(java.util.Date.class)) {  
  89.             reobject = processDate(object);  
  90.         }  
  91.         return reobject;  
  92.     }  
  93.   
  94.     /** 
  95.      * 日期类型匹配 
  96.      * @param object 
  97.      * @return 
  98.      */  
  99.     private static Date processDate(Object object) {  
  100.   
  101.         if (object != null) {  
  102.             String dateStr = (String) object;  
  103.             dateStr = dateStr.replaceAll("-""");  
  104.             dateStr = dateStr.replaceAll(":""");  
  105.             dateStr = dateStr.trim();  
  106.             int year = 1900;  
  107.             int month = 1;  
  108.             int date = 1;  
  109.             int hrs = 0;  
  110.             int min = 0;  
  111.             int sec = 0;  
  112.             if (dateStr.length() >= 4) {  
  113.                 year = Integer.parseInt(dateStr.substring(04));  
  114.             }  
  115.             if (dateStr.length() >= 6) {  
  116.                 month = Integer.parseInt(dateStr.substring(46));  
  117.             }  
  118.             if (dateStr.length() >= 8) {  
  119.                 date = Integer.parseInt(dateStr.substring(68));  
  120.             }  
  121.             if (dateStr.length() >= 10) {  
  122.                 hrs = Integer.parseInt(dateStr.substring(810));  
  123.             }  
  124.             if (dateStr.length() >= 12) {  
  125.                 min = Integer.parseInt(dateStr.substring(1012));  
  126.             }  
  127.             if (dateStr.length() >= 14) {  
  128.                 sec = Integer.parseInt(dateStr.substring(1214));  
  129.             }  
  130.             return new java.util.Date(year, month - 1, date, hrs, min, sec);  
  131.         } else {  
  132.             return null;  
  133.         }  
  134.   
  135.     }  
  136.   
  137.     private static PropertyDescriptor[] propertyDescriptors(Class c) {  
  138.         BeanInfo beanInfo = null;  
  139.         try {  
  140.             beanInfo = Introspector.getBeanInfo(c);  
  141.         } catch (IntrospectionException e) {  
  142.             logger.error("PropertyDescriptor:",e);  
  143.         }  
  144.         return beanInfo.getPropertyDescriptors();  
  145.     }  
  146.   
  147.     public static void main(String[] arg) {  
  148.   
  149.         Map map = new HashMap();  
  150.         map.put("email""email");  
  151.         map.put("endtm""2007-01-11");  
  152.         map.put("password""23232");  
  153.         map.put("roleid""1");  
  154.         map.put("status""3");  
  155.         Loguser user = (Loguser) map2class(  
  156.                 "Loguser", map);  
  157.         System.out.println(user.getPassword());  
  158.         System.out.println(user.getEmail());  
  159.         System.out.println(user.getStatus());  
  160.         System.out.println(user.getEndtm());  
  161.     }  
  162. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics