import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestReflection {
public static void main(String[] args){
User user = new User();
user.setUserName("ZhangSan");
user.setPassword("123456");
try {
printObject( user );
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void printObject(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Field[] fields = obj.getClass().getDeclaredFields();
for(int i= 0; i<fields.length; i++){
Field f = fields[i];
String fieldName = f.getName();
String stringLetter = fieldName.substring(0, 1).toUpperCase();
String getName="get"+stringLetter+fieldName.substring(1);
Class classType=obj.getClass();
//获取相应的方法
Method getMethod=classType.getMethod(getName, new Class[]{});
//调用源对象的getXXX()方法
Object value=getMethod.invoke(obj, new Object[]{});
System.out.println(fieldName+": "+value);
}
}
}
class User{
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestReflection {
public static void main(String[] args){
User user = new User();
user.setUserName("ZhangSan");
user.setPassword("123456");
try {
printObject( user );
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void printObject(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Field[] fields = obj.getClass().getDeclaredFields();
for(int i= 0; i<fields.length; i++){
Field f = fields[i];
String fieldName = f.getName();
String stringLetter = fieldName.substring(0, 1).toUpperCase();
String getName="get"+stringLetter+fieldName.substring(1);
Class classType=obj.getClass();
//获取相应的方法
Method getMethod=classType.getMethod(getName, new Class[]{});
//调用源对象的getXXX()方法
Object value=getMethod.invoke(obj, new Object[]{});
System.out.println(fieldName+": "+value);
}
}
}
class User{
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}