黑马程序员_内省

来源:互联网 发布:linux matlab 启动 编辑:程序博客网 时间:2024/06/05 03:00

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------


内省这个词出现得很怪,就跟我当初刚看到套接字这词的感觉一样,不知道什么意思。后来按中文意思想想,内省过的人会有明确的目标价值观,而内省操作的对象Bean内部代码也呈现了规律性,从而通过类比记住了该词。

而内省访问JavaBean有两种方法:

 

      一、通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的getter/setter 方法,然后通过反射机制来调用这些方法。


首先建立javaBean类,reflectPoint.

 

public class reflectorPoint {

       publicreflectorPoint(int x,int y){

              this.x=x;

              this.y=y;

       }

       private int x;

       private int y;

       public int getX(){

              returnx;

       }

 

       publicvoid setX(int x) {

              this.x= x;

       }

      

       publicint getY() {

              returny;

       }

      

       publicvoid setY(int y) {

              this.y= y;

       }

}

 

第一种方法较为烦锁,因为BeanInfo只能获取对Bean类的所有属性,而不能获取到对应所要查找的成员属性。还需要通过遍历来查找出对应属性。代码如下:

 

package Demo;

 

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

 

public class introSpectorDemo1 {

 

       publicstatic void main(String[] args) throws IntrospectionException,IllegalArgumentException, IllegalAccessException, InvocationTargetException {

              StringproperName="x";

              //创建Bean类reflectorPoint的对象

              reflectorPointrp=new reflectorPoint(4,6);

              //创建BeanInfor对象,获取BeanInofo信息

              BeanInfobi=Introspector.getBeanInfo(rp.getClass());

              //利用BeanInf对象的getPropertyDescriptors方法获取到Bean类的所有成员属性

              PropertyDescriptor[]pds=bi.getPropertyDescriptors();

              //遍历所有成员的属性,当属性名与所要获取的属性名相同时获取该属性的方法

              for(PropertyDescriptorpd:pds){

                     if(pd.getName().equals(properName)){

                            //获取到该成员的get方法

                            MethodgetMethodX=pd.getReadMethod();

                            //调用get方法获取到该变量的值并进行打印

                            System.out.println(getMethodX.invoke(rp));

                            //获取该成员的set方法

                            MethodsetMethodX=pd.getWriteMethod();

                            //调用set方法修改该成员的值

                            setMethodX.invoke(rp,7);

                            System.out.println(getMethodX.invoke(rp));

                     }

              }

 

       }

}

二、通过PropertyDescriptor来操作Bean对象(常用方法)

第二种方法通过PropertyDescriptor来操作Bean对象,代码如下:

 

import java.beans.IntrospectionException;

import java.beans.PropertyDescriptor;

importjava.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

 

public class introSpectorDemo {

 

 

       publicstatic void main(String[] args) throws Exception{

              reflectorPointrp=new reflectorPoint(3,5);

             

              StringpropertyName="x";

              getMethod(rp,propertyName);

             

              intvalue=7;

              setMethod(rp,"y", value);

              System.out.println(getMethod(rp,"y"));

             

       }

 

       //建立setMethod,传入参数为:bean类的对象,所要设置的变量名,所要设置的值

       privatestatic void setMethod(Object rp, String propertyName,

                     intvalue) throws IntrospectionException, IllegalAccessException,

                     InvocationTargetException{

              //创建propertyDescriptor对象,传入所要设置的值的名字,bean的class字节码

              PropertyDescriptorpd1=new PropertyDescriptor(propertyName, rp.getClass());

              //通过propertyDescriptor对象的getWriteMethod方法获取到bean类中对该成员的set方法

              MethodsetMethodX=pd1.getWriteMethod();

              //调用set方法给bean类中的该成员设值

              setMethodX.invoke(rp,value);

       }

       //创建getMethod方法,传入参数是:bean类的对象,所要获致的变量名

       privatestatic Object getMethod(Object rp, String propertyName)

                     throwsIntrospectionException, IllegalAccessException,

                     InvocationTargetException{

              //创建PropertyDescriptor对象,传入参数是:所要获取的变量名,bean类字节码

              PropertyDescriptorpd=new PropertyDescriptor(propertyName, rp.getClass());

              //利用PropertyDescriptor对象的getReadMethod得到bean类中对该成员的get方法

              MethodgetMethodX=pd.getReadMethod();

              //调用get方法,获取到该变量的值

              ObjectretVul=getMethodX.invoke(rp);

              returnretVul;

       }

}

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------


0 0
原创粉丝点击