JAVA反射(一)

来源:互联网 发布:什么软件适合iphonex 编辑:程序博客网 时间:2024/05/29 09:20

package read;

import java.text.SimpleDateFormat;
import java.util.Date;

public class User {

public Integer userId;
public String userName;
public String password;
public Date birthday;
public double totalMoney;


public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer userId, String userName, String password,
Date birthday, double totalMoney) {
super();
this.userId = userId;
this.userName = userName;
this.password = password;
this.birthday = birthday;
this.totalMoney = totalMoney;
}
@Override
public String toString() {
return "User [birthday=" + new SimpleDateFormat("yyyy-MM-dd").format(birthday)
+ ", password=" + password
+ ", totalMoney=" + totalMoney + ", userId=" + userId
+ ", userName=" + userName + "]";
}

}


package read;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* @author yy
*/
public class Reflect {

/**
* @author yy
* @param args
*/
public static void main(String[] args) {

Integer userId = 1;
String userName = "张三";
String password = "123456";
Date birthday = new Date();
double totalMoney = 10000.01;

Map map = new HashMap();
map.put("userName", userName);
map.put("password", password);
map.put("birthday", birthday);
map.put("userId", 1);
map.put("totalMoney", totalMoney);

//通过map传参,反射产生对象并为对象赋值
User user = Reflect.getReflectObj(User.class,map);

System.out.println(user.toString());
}

/**
* @author yy
* @param <T>
* @param clazz
* @param map
* @return
*/
public static <T> T getReflectObj(Class<T> clazz,Map map){
//通过反射来赋值
T t=null;
try {
t = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
Field[] fields = clazz.getFields();
for (Field field : fields) {
String fieldName = field.getName();
Iterator<Map.Entry<String, ?>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, ?> entry = it.next();
String key = entry.getKey();
entry.getValue();
String firstChar = fieldName.substring(0,1).toUpperCase();
String setterName = "set" + firstChar + fieldName.substring(1);
Method setter = clazz.getMethod(setterName, new Class[]{field.getType()});
if(field.getName().toLowerCase().trim().equals(key.toLowerCase().trim()))
{
setter.invoke(t,entry.getValue());
}
}

}
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
return t;
}

}




原创粉丝点击