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.consol.citrus.admin.converter.AbstractObjectConverter.java

/**
 * Adds new endpoint property./*from w  w w  . jav  a  2  s .  co m*/
 * @param fieldName
 * @param displayName
 * @param definition
 * @param defaultValue
 * @param required
 */
protected Property property(String fieldName, String displayName, S definition, String defaultValue,
        boolean required) {
    Field field = ReflectionUtils.findField(definition.getClass(), fieldName);

    if (field != null) {
        Method getter = ReflectionUtils.findMethod(definition.getClass(), getMethodName(fieldName));

        String value = defaultValue;
        if (getter != null) {
            Object getterResult = ReflectionUtils.invokeMethod(getter, definition);
            if (getterResult != null) {
                value = getterResult.toString();
            }
        }

        if (value != null) {
            if (field.isAnnotationPresent(XmlAttribute.class)) {
                return new Property(field.getAnnotation(XmlAttribute.class).name(), fieldName, displayName,
                        resolvePropertyExpression(value), required);
            } else {
                return new Property(fieldName, fieldName, displayName, resolvePropertyExpression(value),
                        required);
            }
        } else {
            return new Property(fieldName, fieldName, displayName, null, required);
        }
    } else {
        log.warn(String.format("Unknown field '%s' on source type '%s'", fieldName, definition.getClass()));
        return null;
    }
}

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

@Test
public void testSetPersistenceBatchSize() {
    int persistenceBatchSizeToSet = 99;
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet);

    Field persistenceBatchSizeField = ReflectionUtils.findField(FrequencyListImporterImpl.class,
            "persistenceBatchSize");
    ReflectionUtils.makeAccessible(persistenceBatchSizeField);
    int persistenceBatchSizeFromObject = (int) ReflectionUtils.getField(persistenceBatchSizeField,
            frequencyListImporterImpl);/*from w  w w  .j  a  va2 s . c  o m*/

    assertEquals(persistenceBatchSizeToSet, persistenceBatchSizeFromObject);
}

From source file:com.ciphertool.zodiacengine.service.GeneticCipherSolutionServiceTest.java

@Test
public void testSetCommandsBefore() {
    String[] commandsBeforeToSet = new String[1];
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();
    geneticCipherSolutionService.setCommandsBefore(commandsBeforeToSet);

    Field commandsBeforeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "commandsBefore");
    ReflectionUtils.makeAccessible(commandsBeforeField);
    String[] commandsBeforeFromObject = (String[]) ReflectionUtils.getField(commandsBeforeField,
            geneticCipherSolutionService);

    assertSame(commandsBeforeToSet, commandsBeforeFromObject);
}

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

private static ProxyFactory findCglibProxyFactory(final Object proxy) {
    Field field = ReflectionUtils.findField(proxy.getClass(), "CGLIB$CALLBACK_0");
    ReflectionUtils.makeAccessible(field);
    Object CGLIB$CALLBACK_0 = ReflectionUtils.getField(field, proxy);
    return (ProxyFactory) ReflectionUtils.getField(CglibAopProxy$DynamicAdvisedInterceptor_advised_FIELD,
            CGLIB$CALLBACK_0);/*from  w ww.ja  v a 2s  .  c  o m*/

}

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

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

    ConservativeMutationAlgorithm conservativeMutationAlgorithm = new ConservativeMutationAlgorithm();
    conservativeMutationAlgorithm.setGeneDao(geneDaoToSet);

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

    assertSame(geneDaoToSet, geneDaoFromObject);
}

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

@Test
public void testSetSequenceDao() {
    SequenceDao sequenceDaoToSet = mock(SequenceDao.class);

    SingleSequenceMutationAlgorithm singleSequenceMutationAlgorithm = new SingleSequenceMutationAlgorithm();
    singleSequenceMutationAlgorithm.setSequenceDao(sequenceDaoToSet);

    Field sequenceDaoField = ReflectionUtils.findField(SingleSequenceMutationAlgorithm.class, "sequenceDao");
    ReflectionUtils.makeAccessible(sequenceDaoField);
    SequenceDao sequenceDaoFromObject = (SequenceDao) ReflectionUtils.getField(sequenceDaoField,
            singleSequenceMutationAlgorithm);

    assertSame(sequenceDaoToSet, sequenceDaoFromObject);
}

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

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

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

    Object geneticStructure = new Object();
    population.setGeneticStructure(geneticStructure);

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

    Field geneticStructureField = ReflectionUtils.findField(MockBreeder.class, "geneticStructure");
    ReflectionUtils.makeAccessible(geneticStructureField);
    Object geneticStructureFromObject = ReflectionUtils.getField(geneticStructureField, breederFromObject);

    assertSame(geneticStructure, geneticStructureFromObject);
}

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

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

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

    simpleSolution.getPlaintextCharacters().get(0).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(1).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(4).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(5).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(6).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(8).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(9).setHasMatch(true);

    assertEquals(0, simpleSolution.getAdjacentMatches());

    abstractSolutionEvaluatorBase.setGeneticStructure(simpleCipher);
    int adjacentMatches = abstractSolutionEvaluatorBase
            .calculateAdjacentMatches(simpleSolution.getPlaintextCharacters());

    /*//from  www . ja va2  s. co  m
     * The adjacent match count should not be updated on the solution. It
     * should only be returned by the method.
     */
    assertEquals(0, simpleSolution.getAdjacentMatches());
    assertEquals(4, adjacentMatches);

    verifyZeroInteractions(mockLogger);
}

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

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

    Field fileParserField = ReflectionUtils.findField(WordListImporterImpl.class, "partOfSpeechFileParser");
    ReflectionUtils.makeAccessible(fileParserField);
    FileParser<Word> fileParserFromObject = (FileParser<Word>) ReflectionUtils.getField(fileParserField,
            wordListImporterImpl);//from  w ww  .  j ava2 s .  c o  m

    assertSame(fileParserToSet, fileParserFromObject);
}

From source file:com._4dconcept.springframework.data.marklogic.core.mapping.BasicMarklogicPersistentPropertyTest.java

private MarklogicPersistentProperty getPropertyFor(MarklogicPersistentEntity<?> persistentEntity,
        String fieldname) {/*from  w w w  .j  a  v  a  2s. c  om*/
    return getPropertyFor(persistentEntity, ReflectionUtils.findField(persistentEntity.getType(), fieldname));
}