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:com.ciphertool.zodiacengine.fitness.AbstractSolutionEvaluatorBaseTest.java

@Test
public void testClearHasMatchValues_NullSolution() {
    ConcreteSolutionEvaluatorBase abstractSolutionEvaluatorBase = new ConcreteSolutionEvaluatorBase();

    Field logField = ReflectionUtils.findField(ConcreteSolutionEvaluatorBase.class, "log");
    Logger mockLogger = mock(Logger.class);
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, abstractSolutionEvaluatorBase, mockLogger);

    abstractSolutionEvaluatorBase.clearHasMatchValues(null);

    verify(mockLogger, times(1))//from w w  w .  j  av  a 2  s  .  co m
            .warn("Attempted to clear hasMatch values, but the SolutionChromosome was null.  Returning early.");
}

From source file:org.terasoluna.gfw.common.codelist.JdbcCodeListTest.java

/**
 * In case LazyInit is set to true/*w  w w.  j av  a  2s .c om*/
 * @throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void testAfterPropertiesSet02() throws Exception {
    // create target
    JdbcCodeList jdbcCodeList = new JdbcCodeList();

    // setup parameters\
    jdbcCodeList.setDataSource(dataSource);
    jdbcCodeList.setLazyInit(true);
    jdbcCodeList.setLabelColumn("code_name");
    jdbcCodeList.setValueColumn("code_id");
    jdbcCodeList.setQuerySql("Select code_id, code_name from codelist");

    // fetch exposed map for the first time

    Field f = ReflectionUtils.findField(JdbcCodeList.class, "exposedMap");
    ReflectionUtils.makeAccessible(f);
    Map<String, String> exposedMapFirstFetch = (Map<String, String>) f.get(jdbcCodeList);

    // assert
    assertNull(exposedMapFirstFetch);

    // run
    jdbcCodeList.afterPropertiesSet();

    // assert again
    assertNull(exposedMapFirstFetch);
}

From source file:org.ff4j.spring.autowire.AutowiredFF4JBeanPostProcessor.java

private void injectValue(Field field, Object currentBean, String propName, Object propValue) {
    // Set as true for modifications
    ReflectionUtils.makeAccessible(field);
    // Update property
    ReflectionUtils.setField(field, currentBean, propValue);
    logger.debug("Injection of property '" + propName + "' on " + currentBean.getClass().getName() + "."
            + field.getName());/*  ww  w .  j  av  a2 s .  c o m*/
}

From source file:com.ciphertool.genetics.PopulationTest.java

@Test
public void testSetLifespan() {
    Population population = new Population();

    int lifespanToSet = 25;
    population.setLifespan(lifespanToSet);

    Field lifespanField = ReflectionUtils.findField(Population.class, "lifespan");
    ReflectionUtils.makeAccessible(lifespanField);
    int lifespanFromObject = (int) ReflectionUtils.getField(lifespanField, population);

    assertSame(lifespanToSet, lifespanFromObject);
}

From source file:nl.surfnet.coin.teams.control.AbstractControllerTest.java

private boolean doAutoWireMock(Object target, Object mock, Class interfaceClass, Field[] fields)
        throws IllegalAccessException {
    boolean found = false;
    for (Field field : fields) {
        if (field.getType().equals(interfaceClass)) {
            ReflectionUtils.makeAccessible(field);
            field.set(target, mock);/*from w  w  w  .  ja v  a2  s .  c  o  m*/
            found = true;
            break;
        }
    }
    return found;
}

From source file:com.ciphertool.zodiacengine.fitness.AbstractSolutionEvaluatorBaseTest.java

@SuppressWarnings("unchecked")
@Test//from w  w  w.  j ava  2s . c  o  m
public void testSetGeneticStructure() {
    ConcreteSolutionEvaluatorBase abstractSolutionEvaluatorBase = new ConcreteSolutionEvaluatorBase();

    abstractSolutionEvaluatorBase.setGeneticStructure(simpleCipher);

    Field cipherField = ReflectionUtils.findField(ConcreteSolutionEvaluatorBase.class, "cipher");
    ReflectionUtils.makeAccessible(cipherField);
    Cipher cipherFromObject = (Cipher) ReflectionUtils.getField(cipherField, abstractSolutionEvaluatorBase);

    assertEquals(simpleCipher, cipherFromObject);

    Field ciphertextKeyField = ReflectionUtils.findField(ConcreteSolutionEvaluatorBase.class, "ciphertextKey");
    ReflectionUtils.makeAccessible(ciphertextKeyField);
    HashMap<String, List<Ciphertext>> ciphertextKeyFromObject = (HashMap<String, List<Ciphertext>>) ReflectionUtils
            .getField(ciphertextKeyField, abstractSolutionEvaluatorBase);

    assertNotNull(ciphertextKeyFromObject);
}

From source file:com.ciphertool.genetics.PopulationTest.java

@Test
public void testSetKnownSolutionFitnessEvaluator() {
    Population population = new Population();

    FitnessEvaluator knownSolutionFitnessEvaluatorMock = mock(FitnessEvaluator.class);
    when(knownSolutionFitnessEvaluatorMock.evaluate(any(Chromosome.class))).thenReturn(DEFAULT_FITNESS_VALUE);
    population.setKnownSolutionFitnessEvaluator(knownSolutionFitnessEvaluatorMock);

    Field knownSolutionFitnessEvaluatorField = ReflectionUtils.findField(Population.class,
            "knownSolutionFitnessEvaluator");
    ReflectionUtils.makeAccessible(knownSolutionFitnessEvaluatorField);
    FitnessEvaluator knownSolutionFitnessEvaluatorFromObject = (FitnessEvaluator) ReflectionUtils
            .getField(knownSolutionFitnessEvaluatorField, population);

    assertSame(knownSolutionFitnessEvaluatorMock, knownSolutionFitnessEvaluatorFromObject);
}

From source file:se.vgregion.alfresco.repo.scripts.StorageContentGet.java

private void streamRendition(WebScriptRequest req, WebScriptResponse res, NodeRef nodeRef, String streamId,
        boolean attach) throws IOException {
    try {/* www . ja  va 2s. c  o  m*/
        final Method method = ReflectionUtils.findMethod(ContentGet.class, "streamRendition",
                WebScriptRequest.class, WebScriptResponse.class, NodeRef.class, String.class, Boolean.class);

        ReflectionUtils.makeAccessible(method);

        ReflectionUtils.invokeMethod(method, req, res, nodeRef, streamId, attach);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java

@Test
public void testImportWordList_LeftoversFromBatch() {
    ThreadPoolTaskExecutor taskExecutorSpy = spy(new ThreadPoolTaskExecutor());
    taskExecutorSpy.setCorePoolSize(4);/*from  w w  w.j av a 2s. c  o  m*/
    taskExecutorSpy.setMaxPoolSize(4);
    taskExecutorSpy.setQueueCapacity(100);
    taskExecutorSpy.setKeepAliveSeconds(1);
    taskExecutorSpy.setAllowCoreThreadTimeOut(true);
    taskExecutorSpy.initialize();

    WordListImporterImpl wordListImporterImpl = new WordListImporterImpl();
    wordListImporterImpl.setTaskExecutor(taskExecutorSpy);

    Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount");
    ReflectionUtils.makeAccessible(rowCountField);
    AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField,
            wordListImporterImpl);

    assertEquals(0, rowCountFromObject.intValue());

    WordDao wordDaoMock = mock(WordDao.class);
    when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true);
    int persistenceBatchSizeToSet = 3;
    int concurrencyBatchSizeToSet = 2;

    wordListImporterImpl.setWordDao(wordDaoMock);
    wordListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet);
    wordListImporterImpl.setConcurrencyBatchSize(concurrencyBatchSizeToSet);

    Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN));
    Word word2 = new Word(new WordId("elmer", PartOfSpeechType.NOUN));
    Word word3 = new Word(new WordId("belden", PartOfSpeechType.NOUN));
    List<Word> wordsToReturn = new ArrayList<Word>();
    wordsToReturn.add(word1);
    wordsToReturn.add(word2);
    wordsToReturn.add(word3);
    PartOfSpeechFileParser fileParserMock = mock(PartOfSpeechFileParser.class);
    when(fileParserMock.parseFile()).thenReturn(wordsToReturn);

    wordListImporterImpl.setFileParser(fileParserMock);

    wordListImporterImpl.importWordList();

    rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl);

    assertEquals(3, rowCountFromObject.intValue());
    verify(wordDaoMock, times(2)).insertBatch(anyListOf(Word.class));
    verify(taskExecutorSpy, times(2)).execute(any(Runnable.class));
}

From source file:de.extra.client.core.builder.impl.MessageBuilderLocatorTest.java

/**
 * Setzt eine Value ber ReflectionUtils/*from   ww  w  . j a  v a  2 s.  c o  m*/
 * 
 * @param messageBuilderLocator
 * @param rootElementsBuilderMap
 * @param string
 */
private void injectValue(final Object object, final Object value, final String fieldName) {
    final Field fieldToSet = ReflectionUtils.findField(object.getClass(), fieldName);
    ReflectionUtils.makeAccessible(fieldToSet);
    ReflectionUtils.setField(fieldToSet, object, value);
}