Example usage for java.lang.reflect Field isAccessible

List of usage examples for java.lang.reflect Field isAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field isAccessible.

Prototype

@Deprecated(since = "9")
public boolean isAccessible() 

Source Link

Document

Get the value of the accessible flag for this reflected object.

Usage

From source file:Main.java

public static void setAccessibleIfNeeded(Field field) {
    if (!field.isAccessible()) {
        field.setAccessible(true);//from w  w w. j  a  va  2 s . c  om
    }
}

From source file:Main.java

public static Object invokeField(Class<?> cls, Object object, String fieldName) throws Exception {
    Field field = cls.getDeclaredField(fieldName);

    boolean accessible = field.isAccessible();
    try {//ww w. j  a va  2  s. c o  m
        field.setAccessible(true);
        return field.get(object);
    } finally {
        field.setAccessible(accessible);
    }
}

From source file:Main.java

private static void setField(final Object bean, final Field field, final Object value) throws Exception {
    if (!field.isAccessible()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                field.setAccessible(true);
                return null;
            }/*  ww  w. j  av a2s  .  c o  m*/
        });
    }
    field.set(bean, value);
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Object getFieldObject(Object target, Class clazz, String fieldName) {
    try {/*from   w  w  w  .j  a v a2 s. c o  m*/
        Field field = clazz.getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        return field.get(target);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

/**
 * Locates a given field anywhere in the class inheritance hierarchy.
 *
 * @param instance an object to search the field into.
 * @param name field name//from   ww  w.  j  a v  a 2 s  .  co  m
 * @return a field object
 * @throws NoSuchFieldException if the field cannot be located
 */
static Field findField(Object instance, String name) throws NoSuchFieldException {
    for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {
            Field field = clazz.getDeclaredField(name);

            if (!field.isAccessible()) {
                field.setAccessible(true);
            }

            return field;
        } catch (NoSuchFieldException e) {
            // ignore and search next
        }
    }

    throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}

From source file:cn.edu.zjnu.acm.judge.SystemOutCharset.java

private static Object get(Field field, Object o) throws Exception {
    if (!field.isAccessible()) {
        field.setAccessible(true);/*from   ww w . ja v a2s.  c  o  m*/
    }
    return field.get(o);
}

From source file:Main.java

public static void setField(Object value, Field field, Object target) {
    boolean accessible = field.isAccessible();
    field.setAccessible(true);/*  w  w w  . j a  va 2  s.  co m*/
    Object result = null;
    try {
        field.set(target, value);
    } catch (IllegalAccessException e) {
        // ignore
    }
    field.setAccessible(accessible);
}

From source file:Main.java

private static void registerFieldSetter(final Class<?> iClass, String fieldName, Field f) {
    // TRY TO GET THE VALUE BY ACCESSING DIRECTLY TO THE PROPERTY
    if (!f.isAccessible())
        f.setAccessible(true);/*  w  ww .j a  v a  2 s .  c om*/

    setters.put(iClass.getName() + "." + fieldName, f);
}

From source file:Main.java

public static Object getField(Field field, Object source) {
    boolean accessible = field.isAccessible();
    field.setAccessible(true);//from   w  w w. j  av  a2s . c  om
    Object result = null;
    try {
        result = field.get(source);
    } catch (IllegalAccessException e) {
        // ignore
    }
    field.setAccessible(accessible);
    return result;
}

From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java

/**
 * Get system ProcessID of specified process. <p/>
 * Currently it method work only on unix systems.
 * @param process//from  ww  w. j  ava  2  s.c  o m
 * @return PID or -1 on fail
 */
public static int getPid(Process process) {
    Class<? extends Process> clazz = process.getClass();
    try {
        Field field = clazz.getDeclaredField("pid");
        if (field.isAccessible()) {
            field.setAccessible(true);
        }
        return (int) field.get(process);
    } catch (IllegalAccessException | NoSuchFieldException e) {
        return -1;
    }
}