集册 Java实例教程 使字段无效

使字段无效

欢马劈雪     最近更新时间:2020-01-02 10:19:05

506
使字段无效
/**
n o w    j a v a  . c o m
**/


//package com.nowjava;


import java.lang.reflect.Field;


public class Main {

    public static <E> void nullifyField(final E entity, final Field field)

            throws Exception {

        if (field.getType().isEnum() || field.getType().isPrimitive()) {

            return;

        }

        field.setAccessible(true);

        field.set(entity, null);

    }/** 来 自 N o w J a v a . c o m**/

}