您的位置:首页 > 编程语言 > Java开发

利用发反射对对象进行赋值

2016-08-24 20:26 387 查看
public class Demo1 {

public static Object getObejctParamater(String className,HashMap<String,String> map) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, ParseException {
Class<?> myclass= Class.forName(className);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Object object = myclass.newInstance();
for(Map.Entry<String,String> entry:map.entrySet()){
Field field = myclass.getDeclaredField(entry.getKey());
field.setAccessible(true);

if("Boolean".equals(field.getType().getSimpleName())){
field.set(object,entry.getValue().equals("true"));
}else if("BigDecimal".equals(field.getType().getSimpleName())){
field.set(object, new BigDecimal(entry.getValue()));
}else if("Date".equals(field.getType().getSimpleName())){
field.set(object, simpleDateFormat.parse(entry.getValue()));
}else if("Timestamp".equals(field.getType().getSimpleName())){
field.set(object, Timestamp.valueOf(entry.getValue()));
}else if(field.getType().isEnum()){
field.set(object,Enum.valueOf((Class<Enum>) field.getType(), entry.getValue()));
}else if("int".equals(field.getType().getSimpleName())||"Integer".equals(field.getType().getSimpleName())){
field.set(object, Integer.parseInt(entry.getValue()));
}else if ("String".equals(field.getType().getSimpleName())){
field.set(object,entry.getValue());
}
//            System.out.println(field.get(object));

}
return object;
}

public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ClassNotFoundException, ParseException {

/*    try {
Class<?> myclass= Class.forName("com.lu.entities.Group");

Method method=myclass.getDeclaredMethod("setGroup_name", String.class);
method.invoke(myclass.newInstance(),"testname");

} catch (ClassNotFoundException e) {
e.printStackTrace();
}*/

HashMap<String, String> map = new HashMap<String, String>();
map.put("group_name", "test_groupname");
map.put("group_desc", "test_group_desc");
map.put("id", "test_id");
map.put("test_field", "mytest_field");
map.put("flag", "false");
map.put("price", "3000");
map.put("brithday", "1990-12-11 15:00:00");
map.put("updated_at", "1990-12-11 15:00:00");
map.put("colorEnum", "BLACK");
map.put("age", "11");
//        map.put("count", "9");

Object group = getObejctParamater("com.my.practise.reflection.Student", map);
System.out.println(group.toString());

/*    Class c = Class.forName("com.my.practise.reflection.Student");
Field[] fs = c.getDeclaredFields();

//定义可变长的字符串,用来存储属性
StringBuffer sb = new StringBuffer();
//通过追加的方法,将每个属性拼接到此字符串中
//最外边的public定义
sb.append(Modifier.toString(c.getModifiers()) + " class " + c.getSimpleName() +"{\n");
//里边的每一个属性
for(Field field:fs){
sb.append("\t");//空格
sb.append(Modifier.toString(field.getModifiers())+" ");//获得属性的修饰符,例如public,static等等
sb.append(field.getType() + " ");//属性的类型的名字
sb.append(field.getName()+";\n");//属性的名字+回车
}

sb.append("}");

System.out.println(sb);*/

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA reflection