Example usage for org.springframework.util ReflectionUtils findField

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

Introduction

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

Prototype

@Nullable
public static Field findField(Class<?> clazz, String name) 

Source Link

Document

Attempt to find a Field field on the supplied Class with the supplied name .

Usage

From source file:dwalldorf.jadecr.converter.PropertyConverter.java

/**
 * Will search for all fields that exist equally in both, {@code src} and {@code dest}, and set copy the value from
 * {@code src} to {@code dest}./* w ww . j  a  va 2s  . co m*/
 *
 * @param src  the object to be copied
 * @param dest the object to copy into
 * @throws IllegalAccessException
 */
private void copyValues(final Object src, final Object dest) throws IllegalAccessException {
    ReflectionUtils.doWithFields(src.getClass(), field -> {
        Field destField = ReflectionUtils.findField(dest.getClass(), field.getName());

        if (destField == null) {
            return;
        }
        ReflectionUtils.makeAccessible(field);
        ReflectionUtils.makeAccessible(destField);
        Object value = field.get(src);

        setValue(value, destField, dest);
    });
}

From source file:com.ciphertool.sentencebuilder.dao.UniqueWordListDaoTest.java

@Test
@SuppressWarnings("unchecked")
public void testConstructor() {
    WordDao wordDaoMock = mock(WordDao.class);
    when(wordDaoMock.findAllUniqueWords()).thenReturn(wordsToReturn);

    UniqueWordListDao uniqueWordListDao = new UniqueWordListDao(wordDaoMock);

    Field wordListField = ReflectionUtils.findField(UniqueWordListDao.class, "wordList");
    ReflectionUtils.makeAccessible(wordListField);
    List<Word> wordListFromObject = (List<Word>) ReflectionUtils.getField(wordListField, uniqueWordListDao);

    assertEquals(3, wordListFromObject.size());
    assertTrue(wordListFromObject.containsAll(wordsToReturn));
    assertTrue(wordsToReturn.containsAll(wordListFromObject));
    verify(wordDaoMock, times(1)).findAllUniqueWords();
}

From source file:com.ciphertool.genetics.algorithms.crossover.ConservativeUnevaluatedCrossoverAlgorithmTest.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*from www .  j  a v  a 2  s.  c  om*/
public void testSetMutationAlgorithm() {
    MutationAlgorithm mutationAlgorithmToSet = mock(MutationAlgorithm.class);

    ConservativeUnevaluatedCrossoverAlgorithm conservativeUnevaluatedCrossoverAlgorithm = new ConservativeUnevaluatedCrossoverAlgorithm();
    conservativeUnevaluatedCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

    Field mutationAlgorithmField = ReflectionUtils.findField(ConservativeUnevaluatedCrossoverAlgorithm.class,
            "mutationAlgorithm");
    ReflectionUtils.makeAccessible(mutationAlgorithmField);
    MutationAlgorithm mutationAlgorithmFromObject = (MutationAlgorithm) ReflectionUtils
            .getField(mutationAlgorithmField, conservativeUnevaluatedCrossoverAlgorithm);

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}

From source file:py.una.pol.karaku.dao.entity.interceptors.AbstractInterceptor.java

/**
 * Retorna el valor de un field de un bean dado.
 * /*from  w ww . ja  va  2s . co m*/
 * <p>
 * Es til cuando se necesita obtener valores de otros fields del bean que
 * se esta interceptando.
 * </p>
 * 
 * @param bean
 *            del cual quitar el valor
 * @param field
 *            nombre del atributo
 * @return valor extrado
 */
protected Object getFieldValueOfBean(@Nonnull Object bean, @Nonnull String field) {

    Field unique = ReflectionUtils.findField(bean.getClass(), field);
    unique.setAccessible(true);
    Object uniqueColumn = ReflectionUtils.getField(unique, bean);
    unique.setAccessible(false);
    return uniqueColumn;
}

From source file:com.ciphertool.zodiacengine.entities.SolutionChromosomeTest.java

@BeforeClass
public static void setUp() {
    BigInteger cipherId = new BigInteger("12345");
    cipher.setId(cipherId);/* w w  w .  j a va  2s  .com*/

    logMock = mock(Logger.class);

    Field logField = ReflectionUtils.findField(SolutionChromosome.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, null, logMock);
}

From source file:com.ciphertool.sentencebuilder.dao.FrequencyWordListDaoTest.java

@Test
@SuppressWarnings("unchecked")
public void testConstructor() {
    WordDao wordDaoMock = mock(WordDao.class);
    when(wordDaoMock.findAllUniqueWords()).thenReturn(wordsToReturn);

    FrequencyWordListDao frequencyWordListDao = new FrequencyWordListDao(wordDaoMock);

    Field wordListField = ReflectionUtils.findField(FrequencyWordListDao.class, "wordList");
    ReflectionUtils.makeAccessible(wordListField);
    List<Word> wordListFromObject = (List<Word>) ReflectionUtils.getField(wordListField, frequencyWordListDao);

    assertEquals(7, wordListFromObject.size());
    assertTrue(wordListFromObject.containsAll(wordsToReturn));
    assertTrue(wordsToReturn.containsAll(wordListFromObject));
    assertEquals(5, countOccurrences(word1, wordListFromObject));
    assertEquals(1, countOccurrences(word2, wordListFromObject));
    assertEquals(1, countOccurrences(word3, wordListFromObject));
    verify(wordDaoMock, times(1)).findAllUniqueWords();
}

From source file:com.ciphertool.genetics.dao.ExecutionStatisticsDaoTest.java

@Test
public void testSetSessionFactory() {
    ExecutionStatisticsDao executionStatisticsDao = new ExecutionStatisticsDao();
    executionStatisticsDao.setSessionFactory(sessionFactoryMock);

    Field sessionFactoryField = ReflectionUtils.findField(ExecutionStatisticsDao.class, "sessionFactory");
    ReflectionUtils.makeAccessible(sessionFactoryField);
    SessionFactory sessionFactoryFromObject = (SessionFactory) ReflectionUtils.getField(sessionFactoryField,
            executionStatisticsDao);/*from   ww w .j av  a2  s. c  om*/

    assertSame(sessionFactoryMock, sessionFactoryFromObject);
}

From source file:com.couchbase.spring.core.mapping.BasicCouchbasePersistentPropertyTest.java

/**
 * Verifies the name of the property with custom name annotation.
 */// w w w.j  a va2 s. c  o m
@Test
public void usesAnnotatedFieldName() {
    Field field = ReflectionUtils.findField(Beer.class, "name");
    assertEquals("foobar", getPropertyFor(field).getFieldName());
}

From source file:es.logongas.ix3.util.ReflectionUtilTest.java

@Test
public void testGetAnnotationMethodIs() throws Exception {
    System.out.println("getAnnotation en un metodo is");
    TestAnnotation expResult = ReflectionUtils.findField(BeanTestC.class, "propBooleanReadOnly")
            .getAnnotation(TestAnnotation.class);
    TestAnnotation result = ReflectionUtil.getAnnotation(BeanTestC.class, "propBooleanReadOnly",
            TestAnnotation.class);
    assertEquals(expResult, result);/*  w  w  w .  j  a v a  2  s .  c  o  m*/
}

From source file:com.ciphertool.zodiacengine.dao.ForcedWordGeneDaoTest.java

@Test
public void testSetWordListDao() {
    ForcedWordGeneDao forcedWordGeneDao = new ForcedWordGeneDao();
    forcedWordGeneDao.setWordListDao(wordListDaoMock);

    Field wordListDaoField = ReflectionUtils.findField(ForcedWordGeneDao.class, "wordListDao");
    ReflectionUtils.makeAccessible(wordListDaoField);
    WordListDao wordListDaoFromObject = (WordListDao) ReflectionUtils.getField(wordListDaoField,
            forcedWordGeneDao);/*from ww  w .  j a va2s  . c o m*/

    assertSame(wordListDaoMock, wordListDaoFromObject);
}