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:com.ciphertool.genetics.algorithms.crossover.LowestCommonGroupCrossoverAlgorithmTest.java

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

    LowestCommonGroupCrossoverAlgorithm lowestCommonGroupCrossoverAlgorithm = new LowestCommonGroupCrossoverAlgorithm();
    lowestCommonGroupCrossoverAlgorithm.setMutateDuringCrossover(mutateDuringCrossoverToSet);

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

    assertEquals(mutateDuringCrossoverToSet, mutateDuringCrossoverFromObject);
}

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

@Test
public void testSetRandomListElementSelector() {
    RandomListElementSelector randomListElementSelectorToSet = mock(RandomListElementSelector.class);
    ConservativeSinglePointCrossoverAlgorithm conservativeSinglePointCrossoverAlgorithm = new ConservativeSinglePointCrossoverAlgorithm();
    conservativeSinglePointCrossoverAlgorithm.setRandomListElementSelector(randomListElementSelectorToSet);

    Field randomListElementSelectorField = ReflectionUtils
            .findField(ConservativeSinglePointCrossoverAlgorithm.class, "randomListElementSelector");
    ReflectionUtils.makeAccessible(randomListElementSelectorField);
    RandomListElementSelector randomListElementSelectorFromObject = (RandomListElementSelector) ReflectionUtils
            .getField(randomListElementSelectorField, conservativeSinglePointCrossoverAlgorithm);

    assertSame(randomListElementSelectorToSet, randomListElementSelectorFromObject);
}

From source file:sample.jsp.SampleJspApplication.java

private void clearJspSystemUris() {
    Field systemUris = ReflectionUtils.findField(TldScanner.class, "systemUris");
    systemUris.setAccessible(true);/* ww  w . j  a va 2s.  c  o  m*/
    ReflectionUtils.setField(systemUris, null, new HashSet<String>());
}

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

@Test
@SuppressWarnings("unchecked")
public void testSetFileParser() {
    FileParser<Word> fileParserToSet = mock(FileParser.class);
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setFileParser(fileParserToSet);

    Field fileParserField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "frequencyFileParser");
    ReflectionUtils.makeAccessible(fileParserField);
    FileParser<Word> fileParserFromObject = (FileParser<Word>) ReflectionUtils.getField(fileParserField,
            frequencyListImporterImpl);/*  w w w  .ja  va  2  s  .  c om*/

    assertSame(fileParserToSet, fileParserFromObject);
}

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

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*from   w  ww .  ja v  a 2  s  .  c  o m*/
public void testSetMutationAlgorithm() {
    MutationAlgorithm mutationAlgorithmToSet = mock(MutationAlgorithm.class);

    LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm();
    liberalUnevaluatedCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

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

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}

From source file:py.una.pol.karaku.services.ReflectionConverter.java

/**
 * @param source//from  w  w w.  jav  a  2 s .c o  m
 * @param targetClass
 * @param depth
 * @param dtoToEntity
 * @param bean
 * @param field
 * @throws IllegalAccessException
 */
private void mapField(final Object source, final Class<?> targetClass, final int depth,
        final boolean dtoToEntity, final Object bean, Field field) throws IllegalAccessException {

    Field toSet = ReflectionUtils.findField(targetClass, field.getName());
    if (toSet == null) {
        return;
    }
    field.setAccessible(true);
    toSet.setAccessible(true);
    Class<?> s = field.getType();
    Class<?> t = toSet.getType();

    if (Collection.class.isAssignableFrom(s) && Collection.class.isAssignableFrom(t)) {
        mapCollection(source, depth, dtoToEntity, bean, field, toSet, t);
    } else if (s.isAssignableFrom(t)) {
        Object o = field.get(source);
        toSet.set(bean, o);
    } else if (checkMappable(s, t)) {
        Object o = convert(s, t, field.get(source), depth - 1, dtoToEntity);
        toSet.set(bean, o);
    } else {
        throw new KarakuRuntimeException(String.format("Cant copy from field %s to field %s", field, toSet));
    }
}

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

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

    ConservativeMutationAlgorithm conservativeMutationAlgorithm = new ConservativeMutationAlgorithm();
    conservativeMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet);

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

    assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject);
}

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

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

    SingleSequenceMutationAlgorithm singleSequenceMutationAlgorithm = new SingleSequenceMutationAlgorithm();
    singleSequenceMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet);

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

    assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject);
}

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

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

    MockBreeder mockBreeder = new MockBreeder();
    population.setBreeder(mockBreeder);/* w  ww .jav a 2  s.c o m*/

    Field breederField = ReflectionUtils.findField(Population.class, "breeder");
    ReflectionUtils.makeAccessible(breederField);
    MockBreeder breederFromObject = (MockBreeder) ReflectionUtils.getField(breederField, population);

    assertSame(mockBreeder, breederFromObject);
}

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

@Test
public void testSetGroupSize() {
    Integer groupSizeToSet = 3;//w  w  w  .j av a  2s  . co m

    TournamentSelectionAlgorithm tournamentSelectionAlgorithm = new TournamentSelectionAlgorithm();
    tournamentSelectionAlgorithm.setGroupSize(groupSizeToSet);

    Field groupSizeField = ReflectionUtils.findField(TournamentSelectionAlgorithm.class, "groupSize");
    ReflectionUtils.makeAccessible(groupSizeField);
    Integer groupSizeFromObject = (Integer) ReflectionUtils.getField(groupSizeField,
            tournamentSelectionAlgorithm);

    assertSame(groupSizeToSet, groupSizeFromObject);
}