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;
        }
        
        
  • 相关阅读:
    在Repeater中动态添加服务器端(按钮)控件
    两种自定义表单设计方案 [转贴]
    如何解决在.Net中用Sql语句向SqlServer数据库中插入特殊字符失败的问题?
    Visual Studio 2005 IDE 技巧和窍门
    C#的Windows编程中多语言的实现
    NET设计模式(18):迭代器模式(Iterator Pattern)(转载)
    “/”应用程序中的服务器错误。当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files”的写访问权限。
    union的妙用
    数据类型—C++基础篇
    C++,VC++,MCF和SLT的关系
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5829562.html
Copyright © 2011-2022 走看看