Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static void setFieldValue(final Object object, final String name, final Object value) throws Exception {
    Field field = object.getClass().getDeclaredField(name);
    field.setAccessible(true);
    field.set(object, value);/*from w  w w.j  a va2  s .c om*/
}

From source file:Main.java

public static Object getFieldValue(final Object object, final String name) throws Exception {
    Field field = object.getClass().getDeclaredField(name);
    field.setAccessible(true);
    return field.get(object);
}

From source file:Main.java

public static void setValue(Field field, Object object, Object value) throws IllegalAccessException {
    field.setAccessible(true);
    field.set(object, value);/* w w  w. ja v  a2  s.  c  o m*/
}

From source file:Main.java

/**
 * Gets the given field's value of the specified object instance, or null if
 * the value cannot be obtained.//w  ww.j av a 2  s .c om
 */
public static Object getValue(final Field field, final Object instance) {
    try {
        field.setAccessible(true);
        return field.get(instance);
    } catch (final IllegalAccessException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Get the private field data value of the given instance of a class
 * //from w  w  w . ja v  a2  s  .  c  om
 * @param objectInstance Object to get private member data from
 * @param fieldName Name of private member to get data from
 * @return Private member's data
 * 
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static Object getPrivateField(Object objectInstance, String fieldName)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR
{
    Class<? extends Object> clazz = objectInstance.getClass();

    Field field = clazz.getDeclaredField(fieldName);

    field.setAccessible(true);

    return field.get(objectInstance);
}

From source file:Main.java

public static void setProperty(Object o, String field, Object value) {
    try {/*from   ww  w. j a v  a2s .  co m*/
        Field f = o.getClass().getDeclaredField(field);
        f.setAccessible(true);
        f.set(o, value);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Set the private field data value of the given instance of a class
 * /*w w w.ja va 2 s  .c  om*/
 * @param objectInstance Object to set private member data to
 * @param fieldName Name of private member to set data for
 * @param value Private member's data to set
 * 
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static void setPrivateField(Object objectInstance, String fieldName, Object value)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR
{
    Class<? extends Object> clazz = objectInstance.getClass();

    Field field = clazz.getDeclaredField(fieldName);

    field.setAccessible(true);

    field.set(objectInstance, value);
}

From source file:Main.java

private static final Field getFieldOrDie(Class<?> clazz, String fieldName) {
    try {//from   w ww  . j a v a  2  s . c om
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field;
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage());
    }
}

From source file:Main.java

private static Field scanField(Class<?> clazz, String... names) {
    for (String name : names) {
        try {//from www  . j a  va2 s.c om
            final Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {
        }
        try {
            return clazz.getField(name);
        } catch (NoSuchFieldException e) {
        }
    }
    return null;
}

From source file:Main.java

public static void setMember(Object object, String memberName, Object value)
        throws IllegalAccessException, NoSuchFieldException {
    Field declaredField = getField(object.getClass(), memberName);
    declaredField.setAccessible(true);
    declaredField.set(object, value);/*from  w w  w  .  j a va  2 s.  c  o m*/
}