mapをオブジェクトに変換

9128 ワード

どのようにしてMapを指定されたオブジェクトに変換しますか?まずテストを見てください.
@Test
    public void test_convertMap2Obj() throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{
        Map map=new HashMap();
        map.put("personName", " ");
        map.put("birthdate", new Timestamp(1000255));
        map.put("identitify", "422203158895255555");
        map.put("age", 21);
        Object obj=ReflectHWUtils.convertMap2Obj(map, Person2.class);
        Person2 person=(Person2)obj;
//      System.out.println(obj);
        System.out.println(person.getPersonName());
        System.out.println(person.getAge());
        System.out.println(person.getBirthdate());
    }

運行結果:黄威21 1970-01-01 08:16:40.255
ReflectHWUtils.convertMap 2 Objは以下のように実現される.
/*** * convert map to object ,see setObjectValue(obj, map) * @param map : key , value  * @param clazz * @return * @throws InstantiationException * @throws IllegalAccessException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException */
    public static Object convertMap2Obj(Map<String, Object> map,Class clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{
        if(ValueWidget.isNullOrEmpty(map)){
            return null;
        }
        Object obj=clazz.newInstance();
        setObjectValue(obj, map);
        /*for(Iterator it=map.entrySet().iterator();it.hasNext();){ Map.Entry<String, Object> entry=(Map.Entry<String, Object>)it.next(); String key=entry.getKey(); Object val=entry.getValue(); }*/
        return obj;
    }

/*** *  .  : setter  . * * @param obj * @param params * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */
    public static void setObjectValue(Object obj, Map<String, Object> params)
            throws SecurityException, NoSuchFieldException,
            IllegalArgumentException, IllegalAccessException {
        if(ValueWidget.isNullOrEmpty(obj)){
            return;
        }
        if (ValueWidget.isNullOrEmpty(params)) {
            return;
        }
        Class<?> clazz = obj.getClass();
        for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
                    .next();
            String key = entry.getKey();
            Object propertyValue = entry.getValue();
            if (ValueWidget.isNullOrEmpty(propertyValue)) {
                continue;
            }
            Field name = getSpecifiedField(clazz, key);
            if (name != null) {
                name.setAccessible(true);
                name.set(obj, propertyValue);
            }
        }

    }

依存メソッド:
/*** *  .  : setter  . * * @param obj * @param params * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */
    public static void setObjectValue(Object obj, Map<String, Object> params)
            throws SecurityException, NoSuchFieldException,
            IllegalArgumentException, IllegalAccessException {
        if(ValueWidget.isNullOrEmpty(obj)){
            return;
        }
        if (ValueWidget.isNullOrEmpty(params)) {
            return;
        }
        Class<?> clazz = obj.getClass();
        for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
                    .next();
            String key = entry.getKey();
            Object propertyValue = entry.getValue();
            if (ValueWidget.isNullOrEmpty(propertyValue)) {
                continue;
            }
            Field name = getSpecifiedField(clazz, key);
            if (name != null) {
                name.setAccessible(true);
                name.set(obj, propertyValue);
            }
        }

    }

    /*** * Get Specified Field * * @param clazz * @param fieldName * @return */
    public static Field getSpecifiedField(Class<?> clazz, String fieldName) {
        Field f = null;
        if (ValueWidget.isNullOrEmpty(clazz)||ValueWidget.isNullOrEmpty(fieldName)) {
            return null;
        }
        try {
            f = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            Class superClass2=clazz.getSuperclass();
            if (superClass2.getName().equals(Object.class.getName())){
                return null;
            }
            return getSpecifiedField(superClass2/* * may be null if it is * Object . */, fieldName);
            // e.printStackTrace();
        }
        return f;
    }

ソースコード参照:ソースコードダウンロードアドレスはブラウザにまたがってplaceholderを実現する