反射让一个类及其超类的的字段全部为null--设置字段为null

package com.ipace.chaoJie.utils;



import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 该方法
 *不能设置父类的字段为null
 * */
public class MakeColumnNull0False<T> {

    public  T f(T o)  {
        /**
         ******下面一直到while*会得到该类及其父类的所有字段*********************************************************************************
         * */
        List<Field> fieldList=new ArrayList<>();
        Class<?> aClass = o.getClass();
        while (aClass != null) {//while得到所有超类的字段属性
            fieldList.addAll(Arrays.asList(aClass.getDeclaredFields()));
            aClass = aClass.getSuperclass(); //得到父类,然后赋给自己
        }
        /**
         *****上面会得到该类及其父类的所有字段***********************************************************************************
         * */
        for(Field field:fieldList){
            field.setAccessible(true);
            Class<?> type = field.getType();
            if("int".equals(type.getName())){
                try {
                    field.setInt(o,0);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }else if("double".equals(type.getName())){
                try {
                    field.setDouble(o,0d);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }
            else if("float".equals(type.getName())){
                try {
                    field.setFloat(o,0f);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }else if("long".equals(type.getName())){
                try {
                    field.setLong(o,0L);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }else if("short".equals(type.getName())){
                try {
                    field.setShort(o, (short) 0);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }else if("boolean".equals(type.getName())){
                try {
                    field.setBoolean(o,false);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }else{
                try {
                    field.set(o,null);
                } catch (IllegalAccessException e) {
                    System.out.println("该类的该字段类型为"+type.getName()+"无法转换为null或者0");
                }
            }

        }

        return o;
    }
    
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值