zoukankan      html  css  js  c++  java
  • Web 使用反射获得一个对象的所有get方法

    问题描述:

    由于想知道request中包含哪些getter方法,就想通过反射进行遍历,然后输出,结果异常,异常信息:

    问题代码:

            try {
                
                outGetter(request);
                
            } catch (IntrospectionException e) {
                
                e.printStackTrace();
         }
    
    
    public void outGetter(Object obj) throws IntrospectionException
        {
            Class<?> clazz = obj.getClass();
            //获得所有的属性
            Field[] fields = clazz.getDeclaredFields();
            
            for(Field field:fields)
            {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method method = pd.getReadMethod();
                System.out.println(method);
            }
    }

    问题分析:

    IntrospectionException: 在 Introspection 期间发生异常时抛出异常。
      典型的 cause 包括:无法将字符串类名称映射到 Class 对象、无法解析字符串方法名,或者指定对其用途而言具有错误类型签名的方法名称。
    而Method not found:isRequest则表示isRequest方法找不到,分析遍历代码,可知:   
    public PropertyDescriptor(String propertyName, Class<?> beanClass) throws IntrospectionException   
      通过调用 getFoo 和 setFoo 存取方法,为符合标准 Java 约定的属性构造一个 PropertyDescriptor。
      因此如果参数名为
    "fred",则假定 writer 方法为 "setFred",reader 方法为 "getFred"(对于 boolean 属性则为 "isFred")。
      注意,属性名应该以小写字母开头,而方法名称中的首写字母将是大写的。   参数:propertyName
    - 属性的编程名称。 beanClass - 目标 bean 的 Class 对象。例如 sun.beans.OurButton.class。   抛出:IntrospectionException - 如果在内省期间发生异常。

    得到结论:通过反射输出对象的getter方法,前提是对象中的属性和getter一一对应
    //通过实体类测试一下
    public class User { private String username; private String pwd;

    public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } } try { User user = new User(); outGetter(user); } catch (IntrospectionException e) { e.printStackTrace;   ·  }
    结果输出:

    问题解决:

    原因找到了,那么回到最初的问题上来,如何获得对象的所有get方法?
        public void outGetter(Object obj) throws IntrospectionException
        {
            Class<?> clazz = obj.getClass();
            
            //返回此class对象所表示的类的所有public方法
            Method[] methods = clazz.getMethods();
            
            for(Method m:methods)
            {
                  System.out.println(string);
            }
        
        }
    输出结果:

    既然得到了所有的方法,那么能不能对这些方法进行筛选呢?

    m.toString().contains("get"):判断方法中是否含有get,如果有,输出
    m.toString().substring(m.toString().lastIndexOf(".")+1):将符合条件的方法名进行处理,只截取最后的方法名
        public void outGetter(Object obj) throws IntrospectionException
        {
            Class<?> clazz = obj.getClass();
            
            //返回此class对象所表示的类的所有public方法
            Method[] methods = clazz.getMethods();
            
            for(Method m:methods)
            {
                //需要进一步筛选
                //截取最后的方法名 
                
                if(m.toString().contains("get"))
                {
                    String string = m.toString().substring(m.toString().lastIndexOf(".")+1);
                    System.out.println(string);
                }
                    
            }
        
        }

    问题总结:

    PropertyDescriptor类:
    PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:
          1.  getReadMethod(),获得用于读取属性值的方法
          2.  getWriteMethod(),获得用于写入属性值的方法
    通过反射输出对象的getter方法,前提是对象中的属性和getter一一对应
  • 相关阅读:
    [kuangbin带你飞]专题十二 基础DP1 E
    hdu 1203 I NEED A OFFER! (01背包)
    hdu 2602 Bone Collector (01背包)
    hdu 4513 吉哥系列故事——完美队形II (manacher)
    hdu 2203 亲和串 (KMP)
    hdu 1686 Oulipo (KMP)
    hdu 1251 统计难题 (字典树)
    hdu 2846 Repository (字典树)
    hdu 1711 Number Sequence (KMP)
    poj 3461 Oulipo(KMP)
  • 原文地址:https://www.cnblogs.com/roxy/p/7464677.html
Copyright © 2011-2022 走看看