我感觉你没有把jar包正确的导入进来,我测试过你的代码了,可以运行
- import java.beans.IntrospectionException;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import org.apache.commons.beanutils.BeanUtils;
- public class IntroSpectorTest {
- public static void main(String[] args)throws Exception{
- ReflectPoint pt1=new ReflectPoint(3,5);
-
- String propertyName="x";
-
- PropertyDescriptor pd = getProperty(pt1, propertyName);
-
-
- //PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
- Object value=7;
-
- setProperties(pt1, pd, value);
-
- System.out.println(BeanUtils.getProperty(pt1, "x"));
-
- }
- private static void setProperties(Object pt1, PropertyDescriptor pd,
- Object value) throws IllegalAccessException,
- InvocationTargetException {
- Method methodSetX=pd.getWriteMethod();
- methodSetX.invoke(pt1,value);
- //Object retVal=methodGetX.invoke(pt1);
- //System.out.println(pt1.getX());
- }
- private static PropertyDescriptor getProperty(Object pt1,
- String propertyName) throws IntrospectionException,
- IllegalAccessException, InvocationTargetException {
- PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
- Method methodGetX=pd.getReadMethod();
- Object retVal=methodGetX.invoke(pt1);
- System.out.println(retVal);
- return pd;
- }
- }[code]
- public class ReflectPoint {
- private int x;
- private int 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;
- }
- public ReflectPoint(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
- public ReflectPoint()
- {}
- }
复制代码 [/code]
|
|