Make a field accessible. - Java Reflection

Java examples for Reflection:Field

Description

Make a field accessible.

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;

public class Main {
    /**/*from   w w w.  ja  v a  2s . c  o  m*/
     * Make a field accessible.
     *
     * @param field the field
     */
    public static void setAccessible(final Field field) {
        if (Modifier.isPublic(field.getModifiers())) {
            return;
        }

        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                field.setAccessible(true);
                return field;
            }
        });
    }
}

Related Tutorials