`
sugongqing
  • 浏览: 77359 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Introspector

    博客分类:
  • java
阅读更多
  1. import  javax.swing.*;  
  2. import  java.awt.*;  
  3. import  java.awt.event.*;  
  4. import  java.beans.*;  
  5. import  java.io.*;  
  6. import  java.lang.reflect.Method;  
  7. public   class  myBeanIntrospector   
  8. {  
  9.  public  myBeanIntrospector ()  
  10.  {  
  11.   try   
  12.   {  
  13.    //实例化一个Bean   
  14.    Student beanObj = new  Student();  
  15.    //依据Bean产生一个相关的BeanInfo类   
  16.    BeanInfo bInfoObject =   
  17.    Introspector .getBeanInfo(beanObj.getClass(),beanObj.getClass().getSuperclass());  
  18.    //定义一个用于显示的字符串   
  19.    String output = "" ;  
  20.   
  21.    //开始自省   
  22.    
  23.    /*  
  24.    * BeanInfo.getMethodDescriptors()  
  25.    * 用于获取该Bean中的所有允许公开的成员方法,以MethodDescriptor数组的形式返回  
  26.    *  
  27.    * MethodDescriptor类  
  28.    * 用于记载一个成员方法的所有信息  
  29.    * MethodDescriptor.getName()  
  30.    * 获得该方法的方法名字  
  31.    * MethodDescriptor.getMethod()  
  32.    * 获得该方法的方法对象(Method类)  
  33.    *  
  34.    * Method类  
  35.    * 记载一个具体的的方法的所有信息  
  36.    * Method.getParameterTypes()  
  37.    * 获得该方法所用到的所有参数,以Class数组的形式返回  
  38.    *  
  39.    * Class..getName()  
  40.    * 获得该类型的名字  
  41.    */   
  42.    output = "内省成员方法:\n" ;  
  43.    MethodDescriptor[] mDescArray = bInfoObject.getMethodDescriptors();  
  44.    for  ( int  i= 0 ;i<mDescArray.length ;i++ )  
  45.    {  
  46.     //获得一个成员方法描述器所代表的方法的名字   
  47.     String methodName = mDescArray[i].getName();  
  48.       
  49.     String methodParams = new  String();  
  50.     //获得该方法对象   
  51.     Method methodObj = mDescArray[i].getMethod();  
  52.     //通过方法对象获得该方法的所有参数,以Class数组的形式返回   
  53.     Class[] parameters = methodObj.getParameterTypes();  
  54.     if  (parameters.length> 0 )  
  55.     {  
  56.      //获得参数的类型的名字   
  57.      methodParams = parameters[0 ].getName();  
  58.      for  ( int  j= 1 ;j<parameters.length ;j++ )  
  59.      {  
  60.       methodParams = methodParams + ","  + parameters[j].getName();  
  61.      }  
  62.     }  
  63.     output += methodName + "("  + methodParams +  ")\n" ;  
  64.    }  
  65.    System.out.println(output);  
  66.    
  67.    /*  
  68.    * BeanInfo.getPropertyDescriptors()  
  69.    * 用于获取该Bean中的所有允许公开的成员属性,以PropertyDescriptor数组的形式返回  
  70.    *  
  71.    * PropertyDescriptor类  
  72.    * 用于描述一个成员属性  
  73.    *  
  74.    * PropertyDescriptor.getName()  
  75.    * 获得该属性的名字  
  76.    *  
  77.    * PropertyDescriptor.getPropertyType()  
  78.    * 获得该属性的数据类型,以Class的形式给出  
  79.    *  
  80.    */   
  81.    output = "内省成员属性:\n" ;  
  82.    PropertyDescriptor[] mPropertyArray = bInfoObject.getPropertyDescriptors();  
  83.    for  ( int  i= 0 ;i<mPropertyArray.length ;i++ )  
  84.    {  
  85.     String propertyName = mPropertyArray[i].getName();  
  86.     Class propertyType = mPropertyArray[i].getPropertyType();  
  87.     output += propertyName + " ( "  + propertyType.getName() +  " )\n" ;  
  88.    }  
  89.    System.out.println(output);  
  90.    
  91.   
  92.    /*  
  93.    * BeanInfo.getEventSetDescriptors()  
  94.    * 用于获取该Bean中的所有允许公开的成员事件,以EventSetDescriptor数组的形式返回  
  95.    *  
  96.    * EventSetDescriptor类  
  97.    * 用于描述一个成员事件  
  98.    *  
  99.    * EventSetDescriptor.getName()  
  100.    * 获得该事件的名字  
  101.    *  
  102.    * EventSetDescriptor.getListenerType()  
  103.    * 获得该事件所依赖的事件监听器,以Class的形式给出  
  104.    *  
  105.    */   
  106.    output = "内省绑定事件:\n" ;  
  107.    EventSetDescriptor[] mEventArray = bInfoObject.getEventSetDescriptors();  
  108.    for  ( int  i= 0 ;i<mEventArray.length ;i++ )  
  109.    {  
  110.     String EventName = mEventArray[i].getName();  
  111.     Class listenerType = mEventArray[i].getListenerType();  
  112.     output += EventName + "("  + listenerType.getName() +  ")\n" ;  
  113.    }  
  114.    System.out.println(output);  
  115.    System.out.println("write by esonghui :)" );  
  116.    
  117.   }  
  118.   catch  (Exception e)  
  119.   {  
  120.    System.out.println("异常:"  + e);  
  121.   }  
  122.  }  
  123.  public   static   void  main(String[] args)   
  124.  {  
  125.   new  myBeanIntrospector ();  
  126.  }  

分享到:
评论
1 楼 niuqiang2008 2011-04-13  
非常感谢您的代码
我自己补了Student的属性 ,
但我不知怎样测怎样 测"内省绑定事件:\n" ;  

相关推荐

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

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

    Neo4j GraphQL 库(graphql--neo4j-introspector-1.0.1.zip)

    Neo4j GraphQL 库(graphql--neo4j-introspector-1.0.1.zip) 源代码。 Neo4j GraphQL 库是一个高度灵活、低代码、开源的 JavaScript 库,可通过利用连接数据的力量为跨平台和移动应用程序实现快速 API 开发。 ...

    Neo4j GraphQL 库(graphql--neo4j-introspector-1.0.1.tar.gz)

    Neo4j GraphQL 库(graphql--neo4j-introspector-1.0.1.tar.gz) 源代码。 Neo4j GraphQL 库是一个高度灵活、低代码、开源的 JavaScript 库,可通过利用连接数据的力量为跨平台和移动应用程序实现快速 API 开发。 ...

    Python库 | plone.introspector-0.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:plone.introspector-0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Java 内省introspector相关原理代码解析

    主要介绍了Java 内省introspector相关原理代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    java-beans-lite:轻量级且快速的java.beans.Introspector实现

    轻巧,快速的java.beans.Introspector重新实现,用于消除对Bean Introspection的java.desktop模块的依赖。 问题 JDK 9中引入的模块封装了Java标准库中的所有AWT,Swing,Image和Sound软件包。 除此之外,它还包含带...

    使用struts2的Introspector做日志

    NULL 博文链接:https://liuna718-163-com.iteye.com/blog/1731217

    backbone-introspector:骨干应用程序自省-生成应用程序组件的树状图(使用d3.js)

    //require the introspector file'introspector'] , function ( Backbone , AppView , Workspace , introspector ) {/*jshint nonew:false*/// Initialize routing and start Backbone.history()new Workspace ( ) ;...

    Java 内省(Introspector)深入理解

    主要介绍了Java 内省(Introspector)深入理解的相关资料,需要的朋友可以参考下

    php-introspector

    php-introspector 这个包是一个薄包装器,它从内省中检索建议: 安装 使用 Atom 包管理器,它可以在设置视图中找到,或者从命令行运行apm install php-introspector 。 您还需要安装软件包。 用法 目前,自动完成...

    RDF Software Introspector-开源

    Introspector使处理源代码的编程工具(例如编译器)能够以标准且中立的方式进行通信,从而减少了意外的编程成本。 http://github.com/h4ck3rm1k3/

    openbeans1.0.zip

    //import java.beans.Introspector; //import java.beans.PropertyDescriptor; import com.googlecode.openbeans.BeanInfo; import com.googlecode.openbeans.IntrospectionException; import ...

    beanutils-1.9.3-bin

    在由JDK提供的默认的API中,有java.beans包,里面有诸如Introspector,BeanInfo,PropertyDescriptor等用来操作JavaBean的方法, 但是由Apache公司开发的BeanUtils会更常用,同时,BeanUtils还需要配合第三方日志...

    Eclipse OLE Introspector-开源

    Eclipse 平台的 COM 内省器和代码生成器

    CBIntrospector:可视化调试工具。 与桌面应用程序 View Introspector 兼容。 简单来说,它是一个 UIView 层次结构查看器库

    CBIntrospectorIntrospector 是一小组适用于iOS 6+和 iOS 模拟器的工具,可帮助调试使用 UIKit 构建的用户界面。 它对于动态创建或可以在运行时更改的 UI 布局,或者通过查找非不透明视图或不必要地重新绘制的视图来...

    Think in Java(中文版)chm格式

    13.18.2 用Introspector提取BeanInfo 13.18.3 一个更复杂的Bean 13.18.4 Bean的封装 13.18.5 更复杂的Bean支持 13.18.6 Bean更多的知识 13.19 Swing入门 13.19.1 Swing有哪些优点 13.19.2 方便的转换 13.19....

    Thinking in Java 中文第四版+习题答案

    13.18.2 用Introspector提取 13.18.3 一个更复杂的 13.18.4 Bean的封装 13.18.5 更复杂的Bean支持 13.18.6 Bean更多的知识 13.19 Swing入门 13.19.1 Swing有哪些优点 13.19.2 方便的转换 13.19.3 显示框架 13.19.4 ...

    JAVA_Thinking in Java

    13.18.2 用Introspector提取BeanInfo 13.18.3 一个更复杂的Bean 13.18.4 Bean的封装 13.18.5 更复杂的Bean支持 13.18.6 Bean更多的知识 13.19 Swing入门 13.19.1 Swing有哪些优点 13.19.2 方便的转换 13.19.3 显示...

    ThinkInJava

    13.18.2 用Introspector提取BeanInfo 13.18.3 一个更复杂的Bean 13.18.4 Bean的封装 13.18.5 更复杂的Bean支持 13.18.6 Bean更多的知识 13.19 Swing入门 13.19.1 Swing有哪些优点 13.19.2 方便的转换 13.19.3 显示...

    java 编程入门思考

    13.18.2 用Introspector提取BeanInfo 13.18.3 一个更复杂的Bean 13.18.4 Bean的封装 13.18.5 更复杂的Bean支持 13.18.6 Bean更多的知识 13.19 Swing入门 13.19.1 Swing有哪些优点 13.19.2 方便的转换 13.19.3 显示...

Global site tag (gtag.js) - Google Analytics