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:candr.yoclip.option.OptionField.java

protected void setIntegerOption(final T bean, final String value) {

    final Integer integerValue = StringUtils.isEmpty(value) ? 0 : Integer.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, integerValue);
        }//from w  ww  .  ja  v  a2s. co m
    });
}

From source file:candr.yoclip.option.OptionField.java

protected void setLongOption(final T bean, final String value) {

    final Long longValue = StringUtils.isEmpty(value) ? 0L : Long.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, longValue);
        }/*  w w  w .ja  v  a2  s.  co m*/
    });
}

From source file:candr.yoclip.option.OptionField.java

protected void setFloatOption(final T bean, final String value) {

    final Float floatValue = StringUtils.isEmpty(value) ? 0.0f : Float.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, floatValue);
        }/*  w w  w . j  a va 2s.  c o m*/
    });
}

From source file:candr.yoclip.option.OptionField.java

protected void setDoubleOption(final T bean, final String value) {

    final Double doubleValue = StringUtils.isEmpty(value) ? 0.0 : Double.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, doubleValue);
        }//  w  w  w  . j a v  a 2  s. c  om
    });
}

From source file:de.systemoutprintln.util.logging.spring.LogPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {

        @Override/*  w  w  w.  java 2  s. c o m*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            if (field.getAnnotation(Log.class) != null) {
                Logger logger = LoggerFactory.getLogger(bean.getClass());
                field.set(bean, logger);
            }
        }
    });

    return bean;
}

From source file:com.db2eshop.util.orm.TableValueEntityResolver.java

/**
 * <p>setValue.</p>/*from  w w w. java  2  s .  c  o m*/
 *
 * @param target a T object.
 * @param <T> a T object.
 * @param propertyName a {@link java.lang.String} object.
 * @param property a {@link java.lang.Object} object.
 * @return a T object.
 */
public <T> T setValue(String propertyName, Object property, T target) {
    Field field;
    try {
        field = target.getClass().getDeclaredField(propertyName);
        field.setAccessible(true);
        field.set(target, box(property, field.getType()));
        return target;
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloud.api.ApiServletTest.java

@Before
public void setup() throws SecurityException, NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException, IOException {
    servlet = new ApiServlet();
    responseWriter = new StringWriter();
    Mockito.when(response.getWriter()).thenReturn(new PrintWriter(responseWriter));
    Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1");
    Mockito.when(accountService.getSystemUser()).thenReturn(user);
    Mockito.when(accountService.getSystemAccount()).thenReturn(account);
    Field accountMgrField = ApiServlet.class.getDeclaredField("_accountMgr");
    accountMgrField.setAccessible(true);
    accountMgrField.set(servlet, accountService);

    Field apiServerField = ApiServlet.class.getDeclaredField("_apiServer");
    apiServerField.setAccessible(true);/*from   w ww  . j  av a2 s  .c  o  m*/
    apiServerField.set(servlet, apiServer);
}

From source file:at.ac.tuwien.infosys.jcloudscale.test.integration.base.TestFieldHandling.java

@Test
public void testPrivateFieldSetViaReflection() throws Exception {

    assertEquals(0, cs.countCloudObjects());
    FieldCloudObject ob = new FieldCloudObject();
    assertEquals(1, cs.countCloudObjects());

    // we need to go to the superclass as this is in fact a dynamic proxy generated
    // by CGLib/* www  . jav a 2 s  .co m*/
    Field field = ob.getClass().getSuperclass().getDeclaredField("privateField");
    ReflectionUtils.makeAccessible(field);
    field.set(ob, "private_changed");

    assertEquals(ob.getPrivateField(), "private_changed");

}

From source file:candr.yoclip.option.OptionField.java

protected void setCharacterOption(final T bean, final String value) {

    final boolean empty = StringUtils.isEmpty(value);
    if (!empty && value.length() > 1) {
        final String error = String.format("%s is a character and cannot accept '%s'", getField().getName(),
                value);/*from  ww  w  . jav  a  2  s  .com*/
        throw new OptionsParseException(error);
    }

    final Character charValue = empty ? '\u0000' : value.charAt(0);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, charValue);
        }
    });
}

From source file:com.aestheticsw.jobkeywords.shared.config.LogInjector.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String name) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            // make the field accessible if defined private
            ReflectionUtils.makeAccessible(field);
            if (field.getAnnotation(Log.class) != null) {
                Logger log = LoggerFactory.getLogger(bean.getClass());
                field.set(bean, log);
            }/*from w  ww  .  j  a  v a2  s . c  o  m*/
        }
    });
    return bean;
}