Example usage for org.springframework.util ReflectionUtils setField

List of usage examples for org.springframework.util ReflectionUtils setField

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils setField.

Prototype

public static void setField(Field field, @Nullable Object target, @Nullable Object value) 

Source Link

Document

Set the field represented by the supplied Field field object on the specified Object target object to the specified value .

Usage

From source file:com.github.javarch.persistence.orm.hibernate.listener.EntityDateRegister.java

public static void setCurrentDateOnFieldWithAnnotation(final Object obj,
        final Class<? extends Annotation> annotationClass) {
    try {//from w w w  . ja v a2 s  .  c  o m
        if (LOG.isDebugEnabled()) {
            LOG.debug("PreUpdateEvent - Em classe {}", obj.getClass().getName());
        }
        List<Field> campos = FieldAnnotationUtils.findFieldsWithAnnotation(obj.getClass(), annotationClass);
        for (Field campo : campos) {
            ReflectionUtils.makeAccessible(campo);
            ReflectionUtils.setField(campo, obj, new Date());
        }
    } catch (Exception e) {
        LOG.error("O seguinte erro ocorreu ao tentar setar new Date() em campo anotado com {} da classe {}: {}",
                annotationClass.getName(), obj.getClass().getName(), e.getMessage());
    }

}

From source file:br.com.modoagil.util.ReflectionUtil.java

/**
 * Seta em um atributo de uma classe o atributo com nome '{@code name}' o valor '{@code value}' no objeto desta instncia
 *
 * @param target// ww  w .  j  a  v  a2s .co  m
 *            objeto a ter o campo e valores setados
 * @param name
 *            nome do atributo a ser setado o contedo
 * @param value
 *            contedo a ser setado
 * @see ReflectionUtils#findField(Class, String)
 * @see ReflectionUtils#makeAccessible(Field)
 * @see ReflectionUtils#setField(Field, Object, Object)
 */
public static void setField(final Object target, final String name, final Object value) {
    final Field field = ReflectionUtils.findField(target.getClass(), name);
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, target, value);
}

From source file:com.consol.citrus.admin.mock.Mocks.java

/**
 * Inject Spring autowired fields in target instance with mocks.
 * @param target/*from w w w.  j  a v a  2 s. com*/
 */
public static void injectMocks(Object target) {
    ReflectionUtils.doWithFields(target.getClass(),
            field -> ReflectionUtils.setField(field, target, Mockito.mock(field.getType())), field -> {
                if (field.isAnnotationPresent(Autowired.class)) {
                    if (!field.isAccessible()) {
                        ReflectionUtils.makeAccessible(field);
                    }

                    return true;
                }

                return false;
            });
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.AlphaSelectorTest.java

@BeforeClass
public static void setUp() {
    alphaSelector = new AlphaSelector();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(AlphaSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, alphaSelector, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.RandomSelectorTest.java

@BeforeClass
public static void setUp() {
    randomSelector = new RandomSelector();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(RandomSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, randomSelector, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.RouletteSelectorTest.java

@BeforeClass
public static void setUp() {
    rouletteSelector = new RouletteSelector();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(RouletteSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, rouletteSelector, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.TournamentSelectorTest.java

@BeforeClass
public static void setUp() {
    tournamentSelector = new TournamentSelector();
    tournamentSelector.setSelectionAccuracy(0.9);

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(TournamentSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, tournamentSelector, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.ProbabilisticSelectionAlgorithmTest.java

@BeforeClass
public static void setUp() {
    probabilisticSelectionAlgorithm = new ProbabilisticSelectionAlgorithm();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(ProbabilisticSelectionAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, probabilisticSelectionAlgorithm, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.TruncationSelectionAlgorithmTest.java

@BeforeClass
public static void setUp() {
    truncationSelectionAlgorithm = new TruncationSelectionAlgorithm();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(TruncationSelectionAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, truncationSelectionAlgorithm, logMock);
}

From source file:org.red5.server.net.rtmp.codec.MuleRTMPMinaProtocolDecoder.java

public MuleRTMPMinaProtocolDecoder() {
    super();/*from  w ww.j  ava  2  s .  c  o m*/
    Field field = ReflectionUtils.findField(RTMPMinaProtocolDecoder.class, "decoder");
    field.setAccessible(true);
    ReflectionUtils.setField(field, this, new MuleRTMPProtocolDecoder());
}