Example usage for java.lang.reflect Field getName

List of usage examples for java.lang.reflect Field getName

Introduction

In this page you can find the example usage for java.lang.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:org.appverse.web.framework.backend.api.converters.helpers.ConverterUtils.java

/**
 * Obtain a list of field declared by a bean class and superclasses
 * /*w ww . j a  v a2  s.  co m*/
 * @param bean
 *            Bean to get the fields
 * @return Array of fields declared by bean class and superclasses
 * @throws Exception
 */
private static Field[] getBeanFields(AbstractBean bean) throws Exception {
    // Fields added to AbstractBean by ConverterAspect
    String[] ignoreFields = ((Detachable) bean).getDetachableFields();
    Class<?> clazz = bean.getClass();
    // Get the all fields declared by the class
    Field[] fields = clazz.getDeclaredFields();
    // For all superclasses until arrive to AbstractBean (including
    // AbstractBean)...
    while (clazz.getSuperclass() != null && AbstractBean.class.isAssignableFrom(clazz.getSuperclass())) {
        clazz = clazz.getSuperclass();
        // Add the clazz declared fields to field list
        fields = (Field[]) ArrayUtils.addAll(fields, clazz.getDeclaredFields());
    }
    // Remove from the list fields added by the converters aspect to
    // Abstract
    // bean
    for (Field field : fields) {
        if (ArrayUtils.contains(ignoreFields, field.getName())) {
            fields = (Field[]) ArrayUtils.removeElement(fields, field);
        }
    }
    return fields;
}

From source file:com.taobao.tdhs.client.protocol.TDHSProtocolBinary.java

protected static void encodeRequest(Request o, ByteArrayOutputStream out, String charsetName)
        throws IllegalAccessException, IOException, TDHSEncodeException {
    for (Field f : o.getClass().getDeclaredFields()) {
        if (!Modifier.isPublic(f.getModifiers())) {
            f.setAccessible(true);/*  w  w  w . j ava2  s. c  o  m*/
        }
        Object v = f.get(o);
        if (v instanceof Request) {
            encodeRequest((Request) v, out, charsetName);
        } else if (f.getName().startsWith("_")) {
            writeObjectToStream(v, out, f.getName(), charsetName);
        }
    }
}

From source file:edu.usu.sdl.openstorefront.util.ReflectionUtil.java

/**
 * Finds the Field given an object/*from  w w  w. j ava  2s.  c om*/
 *
 * @param entity
 * @param fieldName (Ignores case) (use the returned field for the exact
 * member name)
 * @return field or null if not found
 */
public static Field getField(Object entity, String fieldName) {
    Objects.requireNonNull(entity, "Entity must not be NULL");
    Field fieldFound = null;

    List<Field> fields = getAllFields(entity.getClass());
    for (Field field : fields) {
        if (field.getName().equalsIgnoreCase(fieldName)) {
            fieldFound = field;
        }
    }
    return fieldFound;
}

From source file:net.minecraftforge.common.config.ConfigManager.java

private static String getName(Field f) {
    if (f.isAnnotationPresent(Name.class))
        return f.getAnnotation(Name.class).value();
    return f.getName();
}

From source file:com.epam.ta.reportportal.database.search.CriteriaMap.java

public static String getQueryCriteria(Field f) {
    String queryCriteria;//w w w .  ja v  a2s .co  m
    if (f.isAnnotationPresent(org.springframework.data.mongodb.core.mapping.Field.class)) {
        queryCriteria = f.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class).value();
    } else {
        queryCriteria = f.getName();
    }
    return queryCriteria;
}

From source file:com.fengduo.bee.commons.util.StringFormatter.java

public static Object objectFieldEscape(Object ob) {
    Field[] fields = BeanUtils.getAllFields(null, ob.getClass());
    for (Field field : fields) {
        if (field == null || field.getName() == null || field.getType() == null) {
            continue;
        }//from   ww  w.  j a v a2 s. c o  m
        if (StringUtils.equals("serialVersionUID", field.getName())) {
            continue;
        }
        if (!StringUtils.equals(field.getType().getSimpleName(), "String")) {
            continue;
        }
        try {
            Object fieldVal = PropertyUtils.getProperty(ob, field.getName());
            if (null == fieldVal) {
                continue;
            }
            field.setAccessible(true);
            String value = (String) fieldVal;
            PropertyUtils.setProperty(ob, field.getName(), StringEscapeUtils.escapeXml(value));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return ob;
}

From source file:com.fengduo.bee.commons.util.StringFormatter.java

public static Object objectFieldBr(Object ob) {
    Field[] fields = BeanUtils.getAllFields(null, ob.getClass());
    for (Field field : fields) {
        if (field == null || field.getName() == null || field.getType() == null) {
            continue;
        }//from   w  ww .jav a2 s .c  o m
        if (StringUtils.equals("serialVersionUID", field.getName())) {
            continue;
        }
        if (!StringUtils.equals(field.getType().getSimpleName(), "String")) {
            continue;
        }
        try {
            Object fieldVal = PropertyUtils.getProperty(ob, field.getName());
            if (null == fieldVal) {
                continue;
            }
            field.setAccessible(true);
            String value = (String) fieldVal;
            PropertyUtils.setProperty(ob, field.getName(), value.replaceAll("\r\n", "<br/>"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return ob;
}

From source file:Main.java

/**
 * Sets the given field's value of the specified object instance.
 * /*from  w ww  . j av  a  2 s.  co  m*/
 * @throws IllegalArgumentException if the value cannot be set.
 */
public static void setValue(final Field field, final Object instance, final Object value) {
    try {
        field.setAccessible(true);
        field.set(instance, convert(value, field.getType()));
    } catch (final IllegalAccessException e) {
        throw new IllegalArgumentException("No access to field: " + field.getName(), e);
    }
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * <pre>//  w w w .ja  v a2  s .co  m
 * mapbean
 * </pre>
 * 
 * @param <T>
 *            T extends GenericBean
 * @param prefix
 *            fieldId?
 * @param map
 *            {fieldId=fieldValue}
 * @param entry
 *            the bean
 * @param clazz
 *            the bean class
 * @return T
 */
@SuppressWarnings("rawtypes")
public static <T extends GenericBean> T map2Bean(String prefix, Map<String, Object> map, T entry, Class clazz) {
    if (map != null && !map.isEmpty()) {
        Field[] cols = getField(clazz, true);
        for (Field f : cols) {
            String key = prefix + f.getName();
            if (map.containsKey(key)) {
                Object value;
                value = map.get(key);
                setField(entry, f.getName(), value);
            }
        }
    }
    return entry;
}

From source file:com.titilink.camel.rest.util.CommonUtils.java

/**
 * requestaction?/* ww  w  . jav a  2s.c o m*/
 *
 * @param instance
 * @param <T>
 * @throws OperationException
 */
private static <T> void checkActionCount(T instance) throws OperationException {
    if (null == instance) {
        LOGGER.error("checkActionCount FAILED! request is null");
        throw new OperationException(CommonCode.INVALID_INPUT_PARAMETER, "Invalid input params");
    }

    int actionCount = 0;
    Field[] fields = instance.getClass().getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (NEED_IGNORE_FIELD.equals(field.getName())) {
            continue;
        }

        try {
            if (null != field.get(instance)) {
                ++actionCount;
            }
        } catch (Exception e) {
            LOGGER.error("checkActionCount exception for req:" + instance, e);
            throw new OperationException(CommonCode.INVALID_INPUT_PARAMETER, "Invalid input params");
        }
    }

    if (ACTION_COUNT_ONE != actionCount) {
        LOGGER.error("checkActionCount FAILED! the number of action in request:{} is not " + ACTION_COUNT_ONE,
                instance);
        throw new OperationException(CommonCode.INVALID_INPUT_PARAMETER, "Invalid input params");
    }
}