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.genetics.PopulationTest.java

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

    FitnessEvaluator fitnessEvaluatorMock = mock(FitnessEvaluator.class);
    when(fitnessEvaluatorMock.evaluate(any(Chromosome.class))).thenReturn(DEFAULT_FITNESS_VALUE);
    population.setFitnessEvaluator(fitnessEvaluatorMock);

    Field fitnessEvaluatorField = ReflectionUtils.findField(Population.class, "fitnessEvaluator");
    ReflectionUtils.makeAccessible(fitnessEvaluatorField);
    FitnessEvaluator fitnessEvaluatorFromObject = (FitnessEvaluator) ReflectionUtils
            .getField(fitnessEvaluatorField, population);

    assertSame(fitnessEvaluatorMock, fitnessEvaluatorFromObject);
}

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

@Test
public void testSetMutateDuringCrossover() {
    boolean mutateDuringCrossoverToSet = true;

    LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm();
    liberalUnevaluatedCrossoverAlgorithm.setMutateDuringCrossover(mutateDuringCrossoverToSet);

    Field mutateDuringCrossoverField = ReflectionUtils.findField(LiberalUnevaluatedCrossoverAlgorithm.class,
            "mutateDuringCrossover");
    ReflectionUtils.makeAccessible(mutateDuringCrossoverField);
    boolean mutateDuringCrossoverFromObject = (boolean) ReflectionUtils.getField(mutateDuringCrossoverField,
            liberalUnevaluatedCrossoverAlgorithm);

    assertEquals(mutateDuringCrossoverToSet, mutateDuringCrossoverFromObject);
}

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

@Test
public void testSetGroupSelector() {
    TournamentSelector groupSelectorToSet = new TournamentSelector();

    TournamentSelectionAlgorithm tournamentSelectionAlgorithm = new TournamentSelectionAlgorithm();
    tournamentSelectionAlgorithm.setGroupSelector(groupSelectorToSet);

    Field groupSelectorField = ReflectionUtils.findField(TournamentSelectionAlgorithm.class, "groupSelector");
    ReflectionUtils.makeAccessible(groupSelectorField);
    Selector groupSelectorFromObject = (Selector) ReflectionUtils.getField(groupSelectorField,
            tournamentSelectionAlgorithm);

    assertSame(groupSelectorToSet, groupSelectorFromObject);
}

From source file:com.ciphertool.genetics.algorithms.mutation.LiberalMutationAlgorithmTest.java

@Test
public void testSetChromosomeHelper() {
    KeylessChromosomeHelper chromosomeHelperToSet = new KeylessChromosomeHelper();

    LiberalMutationAlgorithm liberalMutationAlgorithm = new LiberalMutationAlgorithm();
    liberalMutationAlgorithm.setChromosomeHelper(chromosomeHelperToSet);

    Field chromosomeHelperField = ReflectionUtils.findField(LiberalMutationAlgorithm.class,
            "keylessChromosomeHelper");
    ReflectionUtils.makeAccessible(chromosomeHelperField);
    KeylessChromosomeHelper chromosomeHelperFromObject = (KeylessChromosomeHelper) ReflectionUtils
            .getField(chromosomeHelperField, liberalMutationAlgorithm);

    assertSame(chromosomeHelperToSet, chromosomeHelperFromObject);
}

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

@Test
public void testSetTaskExecutor() {
    TaskExecutor taskExecutorToSet = mock(TaskExecutor.class);
    WordListImporterImpl wordListImporterImpl = new WordListImporterImpl();
    wordListImporterImpl.setTaskExecutor(taskExecutorToSet);

    Field taskExecutorField = ReflectionUtils.findField(WordListImporterImpl.class, "taskExecutor");
    ReflectionUtils.makeAccessible(taskExecutorField);
    TaskExecutor taskExecutorFromObject = (TaskExecutor) ReflectionUtils.getField(taskExecutorField,
            wordListImporterImpl);// w ww  . j a  va2  s .  c om

    assertEquals(taskExecutorToSet, taskExecutorFromObject);
}

From source file:com.ciphertool.genetics.algorithms.mutation.GroupMutationAlgorithmTest.java

@Test
public void testSetMaxMutationsPerChromosome() {
    Integer maxMutationsPerChromosomeToSet = 3;

    GroupMutationAlgorithm groupMutationAlgorithm = new GroupMutationAlgorithm();
    groupMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet);

    Field maxMutationsPerChromosomeField = ReflectionUtils.findField(GroupMutationAlgorithm.class,
            "maxMutationsPerChromosome");
    ReflectionUtils.makeAccessible(maxMutationsPerChromosomeField);
    Integer maxMutationsPerChromosomeFromObject = (Integer) ReflectionUtils
            .getField(maxMutationsPerChromosomeField, groupMutationAlgorithm);

    assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject);
}

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

@Test
public void testCalculateAdjacentMatches_NullPlaintextCharacters() {
    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.calculateAdjacentMatches(null);

    verify(mockLogger, times(1)).warn(//from ww  w  . j av a 2  s.  c om
            "Attempted to calculate adjacent matches, but the List of plaintextCharacters was null or empty.  Returning -1.");
}

From source file:org.jdal.ui.bind.DirectFieldAccessor.java

@Override
public void setPropertyValue(String propertyName, Object newValue) throws BeansException {
    Field field = this.fieldMap.get(propertyName);
    if (field == null) {
        throw new NotWritablePropertyException(this.target.getClass(), propertyName,
                "Field '" + propertyName + "' does not exist");
    }//from w w  w.  ja  v  a  2s  .  co  m
    Object oldValue = null;
    try {
        ReflectionUtils.makeAccessible(field);
        oldValue = field.get(this.target);
        // jlm - Adapt to simpleTypeConverter
        Object convertedValue = this.typeConverterDelegate.convertIfNecessary(newValue, field.getType());
        field.set(this.target, convertedValue);
    } catch (ConverterNotFoundException ex) {
        PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
        throw new ConversionNotSupportedException(pce, field.getType(), ex);
    } catch (ConversionException ex) {
        PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
        throw new TypeMismatchException(pce, field.getType(), ex);
    } catch (IllegalStateException ex) {
        PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
        throw new ConversionNotSupportedException(pce, field.getType(), ex);
    } catch (IllegalArgumentException ex) {
        PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
        throw new TypeMismatchException(pce, field.getType(), ex);
    } catch (IllegalAccessException ex) {
        throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex);
    }
}

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

@Test
public void testSetTaskExecutor() {
    TaskExecutor taskExecutorToSet = mock(TaskExecutor.class);
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setTaskExecutor(taskExecutorToSet);

    Field taskExecutorField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "taskExecutor");
    ReflectionUtils.makeAccessible(taskExecutorField);
    TaskExecutor taskExecutorFromObject = (TaskExecutor) ReflectionUtils.getField(taskExecutorField,
            frequencyListImporterImpl);/* w w w . j a v  a  2 s . co  m*/

    assertEquals(taskExecutorToSet, taskExecutorFromObject);
}

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

/**
 * In case LazyInit is set to false/* w  w  w . j  av a2s  . com*/
 * @throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void testAfterPropertiesSet01() throws Exception {
    // create target
    JdbcCodeList jdbcCodeList = new JdbcCodeList();

    // setup parameters\
    jdbcCodeList.setDataSource(dataSource);
    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);

    jdbcCodeList.afterPropertiesSet();

    Map<String, String> exposedMapSecondFetch = (Map<String, String>) f.get(jdbcCodeList);
    // assert
    assertThat(exposedMapSecondFetch.size(), is(mapInput.size()));
    for (String key : exposedMapSecondFetch.keySet()) {
        assertThat(exposedMapSecondFetch.get(key), is(mapInput.get(key)));
    }
}