java 反射: 当Timestamp类型的属性值为null时,设置默认值

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;

class Person {
    private String name;
    private int age;

    private Timestamp birth;

    public Timestamp getBirth() {
        return birth;
    }

    public void setBirth(Timestamp birth) {
        this.birth = birth;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }
}

/*****************************************************/
public class InvokeSetterMethod {
    public static void main(String[] args) {
        Person p = new Person();
        invokeSetterMethodByType(p, Person.class, "java.sql.Timestamp",
                Timestamp.valueOf("1111-11-11 11:11:11"), Timestamp.class);
         p.setBirth(Timestamp.valueOf("2014-12-11 00:00:00"));
        System.out.println(p.getBirth());
    }

    /**
     * 调用setter方法
     * 
     * @param obj
     * @param att
     * @param value
     * @param type
     */
    public static void invokeSetterMethodByType(Object obj, Class cl,
            String methodType, Timestamp param, Class<?> paramType) {
        try {

            Field[] f = cl.getDeclaredFields();
            for (Field field : f) {
                // 属性类型
                String type = field.getType().getName();
                // 属性名
                String name = field.getName();
                // 属性值
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
                        cl);
                Method getMethod = pd.getReadMethod();
                Object o = getMethod.invoke(obj);
                // 当Timestamp类型的属性值为null时,设置默认值
                if (methodType.equals(type) && null == o) {
                    setter(obj, name, param, paramType);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用setter方法
     * 
     * @param obj
     * @param att
     * @param value
     * @param type
     */
    public static void setter(Object obj, String att, Object value,
            Class<?> type) {
        try {
            Method met = obj.getClass().getMethod("set" + initStr(att), type);
            met.invoke(obj, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用getter方法
     * 
     * @param obj
     * @param att
     */
    public static void getter(Object obj, String att) {
        try {
            Method met = obj.getClass().getMethod("get" + initStr(att));
            System.out.println(met.invoke(obj));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将单词的首字母大写
     * 
     * @param old
     * @return
     */
    public static String initStr(String old) {
        String str = old.substring(0, 1).toUpperCase() + old.substring(1);
        return str;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值