package JavaProject;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.*;
public class RefkectPoint {
private int x;
private int y;
ReflectPoint(itnt x,int y){
this.x=x;
this.y=y;
}
public void setX(int x){
this.x=x;
}
public int getX(){
return x;
}
public void setY(int y){
this.y=y;
}
public int getY(){
return y;
}
public static void main(String[] args)throws Exception{
ReflectPoint pt1=new ReflectPoint(2,5);
String className="x";
BeanInfo beanInfo=Introspector.getBeanInfo(pt1.getClass());
PropertyDescriptor[] pds= beanInfo.getPropertyDescriptors();
System.out.println(pds.length);
for(PropertyDescriptor pro:pds)
{
System.out.println(pro.getName());
System.out.println(className.getClass());
System.out.println(pro.getDisplayName());
System.out.println(pt1.getClass());
if(pro.getName().equals(className))
{
Method method=pro.getReadMethod();
Object value=method.invoke(pt1);
System.out.println(value);
break;
}
} |
|