- import java.beans.IntrospectionException;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- public class MethodMain {
- /**
- * @param args
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- * @throws IntrospectionException
- * @throws IllegalArgumentException
- */
- public static void main(String[] args) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException {
- // TODO Auto-generated method stub
- Person obj=new Person();
- String propertyName="setName";
- Object value="lisi";
- setProperty(obj,propertyName,value);
- System.out.println(obj.getName());
-
- }
- public static void setProperty(Object obj,String propertyName,Object value) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
- {
- PropertyDescriptor pd=new PropertyDescriptor(propertyName,obj.getClass());
- Method me=pd.getWriteMethod();
- me.invoke(obj, value);
- }
- }
- class Person
- {
- private String name;
- private int age;
- public Person()
- {}
- public Person(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
- public void setName(String name)
- {
- this.name=name;
-
- }
- public String getName()
- {
- return name;
- }
-
- }
复制代码 提示错误:
Exception in thread "main" java.beans.IntrospectionException: Method not found: isSetName
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:89)
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:53)
at MethodMain.setProperty(MethodMain.java:27)
at MethodMain.main(MethodMain.java:21)
求解决。
|
|