利用发反射对对象进行赋值
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);*/ } }
相关文章推荐
- 利用operator关键字在对象间进行赋值的几点注意事项(2)
- Java中的反射机制(二) 一个利用反射进行对象拷贝的例子
- 同类型对象利用反射自动赋值
- Java中的反射机制(二) 一个利用反射进行对象拷贝的例子
- 笔记—利用反射给对象赋值
- java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。
- C#自动给据sql中的带@的变量提取变量名称在从简单数据对象中取得生成SqlParameter数组进行数据插入(利用反射完成)
- 一个利用反射进行对象拷贝的例子
- golang---利用反射机制对结构体进行循环赋值
- 遍历 Map 对象, 利用反射为 Class 对象的对应的属性赋值.
- 利用反射实现两个对象赋值
- Java中的反射机制(二) 一个利用反射进行对象拷贝的例子
- 代码 - 通过反射对象属性进行遍历赋值
- java 利用反射进行赋值
- C#自动给据sql中的带@的变量提取变量名称在从简单数据对象中取得生成SqlParameter数组进行数据插入(利用反射完成)
- c# 利用反射动态给实体类对象赋值
- c# 利用反射动态给实体类对象赋值
- c# 利用反射动态给实体类对象赋值
- 利用反射机制来获取表单请求值以及对象赋值
- 利用反射机制来获取表单请求值以及对象赋值(使用了第三方插件)