public class Test {
public Test() {
}
public static void main(String[] args) throws IllegalArgumentException,
SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Bean bean = new Bean();
System.out.println(bean);
Bean.class.getMethod("setName", String.class).invoke(bean, "Jerry");
Bean.class.getMethod("setAge", int.class).invoke(bean, 25);
System.out.println("After reflection....\n" + bean);
}
}
class Bean {
private String name;
private int age;
public Bean() {
}
public Bean(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "NAME: " + this.getName() + "\nAGE: " + this.getAge();
}
} |
|