自测可用的反射自定义set/get方法,

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class PropertyUtil {

    private static final String SET_PREFIX = "set";
    private static final String IS_PREFIX = "is";
    private static final String GET_PREFIX = "get";

    public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) {//根据需求,定制 自己的get和set方法
        Method setMethod = null;
        Method getMethod = null;
        PropertyDescriptor pd = null;
        try {
            Field field = clazz.getDeclaredField(propertyName);// 根据字段名来获取字段
            if (field != null) {
                // 构建方法的后缀
                String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
                setMethod = clazz.getDeclaredMethod(SET_PREFIX + methodEnd, new Class[] { field.getType() });
                // 构建get 方法
                getMethod = clazz.getDeclaredMethod(GET_PREFIX + methodEnd, new Class[] {});
                // 构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
                pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return pd;
    }

    public static PropertyDescriptor getPropertyDescriptor2(Class<?> clazz, String propertyName) {//使用 PropertyDescriptor 提供的 get和set方法
        try {
            return new PropertyDescriptor(propertyName, clazz);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setProperty(Object obj, String propertyName, Object value) {
        Class<?> clazz = obj.getClass();// 获取对象的类型
        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 获取
                                                                            // clazz
                                                                            // 类型中的
                                                                            // propertyName
                                                                            // 的属性描述器
        Method setMethod = pd.getWriteMethod();// 从属性描述器中获取 set 方法
        try {
            setMethod.invoke(obj, new Object[] { value });// 调用 set
                                                            // 方法将传入的value值保存属性中去
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getProperty(Object obj, String propertyName) {
        Class<?> clazz = obj.getClass();// 获取对象的类型
        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 获取
                                                                            // clazz
                                                                            // 类型中的
                                                                            // propertyName
                                                                            // 的属性描述器
        Method getMethod = pd.getReadMethod();// 从属性描述器中获取 get 方法
        Object value = null;
        try {
            value = getMethod.invoke(clazz, new Object[] {});// 调用方法获取方法的返回值
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;// 返回值
    }    
}


----------
我自己写的,仅供参考.get/set

public void get1332(Class class1) throws Exception {
        if (NullUtil.isNotNull(class1)) {
            Class<?> clz = class1.getClass();
            Field[] fields = clz.getDeclaredFields();
            for (Field field : fields) {
                Type type = field.getGenericType();
                String name = field.getName();
                if (type.toString().equals("class java.lang.String")) {
                    Method m = clz.getMethod("get" + getMethodName(name));
                    String val = (String) m.invoke(class1);
                    if (NullUtil.isNull(val)) {
                        Method setMethod = clz.getDeclaredMethod("set" + getMethodName(name), new Class[] { field.getType() });
                        m.setAccessible(true);
                        setMethod.invoke(class1, new Object[] { "" });
                    }
                } else if (type.toString().equals("class java.lang.Boolean")) {
                    Method m = clz.getMethod("get" + getMethodName(name));
                    Boolean val = (Boolean) m.invoke(class1);
                    if (NullUtil.isNull(val)) {
                        Method setMethod = clz.getDeclaredMethod("set" + getMethodName(name), new Class[] { field.getType() });
                        m.setAccessible(true);
                        setMethod.invoke(class1, new Object[] { false });
                    }
                } else if (type.toString().equals("class java.lang.Long")) {
                    Method m = clz.getMethod("get" + getMethodName(name));
                    Long val = (Long) m.invoke(class1);
                    if (NullUtil.isNull(val)) {
                        Method setMethod = clz.getDeclaredMethod("set" + getMethodName(name), new Class[] { field.getType() });
                        m.setAccessible(true);
                        setMethod.invoke(class1, new Object[] { 0L });
                    }
                }

            }
        }   
        private static String getMethodName(String fildeName) throws Exception {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }

    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值