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.oembedler.moon.graphql.engine.dfs.ResolvableTypeAccessor.java

public static ResolvableTypeAccessor forEnumField(Enum en) {
    Field field = ReflectionUtils.findField(en.getClass(), ((Enum) en).name());
    return new ResolvableTypeAccessor(en.toString(), null, Lists.newArrayList(field.getAnnotations()),
            en.getClass());/*from  w  ww .j  a  v  a  2 s . c om*/
}

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

@BeforeClass
public static void setUp() {
    conservativeMutationAlgorithm = new ConservativeMutationAlgorithm();

    geneDaoMock = mock(VariableLengthGeneDao.class);
    conservativeMutationAlgorithm.setGeneDao(geneDaoMock);

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(ConservativeMutationAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, conservativeMutationAlgorithm, logMock);
}

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

@BeforeClass
public static void setUp() {
    singleSequenceMutationAlgorithm = new SingleSequenceMutationAlgorithm();

    sequenceDaoMock = mock(SequenceDao.class);
    singleSequenceMutationAlgorithm.setSequenceDao(sequenceDaoMock);

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(SingleSequenceMutationAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, singleSequenceMutationAlgorithm, logMock);
}

From source file:org.jdal.vaadin.ui.form.AnnotationFieldFactory.java

/**
 * {@inheritDoc}//from  ww w .ja va  2s.  c o  m
 */
public Field createField(Item item, Object propertyId, Component uiContext) {
    if (item instanceof BeanItem<?>) {
        BeanItem<?> bi = (BeanItem<?>) item;
        String name = (String) propertyId;
        Class<?> clazz = bi.getBean().getClass();
        java.lang.reflect.Field field = ReflectionUtils.findField(clazz, name);
        Annotation[] fa = new Annotation[] {};
        if (field != null) {
            fa = field.getAnnotations();
        }
        java.lang.reflect.Method method = BeanUtils.getPropertyDescriptor(clazz, name).getReadMethod();
        Annotation[] ma = method.getAnnotations();
        Annotation[] annotations = (Annotation[]) ArrayUtils.addAll(fa, ma);
        Field f = null;
        for (Annotation a : annotations) {
            f = findField(a, clazz, name);
            if (f != null) {
                f.setCaption(createCaptionByPropertyId(propertyId));
                applyFieldProcessors(f, propertyId);
                return f;
            }
        }
    }
    // fall back to default
    return super.createField(item, propertyId, uiContext);
}

From source file:com.ciphertool.zodiacengine.dao.CipherDaoTest.java

@Test
public void testSetMongoTemplate() {
    CipherDao cipherDao = new CipherDao();
    cipherDao.setMongoTemplate(mongoTemplateMock);

    Field mongoOperationsField = ReflectionUtils.findField(CipherDao.class, "mongoOperations");
    ReflectionUtils.makeAccessible(mongoOperationsField);
    MongoOperations mongoOperationsFromObject = (MongoOperations) ReflectionUtils.getField(mongoOperationsField,
            cipherDao);// w ww.j  a  v a 2  s . c  om

    assertSame(mongoTemplateMock, mongoOperationsFromObject);
}

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

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

From source file:org.hsweb.web.service.impl.form.validator.java.JavaDycBeanValidator.java

public boolean validateMap(Map<Object, Object> data, Operation operation) {
    ValidateResults results = new ValidateResults();
    try {//from w ww .j  av  a  2  s .co  m
        Object validatorTarget = clazz.newInstance();
        Set<ConstraintViolation<Object>> result = new LinkedHashSet<>();
        if (operation == Operation.INSERT) {
            data.forEach((key, value) -> {
                try {
                    BeanUtils.setProperty(validatorTarget, (String) key, value);
                } catch (Exception e) {
                }
            });
            result.addAll(hibernateValidator.validate(validatorTarget));
        } else
            data.forEach((key, value) -> {
                Field field = ReflectionUtils.findField(clazz, (String) key);
                if (field != null)
                    result.addAll(hibernateValidator.validateValue(clazz, (String) key, value));
            });
        if (result.size() > 0) {
            for (ConstraintViolation<Object> violation : result) {
                String property = violation.getPropertyPath().toString();
                results.addResult(property, violation.getMessage());
            }
        }
    } catch (Exception e) {
        throw new BusinessException("??", e, 500);
    }
    if (results.size() > 0)
        throw new ValidationException(results.get(0).getMessage(), results);
    return true;
}

From source file:org.sakuli.javaDSL.AbstractSakuliTest.java

private static Object getField(Object target, String name) {
    if (target != null) {
        Field field = ReflectionUtils.findField(target.getClass(), name);
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            return ReflectionUtils.getField(field, target);
        }/*from  w w  w .  j  ava 2s  . co m*/
    }
    return null;
}

From source file:py.una.pol.karaku.util.KarakuReflectionUtils.java

/**
 * Busca un {@link Field} por su nombre entre una lista de nombres.
 * /*from w  ww .ja v a2  s.  co m*/
 * <p>
 * Retorna el primer {@link Field} que encuentra, el orden de bsqueda es el
 * mismo que el vector de nombres.
 * </p>
 * 
 * @see ReflectionUtils#findField(Class, String)
 * @param base
 *            clase en la que se busca
 * @param names
 *            nombres de los mtodos
 * @return {@link Field} encontrado, <code>null</code> si no encuentra
 *         ninguno.
 */
public static Field findField(@Nonnull Class<?> base, String... names) {

    Field f = null;
    for (String s : names) {
        f = ReflectionUtils.findField(base, s);
        if (f != null) {
            return f;
        }
    }
    return f;
}

From source file:com.iisigroup.cap.model.GenericBean.java

/**
 * set//w w  w .  j a v a 2  s.c om
 * 
 * @param <T>
 *            T extends GenericBean
 * @param field
 *            ?id
 * @param value
 *            ?
 * @return T <T>
 */
@SuppressWarnings("unchecked")
public <T> T set(String field, Object value) {
    if (CapString.isEmpty(field)) {
        return (T) this;
    }
    try {
        Field f = ReflectionUtils.findField(getClass(), field);
        if (f != null) {
            String setter = new StringBuffer("set").append(String.valueOf(f.getName().charAt(0)).toUpperCase())
                    .append(f.getName().substring(1)).toString();
            Method method = ReflectionUtils.findMethod(this.getClass(), setter, new Class[] { f.getType() });
            if (method != null) {
                method.invoke(this, value);
            }
        }
    } catch (Exception e) {
        throw new CapException(
                new StringBuffer("field:").append(field).append(" ").append(e.getMessage()).toString(), e,
                getClass());
    }
    return (T) this;
}