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:cz.cuni.mff.d3s.spl.example.newton.app.Main.java

private static void inspectClass(Object obj) {
    if (!INSPECT) {
        return;/*from   ww w  . java 2s . c o m*/
    }

    System.out.printf("Inspecting %s:\n", obj);
    Class<?> klass = obj.getClass();
    System.out.printf("  Class: %s\n", klass.getName());
    for (Field f : klass.getDeclaredFields()) {
        Object value;
        boolean accessible = f.isAccessible();
        try {
            f.setAccessible(true);
            value = f.get(obj);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            value = String.format("<failed to read: %s>", e.getMessage());
        }
        f.setAccessible(accessible);
        System.out.printf("  Field %s: %s\n", f.getName(), value);
    }
    for (Method m : klass.getDeclaredMethods()) {
        System.out.printf("  Method %s\n", m.getName());
    }
    System.out.printf("-------\n");
    System.out.flush();
}

From source file:com.dianping.lion.util.BeanUtils.java

@SuppressWarnings("unchecked")
public static <T> T getDeclaredFieldValue(Object object, String propertyName) throws NoSuchFieldException {
    Assert.notNull(object);/*from  w  w w.ja  v a  2 s .c  o m*/
    Assert.hasText(propertyName);

    Field field = getDeclaredField(object.getClass(), propertyName);

    boolean accessible = field.isAccessible();
    Object result = null;
    synchronized (field) {
        field.setAccessible(true);
        try {
            result = field.get(object);
        } catch (IllegalAccessException e) {
            throw new NoSuchFieldException("No such field: " + object.getClass() + '.' + propertyName);
        } finally {
            field.setAccessible(accessible);
        }
    }
    return (T) result;
}

From source file:Main.java

/**
 * Called internally by installGtkPopupBugWorkaround to fix the thickness
 * of a GTK style field by setting it to a minimum value of 1.
 * //from   www  .  j  ava2  s . c om
 * @param style
 *            The GTK style object.
 * @param fieldName
 *            The field name.
 * @throws Exception
 *             When reflection fails.
 */
private static void fixGtkThickness(Object style, String fieldName) throws Exception {
    Field field = style.getClass().getDeclaredField(fieldName);
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    field.setInt(style, Math.max(1, field.getInt(style)));
    field.setAccessible(accessible);
}

From source file:Main.java

private static Object getFieldValueSafely(Field field, Object classInstance)
        throws IllegalArgumentException, IllegalAccessException {
    boolean oldAccessibleValue = field.isAccessible();
    field.setAccessible(true);/*w  ww  . j a  v  a 2 s . c  o m*/
    Object result = field.get(classInstance);
    field.setAccessible(oldAccessibleValue);
    return result;
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

/**
 * Utility method to read a field value. If the field is not accessible, it will be set to be accessible.
 * @param object Instance in which the value should be read
 * @param name Name of the field who's value should be read
 * @return The value of the field/*ww  w .  j av a2 s . com*/
 */
public static Object getFieldValue(Object object, String name) {
    try {
        Field field = getField(object.getClass(), name);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        return field.get(object);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException(
                "Could not read field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

/**
 * Utility method to set a field to a value. If the field is not accessible, it will be set to be accessible.
 * @param object Instance in which the value should be set
 * @param name Name of the field who's value should be set
 * @param value The value to be set//from ww w  .  ja v a 2 s  .co  m
 */
public static void setFieldValue(Object object, String name, Object value) {
    try {
        Field field = getField(object.getClass(), name);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        field.set(object, value);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException(
                "Could not set field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$
    }

}

From source file:Main.java

/**
 * Swing menus are looking pretty bad on Linux when the GTK LaF is used (See
 * bug #6925412). It will most likely never be fixed anytime soon so this
 * method provides a workaround for it. It uses reflection to change the GTK
 * style objects of Swing so popup menu borders have a minimum thickness of
 * 1 and menu separators have a minimum vertical thickness of 1.
 *//*w w w .  java2s  . c  o  m*/
public static void installGtkPopupBugWorkaround() {
    // Get current look-and-feel implementation class
    LookAndFeel laf = UIManager.getLookAndFeel();
    Class<?> lafClass = laf.getClass();

    // Do nothing when not using the problematic LaF
    if (!lafClass.getName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"))
        return;

    // We do reflection from here on. Failure is silently ignored. The
    // workaround is simply not installed when something goes wrong here
    try {
        // Access the GTK style factory
        Field field = lafClass.getDeclaredField("styleFactory");
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        Object styleFactory = field.get(laf);
        field.setAccessible(accessible);

        // Fix the horizontal and vertical thickness of popup menu style
        Object style = getGtkStyle(styleFactory, new JPopupMenu(), "POPUP_MENU");
        fixGtkThickness(style, "yThickness");
        fixGtkThickness(style, "xThickness");

        // Fix the vertical thickness of the popup menu separator style
        style = getGtkStyle(styleFactory, new JSeparator(), "POPUP_MENU_SEPARATOR");
        fixGtkThickness(style, "yThickness");
    } catch (Exception e) {
        // Silently ignored. Workaround can't be applied.
    }
}

From source file:com.vivekpanyam.evolve.Utils.java

private static Field getField(Class<?> cls, String name) {
    for (Field field : cls.getDeclaredFields()) {
        if (!field.isAccessible()) {
            field.setAccessible(true);/*from   www.j  av  a 2  s.  co m*/
        }
        if (field.getName().equals(name)) {
            return field;
        }
    }
    return null;
}

From source file:com.npower.dm.util.BeanHelper.java

/**
 * private/protected/* w  ww .ja v  a2s.  c  o  m*/
 */
static public void setDeclaredProperty(Object object, Field field, Object newValue)
        throws IllegalAccessException {
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    field.set(object, newValue);
    field.setAccessible(accessible);
}

From source file:org.mqnaas.core.impl.CapabilityInstance.java

/**
 * Injects resource in each field of given capability instance (including his superclasses).
 *///www  .  j  a  v a2 s  .  co  m
private static void injectResourceToCapability(ICapability capability, IResource resource)
        throws IllegalArgumentException, IllegalAccessException {
    Class<? extends ICapability> capabilityClass = capability.getClass();
    List<Field> resourceFields = ReflectionUtils.getAnnotationFields(capabilityClass,
            org.mqnaas.core.api.annotations.Resource.class);
    for (Field resourceField : resourceFields) {
        if (!resourceField.isAccessible()) {
            resourceField.setAccessible(true);
        }
        resourceField.set(capability, resource);
    }
}