博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 反射: 当Timestamp类型的属性值为null时,设置默认值
阅读量:4358 次
发布时间:2019-06-07

本文共 3011 字,大约阅读时间需要 10 分钟。

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; }}

 

转载于:https://www.cnblogs.com/qqzy168/p/3798239.html

你可能感兴趣的文章
【bzoj2002】弹飞绵羊——分块
查看>>
php读取数据库数据,出现中文乱码(数据库中没有出现乱码)
查看>>
selenium动作链
查看>>
敏捷外包工程系列之二:人员结构(敏捷外包工程,敏捷开发,产品负责人,客户价值)...
查看>>
《设计你的人生》的部分经典语录
查看>>
mustache多次渲染和多个赋值
查看>>
《Flutter 实战》开源电子书
查看>>
Python 键盘记录
查看>>
HDU 1381 Crazy Search
查看>>
NYOJ 311 完全背包
查看>>
几本书
查看>>
PLSQL
查看>>
修改计算机名
查看>>
Android-Activity的启动模式
查看>>
禅道项目管理系统整合Selenium IDE的思路
查看>>
网页数据交互!有很多可能不完善希望能提出来
查看>>
自家用的java小总结(2.4):类的知识的查漏补缺(内部类)
查看>>
Linux重定向与管道
查看>>
string.indexOf()和$.inArray()查找
查看>>
lamp :在Linux 下搭建apache、Mysql、php
查看>>