本帖最后由 万马奔腾 于 2014-6-2 18:35 编辑
定义的User类如下:
public class User {
private String name;
public User(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
用内省操作代码如下:
import java.beans.*;
import java.lang.reflect.Method;
public class IntrospectorTest {
public static void main(String[] args) throws Exception {
User user = new User("zhangsan");
String uName = "lisi";
setProperty(user,uName);
}
private static void setProperty(User user, String uName) throws Exception {
PropertyDescriptor pd = new PropertyDescriptor(uName,User.class);
Method methodSetUname = pd.getWriteMethod();
methodSetUname.invoke(user, uName);
System.out.println(user.getName());
}
}
出现错误如下:
Exception in thread "main" java.beans.IntrospectionException: Method not found: isLisi
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:106)
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:70)
at techAct5.IntrospectorTest.setProperty(IntrospectorTest.java:32)
at techAct5.IntrospectorTest.main(IntrospectorTest.java:19)
求指导
|
|