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;
    }
    
    
原文地址:https://www.cnblogs.com/hwaggLee/p/5829562.html