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.mutation.cipherkey.RandomValueMutationAlgorithmTest.java

@Test
public void testSetGeneDao() {
    VariableLengthGeneDao geneDaoToSet = mock(VariableLengthGeneDao.class);

    RandomValueMutationAlgorithm randomValueMutationAlgorithm = new RandomValueMutationAlgorithm();
    randomValueMutationAlgorithm.setGeneDao(geneDaoToSet);

    Field geneDaoField = ReflectionUtils.findField(RandomValueMutationAlgorithm.class, "geneDao");
    ReflectionUtils.makeAccessible(geneDaoField);
    GeneDao geneDaoFromObject = (GeneDao) ReflectionUtils.getField(geneDaoField, randomValueMutationAlgorithm);

    assertSame(geneDaoToSet, geneDaoFromObject);
}

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

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

    ConservativeSinglePointCrossoverAlgorithm conservativeSinglePointCrossoverAlgorithm = new ConservativeSinglePointCrossoverAlgorithm();
    conservativeSinglePointCrossoverAlgorithm.setMutateDuringCrossover(mutateDuringCrossoverToSet);

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

    assertEquals(mutateDuringCrossoverToSet, mutateDuringCrossoverFromObject);
}

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

/**
 * Test of getField method, of class ReflectionUtil.
 *//*  w  w w. j a va2 s. c o m*/
@Test
public void testGetField() {
    System.out.println("getField");
    Class clazz = BeanTestC.class;
    String propertyName = "prop";
    Field expResult = ReflectionUtils.findField(BeanTestC.class, propertyName);
    Field result = ReflectionUtil.getField(clazz, propertyName);
    assertEquals(expResult, result);
}

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

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

    ConservativeCrossoverAlgorithm conservativeCrossoverAlgorithm = new ConservativeCrossoverAlgorithm();
    conservativeCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

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

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}

From source file:net.nan21.dnet.core.presenter.converter.ReflookupResolver.java

/**
 * Get the entity-field with the given name.
 * /*  w  w  w.  j  a va  2s  . c o m*/
 * @param fieldName
 * @return
 * @throws Exception
 */
private Field _getEntityField(String fieldName) throws Exception {
    return ReflectionUtils.findField(this.entityClass, fieldName);
}

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

@Test
public void testSetChromosomeHelper() {
    KeylessChromosomeHelper chromosomeHelperToSet = mock(KeylessChromosomeHelper.class);
    LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm();
    liberalUnevaluatedCrossoverAlgorithm.setChromosomeHelper(chromosomeHelperToSet);

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

    assertSame(chromosomeHelperToSet, chromosomeHelperFromObject);
}

From source file:com.ciphertool.sentencebuilder.etl.parsers.PartOfSpeechFileParserTest.java

@Test
public void testParseFile_InvalidFileName() {
    PartOfSpeechFileParser partOfSpeechFileParser = new PartOfSpeechFileParser();
    partOfSpeechFileParser.setFileName("arbitraryFileName");

    Logger logMock = mock(Logger.class);

    Field logField = ReflectionUtils.findField(PartOfSpeechFileParser.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, partOfSpeechFileParser, logMock);

    List<Word> wordsFromFile = partOfSpeechFileParser.parseFile();

    assertTrue(wordsFromFile.isEmpty());
    verify(logMock, times(1)).error(anyString(), any(FileNotFoundException.class));
}

From source file:org.springjutsu.validation.util.PathUtils.java

/**
 * Determine the class of each step in the selected path on the target class. 
 * @param clazz Class to check //from   w  w w.  j a v  a 2  s .c om
 * @param path Path to check
 * @param unwrapCollectionTypes if true returns the parameterized collection type
 * @return array of classes for each step of path.
 */
public static Class<?>[] getClassesForPathTokens(Class<?> clazz, String path, boolean unwrapCollectionTypes) {
    if (path == null || path.trim().isEmpty()) {
        return null;
    }
    Class<?> intermediateClass = clazz;
    String[] pathTokens = path.split("\\.");
    Class<?>[] pathClasses = new Class<?>[pathTokens.length];

    for (int i = 0; i < pathTokens.length; i++) {
        String token = pathTokens[i];
        token = token.replaceAll("\\[[^\\]]+\\]$", "");
        PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(intermediateClass, token);
        if (descriptor == null) {
            return null;
        } else if (List.class.isAssignableFrom(descriptor.getPropertyType())) {
            intermediateClass = TypeDescriptor.nested(ReflectionUtils.findField(intermediateClass, token), 1)
                    .getObjectType();
        } else if (descriptor.getPropertyType().isArray()) {
            intermediateClass = descriptor.getPropertyType().getComponentType();
        } else {
            intermediateClass = descriptor.getPropertyType();
        }
        if (unwrapCollectionTypes) {
            pathClasses[i] = intermediateClass;
        } else {
            pathClasses[i] = descriptor.getPropertyType();
        }
    }
    return pathClasses;
}

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

@Test
public void testSetChromosomeHelper() {
    KeylessChromosomeHelper chromosomeHelperToSet = mock(KeylessChromosomeHelper.class);
    LiberalCrossoverAlgorithm liberalCrossoverAlgorithm = new LiberalCrossoverAlgorithm();
    liberalCrossoverAlgorithm.setChromosomeHelper(chromosomeHelperToSet);

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

    assertSame(chromosomeHelperToSet, chromosomeHelperFromObject);
}

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

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*w  w  w . ja  v a2 s . com*/
public void testSetMutationAlgorithm() {
    MutationAlgorithm mutationAlgorithmToSet = mock(MutationAlgorithm.class);

    LowestCommonGroupCrossoverAlgorithm lowestCommonGroupCrossoverAlgorithm = new LowestCommonGroupCrossoverAlgorithm();
    lowestCommonGroupCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

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

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}