Example usage for java.lang.reflect Field set

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the field represented by this Field object on the specified object argument to the specified new value.

Usage

From source file:Main.java

public static void setField(Class cls, String name, Object obj, Object value) {
    try {/*from w  ww .j a va 2 s. c  om*/
        Field nameField = cls.getDeclaredField(name);
        nameField.setAccessible(true);
        nameField.set(obj, value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void setField(Class type, String name, Object instance, Object value)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = type.getDeclaredField(name);
    field.setAccessible(true);/*from   w  w  w . j  a v  a 2s  . c  o  m*/
    field.set(instance, value);
}

From source file:Main.java

public static void setFieldValue(Object object, Field field, Object value) {
    try {/*from  www.  ja  va  2 s .c om*/
        setAccessibleIfNeeded(field);
        field.set(object, value);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void injectLayoutInflater(LayoutInflater layoutInflater, Object src, Class clz, String name) {
    try {//from   ww w .  j  a  va 2s. com
        Field field = clz.getDeclaredField(name);
        field.setAccessible(true);
        field.set(src, layoutInflater);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void fieldSet(Object object, String fieldName, Object value) throws Exception {
    Field field = object.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);//from  w ww .j  a  va2  s  .co  m
    field.set(object, value);
}

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);//from w  w w  . j ava  2  s. c o m
    field.set(object, value);
}

From source file:Main.java

public static void setValue(Field field, Object object, Object value) throws IllegalAccessException {
    field.setAccessible(true);/*from w  ww.j  a va 2s . c  om*/
    field.set(object, value);
}

From source file:Main.java

private static void setField(Object obj, Class<?> cls, String field, Object value) {
    try {//from www .  j  a v a2  s.  c  o m
        Field localField = cls.getDeclaredField(field);
        localField.setAccessible(true);
        localField.set(obj, value);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setProperty(Object o, String field, Object value) {
    try {/*  w ww  . jav a2  s  . c o 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
 * //from  ww  w  .j  a va  2s .  com
 * @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);
}