Java中的内省及javabean操作
------- android培训、java培训、期待与您交流! ----------
在java的反射中,所有的类被抽象出一个类,即Class类,这样我们就可以在程序运行的过程中通过配置文件,动态的加载类。但是在用反射的时候有些前 提,就是:当我们调用有参的constructor的时候必须先知道构造函数传入的参数是什么类型;调用Method的时候必须先知道成员函数传入的参数 是什么类型;调用field的时候必须先知道成员变量的类型。但是在某些情况下,一个类中的成员属性的名字对外是不可见的,这时候我们只可以得出他的成员 类型的数组field[],在不知道确切变量名的情况下不可以定位到确切的变量上。而他提供了对这个属性公开的读(get)、写(set)方法, 虽然方法名和方法操作的对象有时候有很大的关系,但是我们不能保证这个关系一定成立。而且这种只提供get、set方法,不提供变量名的情况还很常见,因 此java就将这种抽象为javabean类,对javabean类的操作,通过get、set函数后面的名字就“拟”得出变量的名字。通过这个名字,我 们可以确切的求出某个变量的值。而对javabean的操作是通过内省(introspector)来完成的。上例子:
package reflect; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.MethodDescriptor; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; public class TestBean { public static void main(String[] args) throws Exception { testProDes(); testBeanInfo(); testBeanInfo1(); } static void testBeanInfo() throws Exception{ Dog d = (Dog) Class.forName("reflect.Dog").newInstance(); String myname = "name"; BeanInfo beanInfo = Introspector.getBeanInfo(Dog.class);//beaninfo中存放bean的所有信息 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for(PropertyDescriptor pd : propertyDescriptors){ if(pd.getName().equals(myname)){ Method m1 = pd.getWriteMethod(); m1.invoke(d,"zhangsan"); Method m2 = pd.getReadMethod(); m2.invoke(d); System.out.println(d); break; } } } static void testProDes() throws Exception{ Dog d = (Dog) Class.forName("reflect.Dog").newInstance(); String myname = "name" ;//这里是根据函数名确定的string的变量名,而不是内部定义的私有属性名。 PropertyDescriptor pd = new PropertyDescriptor(myname, Dog.class);//propertyDescriptor直接得到属性描述符 Method m1 = pd.getWriteMethod(); m1.invoke(d,"zhangsan"); Method m2 = pd.getReadMethod(); m2.invoke(d); System.out.println(d); } static void testBeanInfo1() throws Exception{ Dog d = (Dog) Class.forName("reflect.Dog").newInstance(); String myname = "name"; BeanInfo beanInfo = Introspector.getBeanInfo(Dog.class);//beaninfo中存放bean的所有信息 MethodDescriptor[] mds = beanInfo.getMethodDescriptors(); for(MethodDescriptor md : mds){ String str = md.getName(); if (str.matches("[gs]et.*")) { System.out.println(str);//打印set/get开头的函数 } } } } class Dog { private String n ; private int a ; public Dog(){ } public Dog(String name,int age){ this.n = name; this.a = age; } public String getName() { return n; } public void setName(String name) { this.n = name; } public int getAge() { return a; } public void setAge(int age) { this.a = age; } @Override public String toString(){ return n + " : "+a; } }
在这里我们故意将Dog类中的get/set方法和属性名定义的不一样。但是通过内省,我们并不需要知道确切的内部属性的名字,通过get/set的名字,就可以实现对属性的设置和读取。
第一个函数直接用propertyDescriptor得出属性描述符,然后通过read方法求出get对应的method,write方法求出set对应的method,通过method的反射invoke函数实现对属性的操作。
第二种函数和第一个函数实现一样的功能,但是路径不一样,先用introspector静态方法得出beaninfo,beaninfo中包含了所 有的关于类的信息,再通过beaninfo的方法得出propertyDescriptor,在按照第一种的实现方法实现对属性的操作。
第三个函数顺便测试了methoddescripor,将这个javabean类中的所有get/set的方法打印出来。