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

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

Introduction

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

Prototype

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

Source Link

Document

Gets an accessible Field by name breaking scope if requested.

Usage

From source file:com.haulmont.cuba.web.toolkit.ui.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:config.CFTemp.java

@Override
public void deser(Map<String, Object> json) {
    for (Map.Entry<String, Object> tmpEntry : json.entrySet()) {
        String fieldName = tmpEntry.getKey();
        Object value = tmpEntry.getValue();

        Field field = FieldUtils.getField(getClass(), fieldName, true);
        if (field != null) {
            Class<?> fldClass = field.getType();
            if (CONVERTERS.containsKey(fldClass)) {
                try {
                    Function<Object, Object> converter = CONVERTERS.get(fldClass);
                    field.set(this, converter.apply(value));
                } catch (IllegalArgumentException ex) {
                    PGException.pgThrow(ex, "Invalid convert");
                } catch (IllegalAccessException ex) {
                    PGException.pgThrow(ex, "Invalid convert");
                }//from  www.  j a  v a  2 s.  com
            }
        }
    }
}

From source file:de.csdev.ebus.command.datatypes.EBusAbstractType.java

/**
 * Sets a property to a type instance//from w  ww.  j a v a 2  s  .c  om
 *
 * @param instance
 * @param property
 * @param value
 */
protected void setInstanceProperty(EBusAbstractType<T> instance, String property, Object value) {

    if (property.equals("replaceValue")) {
        if (value instanceof String) {
            try {
                instance.setReplaceValue(EBusUtils.toByteArray((String) value));
            } catch (EBusTypeException e) {
                logger.error("error!", e);
            }
        }

        return;
    }

    try {
        Field field = FieldUtils.getField(instance.getClass(), property, true);

        if (field != null) {
            field.set(instance, value);
        }

    } catch (SecurityException e) {
        logger.error("error!", e);
    } catch (IllegalArgumentException e) {
        logger.error("error!", e);
    } catch (IllegalAccessException e) {
        logger.error("error!", e);
    }
}

From source file:com.haulmont.cuba.core.entity.BaseEntityInternalAccess.java

public static void setValue(Entity entity, String attribute, @Nullable Object value) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    Field field = FieldUtils.getField(entity.getClass(), attribute, true);
    if (field == null)
        throw new RuntimeException(
                String.format("Cannot find field '%s' in class %s", attribute, entity.getClass().getName()));
    try {//from   w ww.j a  v a  2  s. c o  m
        field.set(entity, value);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(
                String.format("Unable to set value to %s.%s", entity.getClass().getSimpleName(), attribute), e);
    }
}

From source file:gov.va.vinci.leo.ae.LeoBaseAnnotator.java

/**
 * Initialize this annotator.//from  www  . ja va2s  .c om
 *
 * @param aContext the UimaContext to initialize with.
 * @param params the annotator params to load from the descriptor.
 *
 * @see org.apache.uima.analysis_component.JCasAnnotator_ImplBase#initialize(org.apache.uima.UimaContext)
 * @param aContext
 * @param params
 * @throws ResourceInitializationException  if any exception occurs during initialization.
 */
public void initialize(UimaContext aContext, ConfigurationParameter[] params)
        throws ResourceInitializationException {
    super.initialize(aContext);

    if (params != null) {
        for (ConfigurationParameter param : params) {
            if (param.isMandatory()) {
                /** Check for null value **/
                if (aContext.getConfigParameterValue(param.getName()) == null) {
                    throw new ResourceInitializationException(new IllegalArgumentException(
                            "Required parameter: " + param.getName() + " not set."));
                }
                /** Check for empty as well if it is a plain string. */
                if (ConfigurationParameter.TYPE_STRING.equals(param.getType()) && !param.isMultiValued()
                        && GenericValidator
                                .isBlankOrNull((String) aContext.getConfigParameterValue(param.getName()))) {
                    throw new ResourceInitializationException(new IllegalArgumentException(
                            "Required parameter: " + param.getName() + " cannot be blank."));
                }
            }

            parameters.put(param, aContext.getConfigParameterValue(param.getName()));

            /** Set the parameter value in the class field variable **/
            try {
                Field field = FieldUtils.getField(this.getClass(), param.getName(), true);
                if (field != null) {
                    FieldUtils.writeField(field, this, aContext.getConfigParameterValue(param.getName()), true);
                }
            } catch (IllegalAccessException e) {
                logger.warn("Could not set field (" + param.getName()
                        + "). Field not found on annotator class to reflectively set.");
            }
        }
    }
}

From source file:com.haulmont.cuba.core.entity.BaseEntityInternalAccess.java

public static void setValueForHolder(Entity entity, String attribute, @Nullable Object value) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    Field field = FieldUtils.getField(entity.getClass(), String.format("_persistence_%s_vh", attribute), true);
    if (field == null)
        return;// www .  j  a  v a 2 s  .c o  m
    try {
        field.set(entity, value);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(
                String.format("Unable to set value to %s.%s", entity.getClass().getSimpleName(), attribute), e);
    }
}

From source file:com.haulmont.cuba.core.entity.BaseEntityInternalAccess.java

public static Object getValue(Entity entity, String attribute) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    Field field = FieldUtils.getField(entity.getClass(), attribute, true);
    if (field == null)
        throw new RuntimeException(
                String.format("Cannot find field '%s' in class %s", attribute, entity.getClass().getName()));
    try {/* ww  w  .ja va  2s  .  c  o m*/
        return field.get(entity);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(
                String.format("Unable to set value to %s.%s", entity.getClass().getSimpleName(), attribute), e);
    }
}

From source file:gov.va.vinci.leo.ae.LeoBaseAnnotator.java

/**
 * Get a map of parameters and their values.  Parameter list is retrieved from the inner Param class. Values are
 * retrieved by matching the name of the ConfigurationParameter to the name of a variable set in the class.
 *
 * @return  a map of parameters and their values that is created during initialization.
 *///  ww  w.ja  v  a  2 s  . co m
protected Map<ConfigurationParameter, Object> getParametersToValuesMap() {
    Map<ConfigurationParameter, Object> parameterObjectMap = new HashMap<ConfigurationParameter, Object>();
    ConfigurationParameter[] params;
    try {
        params = this.getAnnotatorParams();
        for (ConfigurationParameter parameter : params) {
            try {
                Field field = FieldUtils.getField(this.getClass(), parameter.getName(), true);
                parameterObjectMap.put(parameter, FieldUtils.readField(field, this, true));
            } catch (Exception eField) {
                parameterObjectMap.put(parameter, null);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return parameterObjectMap;
}

From source file:org.apache.usergrid.persistence.Schema.java

private <T extends Annotation> T getAnnotation(Class<? extends Entity> entityClass,
        PropertyDescriptor descriptor, Class<T> annotationClass) {
    try {/*from  ww w  . ja  v a2 s .co m*/
        if ((descriptor.getReadMethod() != null)
                && descriptor.getReadMethod().isAnnotationPresent(annotationClass)) {
            return descriptor.getReadMethod().getAnnotation(annotationClass);
        }
        if ((descriptor.getWriteMethod() != null)
                && descriptor.getWriteMethod().isAnnotationPresent(annotationClass)) {
            return descriptor.getWriteMethod().getAnnotation(annotationClass);
        }
        Field field = FieldUtils.getField(entityClass, descriptor.getName(), true);
        if (field != null) {
            if (field.isAnnotationPresent(annotationClass)) {
                return field.getAnnotation(annotationClass);
            }
        }
    } catch (Exception e) {
        logger.error("Could not retrieve the annotations", e);
    }
    return null;
}

From source file:org.jnap.core.persistence.hsearch.FullTextDaoSupport.java

protected String[] getIndexedFields() {
    if (this.indexedFields == null) {
        PropertyDescriptor[] beanProperties = BeanUtils.getPropertyDescriptors(getEntityClass());
        List<String> fields = new ArrayList<String>();
        for (PropertyDescriptor propertyDescriptor : beanProperties) {
            Field field = AnnotationUtils.findAnnotation(propertyDescriptor.getReadMethod(), Field.class);
            if (field == null) {
                java.lang.reflect.Field propertyField = FieldUtils.getField(getEntityClass(),
                        propertyDescriptor.getName(), true);
                if (propertyField != null && propertyField.isAnnotationPresent(Field.class)) {
                    field = propertyField.getAnnotation(Field.class);
                }// w  ww . j  a va  2  s  . com
            }
            if (field != null) {
                fields.add(propertyDescriptor.getName());
            }
        }
        if (fields.isEmpty()) {
            throw new HSearchQueryException(""); // TODO ex msg
        }
        this.indexedFields = fields.toArray(new String[] {});
    }
    return this.indexedFields;
}