天天看點

填充對象中的時間屬性

前提:不知道對象具體類型.

解決方法:使用反射

填充對象中的時間屬性

/*** 

     * 把對象中的列,類型為時間的都設定為目前時間 

     * @param obj 

     * @throws securityexception 

     * @throws nosuchfieldexception 

     * @throws illegalargumentexception 

     * @throws illegalaccessexception 

     */  

    public static void filltimeforobj(object obj) throws securityexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception{  

        list<field> fieldslist=getallfieldlist(obj.getclass());  

        int size=fieldslist.size();  

        for(int i=0;i<size;i++){  

            field f=fieldslist.get(i);  

            string typename=f.gettype().getname();  

            if(typename.equals("java.sql.timestamp")||typename.equals("java.util.date")){  

                setobjectvalue(obj, f, timehwutil.getcurrenttimestamp());  

            }else if(typename.equals("java.sql.date")){  

                setobjectvalue(obj, f, new java.util.date());  

            }  

        }  

    }  

 依賴的方法:

填充對象中的時間屬性

     * get all field ,including fields in father/super class 

     *  

     * @param clazz 

     * @return 

    public static list<field> getallfieldlist(class<?> clazz) {  

        list<field> fieldslist = new arraylist<field>();// return object  

        if (clazz == null) {  

            return null;  

        class<?> superclass = clazz.getsuperclass();// father class  

        if (!superclass.getname().equals(object.class.getname()))/* 

                                                                 * java.lang.object 

                                                                 */{  

            // system.out.println("has father");  

            fieldslist.addall(getallfieldlist(superclass));// recursive  

        field[] fields = clazz.getdeclaredfields();  

        for (int i = 0; i < fields.length; i++) {  

            field field = fields[i];  

            // 排除因實作serializable 接口而産生的屬性serialversionuid  

            if (!field.getname().equals("serialversionuid")) {  

                fieldslist.add(field);  

        return fieldslist;  

     * 設定對象的屬性值。 

     * @param propertyname 

     *            : property name 

     * @param propertyvalue 

     *            : value of property<br> must be string or field 

    public static void setobjectvalue(object obj, object propertyname,  

            object propertyvalue) throws securityexception,  

            nosuchfieldexception, illegalargumentexception,  

            illegalaccessexception {  

        if (valuewidget.isnullorempty(propertyname)  

                || valuewidget.isnullorempty (propertyvalue)) {  

            return;  

        class<?> clazz = obj.getclass();  

        field name = null;  

        if(propertyname instanceof string){  

        name=getspecifiedfield(clazz, (string)propertyname);  

        }else{  

            name=(field)propertyname;  

        name.setaccessible(true);  

        name.set(obj, propertyvalue);  

     * get specified field 

     * @param fieldname 

    public static field getspecifiedfield(class<?> clazz, string fieldname) {  

        field f = null;  

        if (valuewidget.isnullorempty(clazz)) {  

        try {  

            f = clazz.getdeclaredfield(fieldname);  

        } catch (nosuchfieldexception e) {  

            return getspecifiedfield(clazz.getsuperclass()/* 

                                                         * may be null if it is 

                                                         * object . 

                                                         */, fieldname);  

            // e.printstacktrace();  

        return f;  

把a對象的時間屬性的值設定到b對象中

填充對象中的時間屬性

     * 使用spring mvc儲存對象時,對象的createtime(時間類型的屬性)沒有注入進來, 

     * <br>這時需要背景先通過id擷取持久化對象,然後手動注入 

     * @param editedobj 

     * @param persistentobj 

    public static void filltimeforeditedobj(object editedobj,object persistentobj) throws securityexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception{  

        if(!editedobj.getclass().getname().equals(persistentobj.getclass().getname())){  

            throw new runtimeexception("two object type should be the same ,but is different .");  

        list<field> fieldslist=getallfieldlist(editedobj.getclass());  

            if(editedobj instanceof java.util.date || typename.equals("java.sql.timestamp")||typename.equals("java.util.date")||typename.equals("java.sql.date")){  

                object valueedited=getobjectvalue(editedobj, f);  

                object valuepersistent=getobjectvalue(persistentobj, f);  

                if(valuewidget.isnullorempty(valueedited)&&(!valuewidget.isnullorempty(valuepersistent))){  

                    setobjectvalue(editedobj, f, valuepersistent);  

                }  

 參考: