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:org.openinfinity.core.aspect.ArgumentBuilder.java

private Field getField(String allowedField, Object object, String argument) {
    Field field;//w w w . j  a  va 2s .  com
    LOGGER.debug(object.getClass().getName() + "." + allowedField);
    field = ReflectionUtils.findField(object.getClass(), allowedField);
    LOGGER.debug("field name: " + field.getName());
    field.setAccessible(true);
    FIELD_CACHE.put(argument, field);
    return field;
}

From source file:com.github.lothar.security.acl.jpa.query.AclJpaQuery.java

@SuppressWarnings("unchecked")
private static <T> T getField(Class<?> type, Object object, String fieldName) {
    Field field = ReflectionUtils.findField(type, fieldName);
    field.setAccessible(true);//from   w  w w  . j  av a2  s .  c  om
    Object property = ReflectionUtils.getField(field, object);
    return (T) property;
}

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);/*from  w  w w . ja va2  s .co  m*/

    assertEquals(taskExecutorToSet, taskExecutorFromObject);
}

From source file:com.agileapes.couteau.context.spring.event.impl.AbstractMappedEventsTranslationScheme.java

@Override
public void fillIn(Event originalEvent, ApplicationEvent translated) throws EventTranslationException {
    for (Method method : originalEvent.getClass().getMethods()) {
        if (!method.getName().matches("set[A-Z].*") || !Modifier.isPublic(method.getModifiers())
                || !method.getReturnType().equals(void.class) || method.getParameterTypes().length != 1) {
            continue;
        }//ww  w  .j a va 2 s  .c  om
        final String propertyName = StringUtils.uncapitalize(method.getName().substring(3));
        final Field field = ReflectionUtils.findField(translated.getClass(), propertyName);
        if (field == null || !method.getParameterTypes()[0].isAssignableFrom(field.getType())) {
            continue;
        }
        field.setAccessible(true);
        try {
            method.invoke(originalEvent, field.get(translated));
        } catch (Exception e) {
            throw new EventTranslationException("Failed to set property on original event: " + propertyName, e);
        }
    }
}

From source file:es.logongas.ix3.dao.impl.ExceptionTranslator.java

private ClassAndLabel getFieldLabel(Class clazz, String fieldName) {
    Field field = ReflectionUtils.findField(clazz, fieldName);
    if (field == null) {
        return null;
    }/*w  ww .j  a  va 2 s. c  om*/

    Label label = field.getAnnotation(Label.class);
    if (label != null) {
        return new ClassAndLabel(field.getType(), label.value());
    } else {
        return new ClassAndLabel(field.getType(), null);
    }

}

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

/**
 * In case LazyInit is set to false/*w w  w.  j  av  a 2  s .  c om*/
 * @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)));
    }
}

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

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

    LiberalCrossoverAlgorithm liberalCrossoverAlgorithm = new LiberalCrossoverAlgorithm();
    liberalCrossoverAlgorithm.setMutateDuringCrossover(mutateDuringCrossoverToSet);

    Field mutateDuringCrossoverField = ReflectionUtils.findField(LiberalCrossoverAlgorithm.class,
            "mutateDuringCrossover");
    ReflectionUtils.makeAccessible(mutateDuringCrossoverField);
    boolean mutateDuringCrossoverFromObject = (boolean) ReflectionUtils.getField(mutateDuringCrossoverField,
            liberalCrossoverAlgorithm);/*from  ww  w .j  ava 2s  .  c o m*/

    assertEquals(mutateDuringCrossoverToSet, mutateDuringCrossoverFromObject);
}

From source file:edu.mayo.cts2.framework.webapp.rest.controller.AbstractMessageWrappingController.java

private void setDirectoryEntries(Directory directory, List<?> entries) {
    try {// w w  w.j a  va  2  s. c  o m
        final Field field = ReflectionUtils.findField(directory.getClass(), "_entryList");

        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                field.setAccessible(true);

                return null;
            }
        });

        ReflectionUtils.setField(field, directory, entries);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

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

    AscendingFitnessComparator ascendingFitnessComparator = new AscendingFitnessComparator();
    population.setFitnessComparator(ascendingFitnessComparator);

    Field fitnessComparatorField = ReflectionUtils.findField(Population.class, "fitnessComparator");
    ReflectionUtils.makeAccessible(fitnessComparatorField);
    AscendingFitnessComparator fitnessComparatorFromObject = (AscendingFitnessComparator) ReflectionUtils
            .getField(fitnessComparatorField, population);

    assertSame(ascendingFitnessComparator, fitnessComparatorFromObject);
}

From source file:ru.anr.base.BaseParent.java

/**
 * Injection of a private field into the given object. Of course, we are
 * against such a technique :-). But still in tests this is a very
 * convenient approach.// w ww  . j ava  2s.c  om
 *
 * @param target
 *            The target object
 * @param fieldName
 *            The name of the field to inject
 * @param field
 *            The field's value
 */
public static void inject(Object target, String fieldName, Object field) {

    Field f = ReflectionUtils.findField(target.getClass(), fieldName);
    Assert.notNull(f);

    ReflectionUtils.makeAccessible(f);
    ReflectionUtils.setField(f, target, field);
}