zoukankan      html  css  js  c++  java
  • java-读取javabean类的set方法并设值

        
        /**
         * 新反射实例化模型
         * @param filenamepath
         * @return
         */
        public static Object newIntence(String filenamepath) {
            Object t = null;
            try {
                Class<?> cls = Class.forName(filenamepath);
                t = cls.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return t;
        }
    
        
        @SuppressWarnings("unchecked")
        public static <T> T convertObj(Map<String,Object> map, Class<T> cls) {
            Object obj = ModelUtils.createInstance(cls);
            if( map != null ) {
                convertObj(JSONObject.fromObject(map), obj);
            }
            return (T)obj;
        }
        
    
        public static void convertObj(JSONObject jsonObj, Object obj)
        {
            PropertyDescriptor pds[] = BeanUtils.getPropertyDescriptors(obj.getClass());
            for(int i = 0; i < pds.length; i++)
            {
                PropertyDescriptor pd = pds[i];
                if(jsonObj.containsKey(pd.getName())){
                    Method method = pd.getWriteMethod();
                    String parameterTypeName = (method.getParameterTypes()[0].getName());
                    Object ob = pd.getName();
                    //System.out.println(method.getName()+":"+ob+":"+jsonObj.get(ob));
                    invokeSetMethod(obj, method, converValueType(parameterTypeName, jsonObj.get(ob)));
                }
            }
    
        }
        
    
        public static void invokeSetMethod(Object object, Method method, Object value)
        {
            try
            {
                method.invoke(object, new Object[] {
                    value
                });
            }
            catch(IllegalAccessException ex)
            {
                throw new IllegalArgumentException(ex.getMessage());
            }
            catch(InvocationTargetException ex)
            {
                throw new IllegalArgumentException(ex.getMessage());
            }
            catch(Exception ex)
            {
                throw new IllegalArgumentException(ex.getMessage());
            }
        }
    
        /**
         * 数据根据类型转化成响应的数据
         * @param methodType
         * @param ob
         * @return
         */
        public static Object converValueType(String methodType,Object ob){
            if( ob == null )return null;
            if( StringUtils.isBlank(methodType)) return ob.toString();
            if( "java.math.BigDecimal".equalsIgnoreCase(methodType)){
                try {
                    return new BigDecimal(ob.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else if( "java.lang.String".equalsIgnoreCase(methodType)){
                try {
                    return ob.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }else if( "java.lang.Integer".equalsIgnoreCase(methodType)){
                try {
                    return Integer.parseInt(ob.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else if( "java.util.Date".equalsIgnoreCase(methodType)){
                try {
                    return DateTimeUtils.str2Date(ob.toString(), DateTimeUtils.FORMAT_yyyy_MM_dd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else if( "long".equalsIgnoreCase(methodType)){
                try {
                    return Long.parseLong(ob.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                logger.debug("ConverUtils converValueType is fail .type["+methodType+"],value["+ob+"]");
            }
            return null;
        }
        
        
  • 相关阅读:
    C++ 内存分配(new,operator new)详解
    单例
    实现sizeof
    muduo学习一:简介
    虚函数可以是内联函数吗?
    C++多态实现机制
    [转] Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
    [Android]实现客户端之间的即时通信
    android获取手机通讯录
    java中forName()的作用
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5829562.html
Copyright © 2011-2022 走看看