JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。
对JavaBean的简单内省操作
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InstrospectorTest
{
/**
* @param args
* @throws IntrospectionException
*/
public static void main(String[] args) throws Exception
{
ReflectPoint p=new ReflectPoint(3,5);
String propertyName="x";
//x-->X-getX-->MethodGetX
Object retVal = getProperty(p, propertyName);
System.out.println(retVal);
Object value=7;
setProperty(p, propertyName, value);
System.out.println(p.getX());
}
private static void setProperty(Object p, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException
{
PropertyDescriptor pd2=new PropertyDescriptor(propertyName,p.getClass());
Method methodSetX=pd2.getWriteMethod();
Object SetVal=methodSetX.invoke(p,value);
}
private static Object getProperty(Object p, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException
{
PropertyDescriptor pd=new PropertyDescriptor(propertyName,p.getClass());
Method methodGetX=pd.getReadMethod();
Object retVal=methodGetX.invoke(p);
return retVal;
}
}
对javabean的复杂内省
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;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class InstrospectorTest
{
/**
* @param args
* @throws IntrospectionException
*/
public static void main(String[] args) throws Exception
{
ReflectPoint p=new ReflectPoint(3,5);
String propertyName="x";
//x-->X-getX-->MethodGetX
Object retVal = getProperty(p, propertyName);
System.out.println(retVal);
Object value=7;
setProperty(p, propertyName, value);
System.out.println(p.getX());
System.out.println(BeanUtils.getProperty(p, "x").getClass().getName());
BeanUtils.setProperty(p, "x",9);
System.out.println(p.getX());
BeanUtils.setProperty(p, "birthday.setTime", "111");
System.out.println(BeanUtils.getProperty(p, "birthday.setTime"));
/* Map map={name:"zhansan",age:18};
* BeanUtils.setProperty(map,"name","lim");
*/
PropertyUtils.setProperty(p, "x", 11);
System.out.println(PropertyUtils.getProperty(p, "x").getClass().getName());
}
private static void setProperty(Object p, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException
{
PropertyDescriptor pd2=new PropertyDescriptor(propertyName,p.getClass());
Method methodSetX=pd2.getWriteMethod();
Object SetVal=methodSetX.invoke(p,value);
}
private static Object getProperty(Object p, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException
{
/* PropertyDescriptor pd=new PropertyDescriptor(propertyName,p.getClass());
Method methodGetX=pd.getReadMethod();
Object retVal=methodGetX.invoke(p);*/
BeanInfo beanInfo=Introspector.getBeanInfo(p.getClass());
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
Object retVal=null;
for(PropertyDescriptor pd:pds)
{
if(pd.getName().equals(propertyName))
{
Method methodGetX=pd.getReadMethod();
retVal=methodGetX.invoke(p);
break;
}
}
return retVal;
}
}
复杂内省实现思路:
1、在IntroSpector类中有getBeanInfo(Class cls)的方法。
2、获取Class对象的Bean信息,返回的是BeanInfo类型。
3、BeanInfo类中有getPropertyDescriptors()的方法,可获取所有的BeanInfo的属性信息,返回一个PropertyDescriptor[]。
4、在通过遍历的形式,找出与自己想要的那个属性信息。