import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
public class IntroSpectorTest {
public static void main(String[] args) throws Exception {
ReflectPoint pt=new ReflectPoint(3,5);
String propertyName="x";
PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt.getClass());
Method methodGetX1 = pd.getReadMethod();
System.out.println(methodGetX1.invoke(pt));
System.out.println(BeanUtils.getProperty(pt, propertyName));
System.out.println( pt.getX());
}
}
class ReflectPoint {
private int x;
private int y;
public ReflectPoint(int x,int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
运行里,提示
Exception in thread "main" java.lang.NoSuchMethodException: Property 'x' has no getter method in class 'class ReflectPoint'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1327)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741)
at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:382)
at IntroSpectorTest.main(IntroSpectorTest.java:18)
|
|