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:net.minecraftforge.fml.relauncher.ReflectionHelper.java

public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, int fieldIndex) {
    try {/*from  w  w  w . j  av  a  2  s. c  o  m*/
        Field f = classToAccess.getDeclaredFields()[fieldIndex];
        f.setAccessible(true);
        f.set(instance, value);
    } catch (Exception e) {
        throw new UnableToAccessFieldException(new String[0], e);
    }
}

From source file:com.aw.support.reflection.AttributeAccessor.java

/**
 * @param target//w w w  . jav  a2 s .  co  m
 * @param attrName
 * @return
 */
public static void set(Object target, String attrName, Object value) {
    Field field = null;
    try {
        Class cls = target.getClass();
        field = getField(cls, attrName);
        field.setAccessible(true);
        field.set(target, value);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Error setting the value of the attribute:<" + attrName
                + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <"
                + value.getClass() + ">");
    } catch (Throwable e) {
        e.printStackTrace();
        throw new IllegalStateException(
                "Error setting the value of the attribute:<" + attrName + "> of object:<" + target + ">");
    }
}

From source file:de.taimos.dvalin.mongo.Tester.java

@BeforeClass
public static void init() {
    try {/*from w  ww.j a  v a 2 s .  c o  m*/
        Field mongoField = AbstractMongoDAO.class.getDeclaredField("mongo");
        mongoField.setAccessible(true);
        mongoField.set(Tester.dao, ABaseTest.mongo);

        Field jongoField = AbstractMongoDAO.class.getDeclaredField("jongo");
        jongoField.setAccessible(true);
        jongoField.set(Tester.dao, JongoFactory.createDefault(ABaseTest.mongo.getDB(ABaseTest.dbName)));

        Field dbField = AbstractMongoDAO.class.getDeclaredField("db");
        dbField.setAccessible(true);
        dbField.set(Tester.dao, ABaseTest.mongo.getDatabase(ABaseTest.dbName));

        Mongobee bee = new Mongobee(ABaseTest.mongo);
        bee.setChangeLogsScanPackage("de.taimos.dvalin.mongo.changelog");
        bee.setDbName(ABaseTest.dbName);
        bee.setEnabled(true);
        bee.execute();
        Tester.dao.init();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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  w w w .j av  a2  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:com.npower.dm.util.BeanHelper.java

/**
 * private/protected//w  w  w . j a v a 2 s. c om
 */
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:Main.java

/**
 * Sets the given field's value of the specified object instance.
 * /*ww  w.j  a v  a  2s.co m*/
 * @throws IllegalArgumentException if the value cannot be set.
 */
public static void setValue(final Field field, final Object instance, final Object value) {
    try {
        field.setAccessible(true);
        field.set(instance, convert(value, field.getType()));
    } catch (final IllegalAccessException e) {
        throw new IllegalArgumentException("No access to field: " + field.getName(), e);
    }
}

From source file:Main.java

/**
 * This method will iterate over all the fields on an object searching for declared fields defined as
 * Collection, List, Set, Map type.  If those fields are null they will be set to empty unmodifiable
 * instances.  If those fields are not null then it will force them to be unmodifiable.
 *
 * This method does not handle nested types.
 *
 * @param o the object to modify.  a null object will do nothing.
 *///from   www. j a v a  2 s . c o m
public static void makeUnmodifiableAndNullSafe(Object o) throws IllegalAccessException {
    if (o == null) {
        return;
    }

    Class<?> targetClass = o.getClass();
    for (Field f : targetClass.getDeclaredFields()) {
        f.setAccessible(true);
        try {
            if (f.getType().isAssignableFrom(List.class)) {
                f.set(o, unmodifiableListNullSafe((List<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Set.class)) {
                f.set(o, unmodifiableSetNullSafe((Set<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Collection.class)) {
                f.set(o, unmodifiableCollectionNullSafe((Collection<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Map.class)) {
                f.set(o, unmodifiableMapNullSafe((Map<?, ?>) f.get(o)));
            }
        } finally {
            f.setAccessible(false);
        }
    }
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

private static boolean checkKeynodesNumberPatternURI(SCSession session, Class<?> klass, Field field)
        throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
    KeynodesNumberPatternURI patternURI = field.getAnnotation(KeynodesNumberPatternURI.class);

    if (patternURI != null) {
        String[] comp = URIUtils.splitByIdtf(patternURI.patternURI());

        SCSegment segment = session.openSegment(comp[0]);

        List<SCAddr> keynodes = new LinkedList<SCAddr>();
        for (int i = patternURI.startIndex(); i <= patternURI.endIndex(); ++i) {
            String keynodeName = MessageFormat.format(comp[1], i);

            SCAddr keynode = session.findByIdtf(keynodeName, segment);
            Validate.notNull(keynode, keynodeName);

            keynodes.add(keynode);/*  w w w  .  ja va2s  .  co m*/

            String fieldName = MessageFormat.format(patternURI.patternName(), i);
            Field keynodeField = klass.getField(fieldName);
            keynodeField.set(null, keynode);

            if (log.isDebugEnabled())
                log.debug(comp[0] + "/" + keynodeName + " --> " + keynodeField.getName());
        }

        field.set(null, (SCAddr[]) keynodes.toArray(new SCAddr[keynodes.size()]));

        if (log.isDebugEnabled())
            log.debug(patternURI.patternURI() + " --> " + field.getName());

        return true;
    } else {
        return false;
    }
}

From source file:de.Keyle.MyPet.util.Util.java

public static boolean setFieldValue(Field field, Object object, Object value) {
    try {//from   w  w  w .j a v a 2s  .  c  o m
        field.set(object, value);
        return true;
    } catch (IllegalAccessException e) {
        return false;
    }
}

From source file:Main.java

public static void setField(String paramString, Object paramObject1, Object paramObject2) throws Exception {
    if (TextUtils.isEmpty(paramString))
        throw new RuntimeException("field name can not be empty");
    if (paramObject1 == null)
        throw new RuntimeException("target object can not be null");
    Field localField = paramObject1.getClass().getDeclaredField(paramString);
    if (localField == null)
        throw new RuntimeException("target object: " + paramObject1.getClass().getName()
                + " do not have this field: " + paramString);
    localField.setAccessible(true);/*from w w w  . j a  v a 2  s .  c  om*/
    localField.set(paramObject1, paramObject2);
}