Example usage for org.apache.commons.lang3.reflect FieldUtils getField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils getField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils getField.

Prototype

public static Field getField(final Class<?> cls, final String fieldName, final boolean forceAccess) 

Source Link

Document

Gets an accessible Field by name, breaking scope if requested.

Usage

From source file:com.antonjohansson.elasticsearchshell.utils.ReflectionUtils.java

/**
 * Gets the value of a field of an object.
 *
 * @param clazz The class of the object to get field value from.
 * @param object The object to get field value from.
 * @param fieldName The name of the field to get value from.
 * @return Returns the field value./*from  w w  w.  j  a va 2s . c om*/
 */
@SuppressWarnings("unchecked")
public static <V, T> V getFieldValue(Class<T> clazz, T object, String fieldName) {
    Field field = FieldUtils.getField(clazz, fieldName, true);
    try {
        return (V) field.get(object);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.sourcepond.maven.release.config.ConfigurationAccessor.java

private Field getFieldOrNull(final String pName, final Class<?> pExpectedReturnType) {
    Field field = fields.get(pName);
    if (field == null) {
        field = FieldUtils.getField(mojo.getClass(), pName, true);
        if (field != null) {
            isTrue(field.getType().equals(pExpectedReturnType),
                    "Field %s is not compatible with return type %s", field, pExpectedReturnType);
            fields.put(pName, field);// w  w  w  . j  a v  a2  s .  co m
        }
    }
    return field;
}

From source file:me.bird.util.Reflections.java

/**
 * , private/protected, ??setter.//from w w  w.ja va 2  s .c om
 */
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
    //      Field field = FieldUtils.getDeclaredField(obj.getClass(), fieldName, true);
    Field field = FieldUtils.getField(obj.getClass(), fieldName, true);

    if (field == null) {
        throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
    }

    try {
        field.set(obj, value);
    } catch (IllegalAccessException e) {
        logger.error("??", e);
    }
}

From source file:com.antonjohansson.elasticsearchshell.shell.output.ConsoleTest.java

private void setConsoleReader() {
    Field field = FieldUtils.getField(JLineShell.class, "reader", true);
    try {//w  w w .ja  v  a  2 s.  c o m
        field.set(shell, reader);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException("Could not set the 'reader' field on the shell");
    }
}

From source file:com.haulmont.cuba.web.widgets.renderers.componentrenderer.grid.ComponentPropertyGenerator.java

private boolean typeHasProperty(String propertyId) {
    boolean hasProperty = FieldUtils.getField(typeOfRows, propertyId, true) != null;
    String prefixedProperty = "is" + propertyId.substring(0, 1).toUpperCase() + propertyId.substring(1);
    boolean hasPrefixedProperty = FieldUtils.getField(typeOfRows, prefixedProperty, true) != null;
    return hasProperty || hasPrefixedProperty;
}

From source file:com.funtl.framework.smoke.core.modules.act.service.creator.RuntimeActivityCreatorSupport.java

private static void copyFields(Object source, Object target, String... fieldNames) {
    Assert.assertNotNull(source);/*from www .  ja v a2  s .co  m*/
    Assert.assertNotNull(target);
    Assert.assertSame(source.getClass(), target.getClass());

    for (String fieldName : fieldNames) {
        try {
            Field field = FieldUtils.getField(source.getClass(), fieldName, true);
            field.setAccessible(true);
            field.set(target, field.get(source));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.anrisoftware.globalpom.reflection.beans.BeanAccessImpl.java

private Field findField(String fieldName, Object bean) {
    return FieldUtils.getField(bean.getClass(), fieldName, true);
}

From source file:com.github.jinahya.simple.file.back.LocalFileBackTest.java

@BeforeClass
public void beforeClass() throws ReflectiveOperationException {

    rootPath = (Path) FieldUtils.getField(LocalFileBack.class, "rootPath", true).get(fileBack);
}

From source file:com.validation.manager.test.AbstractVMTestCase.java

public boolean checkHistory(Versionable v) {
    History current = v.getHistoryList().get(v.getHistoryList().size() - 1);
    List<Field> af = getAuditableFields(v);
    assertEquals(af.size(), current.getHistoryFieldList().size());
    assertTrue(af.size() > 0);/*w w  w .  j av  a2 s .c  om*/
    for (HistoryField hf : current.getHistoryFieldList()) {
        try {
            //Compare audit field vs. the record in history.
            Object o = FieldUtils.readField(FieldUtils.getField(v.getClass(), hf.getFieldName(), true), v);
            if (!Versionable.fieldMatchHistory(hf, o)) {
                return false;
            }
        } catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
    v.getHistoryList().forEach(h -> {
        LOG.info(h.toString());
    });
    return true;
}

From source file:com.feilong.core.lang.reflect.FieldUtilTemp.java

/**
 *  <code>ownerClass</code>  <code>fieldName</code> .
 *
 * @param <T>/*from w w  w .  j a v  a 2s  . c o  m*/
 *            the generic type
 * @param ownerClass
 *            the owner class
 * @param fieldName
 *            the field name
 * @param ownerObj
 *            object from which the represented field's value is to be extracted
 * @return the class field value
 * @see org.apache.commons.lang3.reflect.FieldUtils#getField(Class, String, boolean)
 * @since 1.7.1
 */
@SuppressWarnings("unchecked")
private static <T> T getFieldValue(Class<?> ownerClass, String fieldName, Object ownerObj) {
    try {
        //?, ? Class#getField(String), ?? public
        Field field = FieldUtils.getField(ownerClass, fieldName, true);
        return (T) field.get(ownerObj);
    } catch (Exception e) {
        String message = Slf4jUtil.format("ownerClass:[{}],fieldName:[{}],ownerObj:[{}]", ownerClass.getName(),
                fieldName, ownerObj);
        LOGGER.error(message, e);
        throw new ReflectException(message, e);
    }
}