package com.songwei.struts.model;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
public class User {
public User() {
// TODO Auto-generated constructor stub
this.name = "default";
System.out.println("name=default");
}
public User(String name) {
this.name = name;
System.out.println("name=" + name);
}
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
try {
Class cls1 = Class.forName("com.songwei.struts.model.User");
try {
try {
Constructor con1 = cls1
.getDeclaredConstructor(new Class[] { String.class });
Constructor con2 = cls1
.getDeclaredConstructor(null);
try {
Object obj1 = con1
.newInstance(new Object[] { "songwei" });
Object obj2 = con2.newInstance(new Object[] {});
Field field1 = null, field2 = null;
try {
field1 = cls1.getDeclaredField("name");
field2 = cls1.getDeclaredField("name");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("field1 name=" + field1.get(obj1));
System.out.println("field2 name=" + field2.get(obj2));
Method method1=cls1.getDeclaredMethod("getName", null);
System.out.println("method1 name="+method1.invoke(obj1, null));
Method method2=cls1.getDeclaredMethod("getName", null);
System.out.println("method2 name="+method2.invoke(obj2, null));
Method method3=cls1.getDeclaredMethod("setName", new Class[]{String.class});
method3.invoke(obj2, new Object[]{"Pipper.Sung"});
Method method4=cls1.getDeclaredMethod("getName", null);
System.out.println("method4 name="+method2.invoke(obj2, null));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}