Example usage for org.springframework.util ReflectionUtils makeAccessible

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

Introduction

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

Prototype

@SuppressWarnings("deprecation") 
public static void makeAccessible(Field field) 

Source Link

Document

Make the given field accessible, explicitly setting it accessible if necessary.

Usage

From source file:org.sakuli.javaDSL.AbstractSakuliTest.java

private static Object getField(Object target, String name) {
    if (target != null) {
        Field field = ReflectionUtils.findField(target.getClass(), name);
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            return ReflectionUtils.getField(field, target);
        }/*from   w  ww.  j  av a  2  s.c  om*/
    }
    return null;
}

From source file:com.emergya.utils.DataSourceUtils.java

/**
 * Genera un nuevo data source con las propiedades
 * {@link DataSourceUtils#dataSourceProperties}
 * /*from  w  w  w.j  a  va 2s  .  c o m*/
 * @return {@link BasicDataSource}
 */
public DataSource getDataSource() {
    DataSource dataSource = new BasicDataSource();

    // Se escriben las propiedades por reflexion
    for (Object key : dataSourceProperties.keySet()) {
        if (key instanceof String) {
            try {
                String property = (String) key;
                String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
                Method setterMethod = ReflectionUtils.findMethod(BasicDataSource.class, setter, String.class);
                ReflectionUtils.makeAccessible(setterMethod);
                ReflectionUtils.invokeMethod(setterMethod, dataSource,
                        dataSourceProperties.getProperty(property));
            } catch (Exception e) {
                // Error al poner la propiedad
            }
        }
    }

    return dataSource;
}

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.zodiacengine.entities.SolutionChromosomeTest.java

@BeforeClass
public static void setUp() {
    BigInteger cipherId = new BigInteger("12345");
    cipher.setId(cipherId);//from   w  w w .  j  ava2  s.  co m

    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   www  .  j a  va2 s  .com*/

    assertSame(sessionFactoryMock, sessionFactoryFromObject);
}

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.genetics.algorithms.crossover.ConservativeUnevaluatedCrossoverAlgorithmTest.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test//from w w w  . j a v a  2 s  .c  o m
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: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  w w w  .  j a va 2 s.c  o  m

    assertSame(wordListDaoMock, wordListDaoFromObject);
}

From source file:kelly.core.injector.AbstractSpringInjector.java

@Override
public final void inject(Object bean) {
    if (bean == null)
        return;/* w  ww .  ja  v  a2 s.  co  m*/

    Field[] fields = getFieldsIncludingSuperclass(bean);

    for (Field field : fields) {
        Inject inject = field.getAnnotation(Inject.class);
        boolean nullable = field.getAnnotation(Nullable.class) != null;
        if (inject == null) {
            continue;
        } else {

            String beanName = inject.value();
            Object com = null;
            if ("".equals(beanName)) { // 
                com = getBeanFromApplicationContext(field.getType());
            } else {
                com = getBeanFromApplicationContext(beanName);
            }

            if (com == null) {
                if (nullable) {
                    continue;
                } else {
                    throw new InjectException("Inject failed for field " + field.toString());
                }
            }
            ReflectionUtils.makeAccessible(field);
            ReflectionUtils.setField(field, bean, com);
        }
    }
}