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:com.kangyonggan.api.common.util.Reflections.java

/**
 * , private/protected, ??setter.//from w  ww .java 2s.  co m
 */
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
    Field field = getAccessibleField(obj, fieldName);

    if (field == null) {
        throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
    }

    try {
        field.set(obj, value);
    } catch (IllegalAccessException e) {
        log.error("??:{}", e.getMessage());
    }
}

From source file:edu.mayo.cts2.framework.model.util.ModelUtils.java

public static ChangeableResource createChangeableResource(IsChangeable changeable) {
    ChangeableResource choice = new ChangeableResource();

    for (Field field : choice.getClass().getDeclaredFields()) {
        if (field.getType().equals(changeable.getClass()) || field.getName().equals("_choiceValue")) {
            field.setAccessible(true);/* w w  w  . java  2  s .  c  o m*/
            try {
                field.set(choice, changeable);
            } catch (IllegalArgumentException e) {
                throw new IllegalStateException(e);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
        }
    }

    return choice;
}

From source file:fi.foyt.foursquare.api.JSONFieldParser.java

/**
 * Sets entity's field's value/*from  w w w. j  av a  2 s.  c  o m*/
 * 
 * @param entity entity
 * @param fieldName field
 * @param value value
 * @throws FoursquareApiException when something unexpected happens
 */
private static void setEntityFieldValue(FoursquareEntity entity, String fieldName, Object value)
        throws FoursquareApiException {
    java.lang.reflect.Field fieldProperty;
    try {
        fieldProperty = getField(entity.getClass(), fieldName);
        fieldProperty.setAccessible(true);
        fieldProperty.set(entity, value);
    } catch (Exception e) {
        throw new FoursquareApiException(e);
    }
}

From source file:com.haulmont.cuba.core.entity.BaseEntityInternalAccess.java

public static void setValue(Entity entity, String attribute, @Nullable Object value) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    Field field = FieldUtils.getField(entity.getClass(), attribute, true);
    if (field == null)
        throw new RuntimeException(
                String.format("Cannot find field '%s' in class %s", attribute, entity.getClass().getName()));
    try {//from  ww w  . j av a  2 s  .  c  o  m
        field.set(entity, value);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(
                String.format("Unable to set value to %s.%s", entity.getClass().getSimpleName(), attribute), e);
    }
}

From source file:com.xyz.util.ReflectionUtil.java

/**
 * ,private/protected,??setter./*ww w. j av a2  s .c o  m*/
 */
public static void setFieldValue(final Object object, final String fieldName, final Object value) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null)
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");

    makeAccessible(field);

    try {
        field.set(object, value);
    } catch (IllegalAccessException e) {
        logger.error("??:" + e.getMessage());
    }
}

From source file:ReflectionUtils.java

/**
 * ,private/protected,setter.//www .  java  2s.c  o  m
 */
public static void setFieldValue(final Object object, final String fieldName, final Object value) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null)
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");

    makeAccessible(field);

    try {
        field.set(object, value);
    } catch (IllegalAccessException e) {
        logger.error(":{}", e);
    }
}

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Sets the application name. There is no API for this (See
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6528430) so this
 * method uses reflection to do this. This may fail if the Java
 * implementation is changed but any exception here will be ignored.
 * //  w  w  w  . j  a  va  2s  . co  m
 * The application name is currently only used for X11 desktops and only
 * important for some window managers like Gnome Shell.
 * 
 * @param appName
 *            The application name to set.
 */
public static void setAppName(final String appName) {
    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    final Class<?> cls = toolkit.getClass();

    try {
        // When X11 toolkit is used then set the awtAppClassName field
        if (cls.getName().equals("sun.awt.X11.XToolkit")) {
            final Field field = cls.getDeclaredField("awtAppClassName");
            field.setAccessible(true);
            field.set(toolkit, appName);
        }
    } catch (final Exception e) {
        LOG.warn("Unable to set application name: " + e, e);
    }
}

From source file:com.zfsoft.util.reflect.ReflectionUtils.java

/**
 * , private/protected, ??setter./*from w  w w. j  a  v  a 2s .c om*/
 */
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
    Field field = getAccessibleField(obj, fieldName);

    if (field == null) {
        throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
    }

    try {
        field.set(obj, value);
    } catch (IllegalAccessException e) {
        logger.error(e);
    }
}

From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java

public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception {
    Class searchClass = input.getClass();

    while (searchClass != null) {
        Field[] fields = searchClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName)) {
                field.setAccessible(true);
                //Check to see if we're trying to set a int, long, etc with a String
                if (field.getType().getName().equals("java.lang.Long")) {
                    if (value instanceof String) {
                        field.set(input, Long.getLong((String) value));
                    } else {
                        field.set(input, value);
                    }//  w w  w.j  a  va  2  s  . c o  m
                } else if (field.getType().getName().equals("java.lang.Integer")) {
                    if (value instanceof String) {
                        field.set(input, Integer.getInteger((String) value));
                    } else {
                        field.set(input, value);
                    }
                } else {
                    field.set(input, value);
                }
            }
        }
        searchClass = searchClass.getSuperclass();
    }
    return input;
}

From source file:com.ms.commons.lang.ObjectUtils.java

/**
 * string?trim?/* ww w .j  a v a  2 s  .c  om*/
 * 
 * @param object
 * @throws Exception
 */
private static void trimStringField(Object object, Class<?> clazz) throws Exception {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.getType() == String.class) {
            boolean isFoolback = false;
            if (field.isAccessible() == false) {
                isFoolback = true;
                field.setAccessible(true);
            }
            String value = (String) field.get(object);
            if (StringUtils.isNotEmpty(value)) {
                value = value.trim();
                field.set(object, value);
            }
            if (isFoolback) {
                field.setAccessible(false);
            }
        }
    }
}