分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

Web 使用反射获得一个对象的所有get方法:

发布时间:2023-09-06 01:08责任编辑:傅花花关键词:Web

问题描述:

由于想知道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一一对应

Web 使用反射获得一个对象的所有get方法:

原文地址:http://www.cnblogs.com/roxy/p/7464677.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved