JAVA反射(一)

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;
}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值