`

java内省机制

阅读更多

今天在使用工具开发web应用的时候,发现java内省机制中有一个问题。就是当你设置属性名称开头两个字母都为大写时候。getXXname()找不到。

注意 1、开头两个字母不要同时大写。 

 

内省是  Java   语言对 Bean 类属性、事件的一种缺省处理方法。例如类 A 中有属性 name, 那我们可以通过 getName,setName 来得到其值或者设置新的值。通过 getName/setName 来访问 name 属性,这就是默认的规则。  Java   中提供了一套 API 用来访问某个属性的 getter/setter 方法,通过这些 API 可以使你不需要了解这个规则(但你最好还是要搞清楚),这些 API 存放于包 java.beans 中。

  一般的做法是通过类 Introspector 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法。下面我们来看一个例子,这个例子把某个对象的所有属性名称和值都打印出来:

  Java代码

  package  MyTest;

  public   class  bean {

  private  String id =  null  ;

  private  String name =  null  ;

  public  String getId() {

  return  id;

  }

  public   void  setId(String id) {

  this .id = id;

  }

  public  String getName() {

  return  name;

  }

  public   void  setName(String name) {

  this .name = name;

  }

  }

  package  MyTest;

  import  java.beans.BeanInfo;

  import  java.beans.EventSetDescriptor;

  import  java.beans.Introspector;

  import  java.beans.MethodDescriptor;

  import  java.beans.PropertyDescriptor;

  import  java.lang.reflect.Method;

  public   class  myBeanIntrospector {

  public  myBeanIntrospector()

  {

  try

  {

  //实例化一个Bean

  bean beanObj = new  bean();

  //依据Bean产生一个相关的BeanInfo类

  BeanInfo bInfoObject =

  Introspector.getBeanInfo(beanObj.getClass(),beanObj.getClass().getSuperclass());

  //定义一个用于显示的字符串

  String output = "" ;

  //开始自省

  /*

  * BeanInfo.getMethodDescriptors()

  * 用于获取该Bean中的所有允许公开的成员方法,以MethodDescriptor数组的形式返回

  *

  * MethodDescriptor类

  * 用于记载一个成员方法的所有信息

  * MethodDescriptor.getName()

  * 获得该方法的方法名字

  * MethodDescriptor.getMethod()

  * 获得该方法的方法对象(Method类)

  *

  * Method类

  * 记载一个具体的的方法的所有信息

  * Method.getParameterTypes()

  * 获得该方法所用到的所有参数,以Class数组的形式返回

  *

  * Class..getName()

  * 获得该类型的名字

  */

  output = "内省成员方法:\n" ;

  MethodDescriptor[] mDescArray = bInfoObject.getMethodDescriptors();

  for  ( int  i= 0 ;i<mDescArray.length ;i++ )

  {

  //获得一个成员方法描述器所代表的方法的名字

  String methodName = mDescArray[i].getName();

  String methodParams = new  String();

  //获得该方法对象

  Method methodObj = mDescArray[i].getMethod();

  //通过方法对象获得该方法的所有参数,以Class数组的形式返回

  Class[] parameters = methodObj.getParameterTypes();

  if  (parameters.length> 0 )

  {

  //获得参数的类型的名字

  methodParams = parameters[0 ].getName();

  for  ( int  j= 1 ;j<parameters.length ;j++ )

  {

  methodParams = methodParams + ","  + parameters[j].getName();

  }

  }

  output += methodName + "("  + methodParams +  ")\n" ;

  }

  System.out.println(output);

  /*

  * BeanInfo.getPropertyDescriptors()

  * 用于获取该Bean中的所有允许公开的成员属性,以PropertyDescriptor数组的形式返回

  *

  * PropertyDescriptor类

  * 用于描述一个成员属性

  *

  * PropertyDescriptor.getName()

  * 获得该属性的名字

  *

  * PropertyDescriptor.getPropertyType()

  * 获得该属性的数据类型,以Class的形式给出

  *

  */

  output = "内省成员属性:\n" ;

  PropertyDescriptor[] mPropertyArray = bInfoObject.getPropertyDescriptors();

  for  ( int  i= 0 ;i<mPropertyArray.length ;i++ )

  {

  String propertyName = mPropertyArray[i].getName();

  Class propertyType = mPropertyArray[i].getPropertyType();

  output += propertyName + " ( "  + propertyType.getName() +  " )\n" ;

  }

  System.out.println(output);

  /*

  * BeanInfo.getEventSetDescriptors()

  * 用于获取该Bean中的所有允许公开的成员事件,以EventSetDescriptor数组的形式返回

  *

  * EventSetDescriptor类

  * 用于描述一个成员事件

  *

  * EventSetDescriptor.getName()

  * 获得该事件的名字

  *

  * EventSetDescriptor.getListenerType()

  * 获得该事件所依赖的事件监听器,以Class的形式给出

  *

  */

  output = "内省绑定事件:\n" ;

  EventSetDescriptor[] mEventArray = bInfoObject.getEventSetDescriptors();

  for  ( int  i= 0 ;i<mEventArray.length ;i++ )

  {

  String EventName = mEventArray[i].getName();

  Class listenerType = mEventArray[i].getListenerType();

  output += EventName + "("  + listenerType.getName() +  ")\n" ;

  }

  System.out.println(output);

  System.out.println("write by esonghui :" );

  }

  catch  (Exception e)

  {

  System.out.println("异常:"  + e);

  }

  }

  public   static   void  main(String[] args)

  {

  new  myBeanIntrospector();

  }

  }

分享到:
评论

相关推荐

    JAVA的内省机制(introspector)与反射机制(reflection).docx

    "JAVA的内省机制(introspector)与反射机制(reflection)" JAVA 的内省机制(introspector)和反射机制(reflection)是两个重要的概念,在 JAVA 编程中扮演着至关重要的角色。那么,什么是内省机制和反射机制?它们...

    java反射机制介绍

    java有着一个非常突出的动态相关机制:Reflection。这个字的意思是“反射、映象、倒影”,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才...

    候捷谈Java反射机制

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。...

    Java反射与内省-PPT

    JAVA反射机制、注解、动态加载类、动态调用对象方法

    java反射机制 读者基础:具备Java 语言基础

    这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称的class的内部信息,包括其modifiers(诸如public,static 等等)、superclass(例如Object)、实现之interfaces(例如Cloneable),也包括fields和...

    06-内省.xmind

    内省机制

    java项目应用中自定义sql在ibaits框架中的使用.docx

    该文章介绍了自定义sql在ibaits应用,并描述了自己在实现过程中遇到的问题及解决过程,望能遇到同样问题的你提供参考

    NewsCrusader

    提供一个索引内省机制,以后可以在此基础上构建以支持查询。 实现可以将自由文本解析为结构化查询词的查询解析器 实现一个查询机制,可以摄取查询词并找到匹配的文档 实施两种流行的评分机制来对检索到的文档进行...

    Tcl_TK编程权威指南pdf

    内省(introspection) namespace命令 转换现有的软件包以使用名字空间 [incrtcl]对象系统 注意事项 第15章 国际化(internationalization) 字符集与编码 消息目录 第16章 事件驱动的编程 tcl事件循环 ...

Global site tag (gtag.js) - Google Analytics