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:com.qrmedia.commons.persistence.hibernate.clone.HibernateEntityBeanCloner.java

private static Collection<String> calculateTargetedFieldNames(Object entity, boolean preserveIdFields) {
    Collection<String> targetedFieldNames = new ArrayList<String>();
    Class<?> entityClass = entity.getClass();

    for (Field field : ClassUtils.getAllDeclaredFields(entityClass)) {
        String fieldName = field.getName();

        // ignore static members and members without a valid getter and setter
        if (!Modifier.isStatic(field.getModifiers()) && PropertyUtils.isReadable(entity, fieldName)
                && PropertyUtils.isWriteable(entity, fieldName)) {
            targetedFieldNames.add(field.getName());
        }/*w  w  w  . j a va2  s  .com*/

    }

    /*
     * Assume that, in accordance with recommendations, entities are using *either* JPA property
     * *or* field access. Guess the access type from the location of the @Id annotation, as
     * Hibernate does.
     */
    Set<Method> idAnnotatedMethods = ClassUtils.getAnnotatedMethods(entityClass, Id.class);
    boolean usingFieldAccess = idAnnotatedMethods.isEmpty();

    // ignore fields annotated with @Version and, optionally, @Id
    targetedFieldNames.removeAll(usingFieldAccess
            ? getFieldNames(ClassUtils.getAllAnnotatedDeclaredFields(entityClass, Version.class))
            : getPropertyNames(ClassUtils.getAnnotatedMethods(entityClass, Version.class)));

    if (!preserveIdFields) {
        targetedFieldNames.removeAll(usingFieldAccess
                ? getFieldNames(ClassUtils.getAllAnnotatedDeclaredFields(entityClass, Id.class))
                : getPropertyNames(idAnnotatedMethods));
    }

    return targetedFieldNames;
}

From source file:Main.java

/**
 * @param clazz/*from ww w.  j av a  2  s  .  c  o m*/
 * @return namespace of root element qname or null if this is not object does not represent a root element
 */
public static String getEnumValue(Enum<?> myEnum) {
    Field f;
    String value;
    try {
        f = myEnum.getClass().getField(myEnum.name());

        f.setAccessible(true);

        XmlEnumValue xev = (XmlEnumValue) getAnnotation(f, XmlEnumValue.class);
        if (xev == null) {
            value = f.getName();
        } else {
            value = xev.value();
        }
    } catch (SecurityException e) {
        value = null;
    } catch (NoSuchFieldException e) {
        value = null;
    }

    return value;
}

From source file:com.leadtone.riders.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredField.//from w w w .  java2  s. c  o m
 * 
 * ?Object?, null.
 * @param <T>
 */
public static <T> List<String> getDeclaredFieldNames(final Class<T> c) {
    List<Field> list = new ArrayList<Field>();
    List<String> nameList = new ArrayList<String>();
    for (Class<?> superClass = c; superClass != Object.class; superClass = superClass.getSuperclass()) {
        CollectionUtils.addAll(list, superClass.getDeclaredFields());
    }
    for (Field field : list) {
        nameList.add(field.getName());
    }
    return nameList;
}

From source file:com.shrj.util.ReflectionUtils.java

public static String getAnnotatedFieldName(Class clazz, Class<? extends Annotation> annotation) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(annotation)) {
            return field.getName();
        }/*www  .  ja  v a2  s. c o  m*/
    }
    return null;
}

From source file:ColorUtils.java

private static Map<String, Color> buildNameToColorMap() {
    Map<String, Color> colorMap = new HashMap<String, Color>();
    for (Field field : Color.class.getDeclaredFields()) {
        if (isConstantColorField(field)) {
            try {
                Color color = (Color) field.get(null);
                colorMap.put(field.getName().toUpperCase(), color);
            } catch (IllegalAccessException e) {
                // Should not occur because the field is public and static
            }// w  w w  .j  a v a2 s.  co  m
        }
    }

    colorMap.put("DARKGREY", getColor("555555"));
    colorMap.put("DARKBLUE", getColor("000055"));
    colorMap.put("DARKRED", getColor("550000"));
    colorMap.put("DARKGREEN", getColor("005500"));

    colorMap.put("DARK_GREY", getColor("555555"));
    colorMap.put("DARK_BLUE", getColor("000055"));
    colorMap.put("DARK_RED", getColor("550000"));
    colorMap.put("DARK_GREEN", getColor("005500"));

    return colorMap;
}

From source file:com.impetus.ankush.common.utils.JsonMapperUtil.java

/**
 * Object from map./*from ww w . ja v  a2s.  c o m*/
 * 
 * @param <S>
 *            the generic type
 * @param values
 *            the values
 * @param targetClass
 *            the target class
 * @return the s
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InstantiationException
 *             the instantiation exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws Exception
 *             the exception
 */
public static <S> S objectFromMap(Map<String, Object> values, Class<S> targetClass)
        throws IllegalArgumentException, IllegalAccessException, InstantiationException,
        InvocationTargetException, Exception {
    // Creating target class object.
    S mainObject = targetClass.newInstance();

    // Getting fields of the class.
    Field[] fields = targetClass.getDeclaredFields();
    Map<String, Field> fieldMap = new HashMap<String, Field>();

    for (Field field : fields) {
        // Putting fields in fieldMap
        fieldMap.put(field.getName(), field);
    }

    // Iterating over the key set in value map.
    for (String mainKey : values.keySet()) {
        if (values.get(mainKey) instanceof LinkedHashMap) {
            // Creating target object type.
            if (fieldMap.get(mainKey) == null) {
                continue;
            }
            Object subObject = fieldMap.get(mainKey).getType().newInstance();

            // Casting to map.
            Map subValues = (Map) values.get(mainKey);

            // Iterating over the map keys.
            for (Object subKey : subValues.keySet()) {
                BeanUtils.setProperty(subObject, (String) subKey, subValues.get(subKey));
            }

            // setting the sub object in bean main object.
            BeanUtils.setProperty(mainObject, mainKey, subObject);
        } else {
            // setting the value in bean main object.
            BeanUtils.setProperty(mainObject, mainKey, values.get(mainKey));
        }
    }
    return mainObject;
}

From source file:ml.shifu.shifu.util.ClassUtils.java

public static Field getDeclaredFieldIncludeSuper(String fieldName, Class<?> clazz) {
    String key = clazz.getName() + "#" + fieldName;
    Field cacheField = FIELD_CACHE.get(key);
    if (cacheField != null) {
        return cacheField;
    }/*  w  w  w . j av  a  2s  .  c om*/
    for (Field field : ClassUtils.getAllFields(clazz)) {
        if (field.getName().equals(fieldName)) {
            return field;
        }
    }
    return null;
}

From source file:com.stratio.deep.commons.utils.AnnotationUtils.java

/**
 * Returns the field name as known by the datastore. If the provided field object DeepField annotation
 * specifies the fieldName property, the value of this property will be returned, otherwise the java field name
 * will be returned.//from w w  w  .  jav a  2s.com
 *
 * @param field the Field object associated to the property for which we want to resolve the name.
 * @return the field name.
 */
public static String deepFieldName(Field field) {

    DeepField annotation = field.getAnnotation(DeepField.class);
    if (StringUtils.isNotEmpty(annotation.fieldName())) {
        return annotation.fieldName();
    } else {
        return field.getName();
    }
}

From source file:de.cbb.mplayer.mapping.MappingUtil.java

private static void mapFieldToEntity(Field field, Object presenter, Object entity) {
    try {//w ww. j ava2s.c o m
        if (!Control.class.isAssignableFrom(field.getType()))
            return;
    } catch (Exception ex) {
        log.debug("mapFieldToEntity failed", ex);
    }
    try {
        String fieldname = field.getName();
        field.setAccessible(true);
        Object value1 = null;

        ValueWrapper wrapper = MappingFactory.buildValueWrapper(presenter, field, value1, entity, fieldname);
        if (wrapper == null)
            return;
        value1 = wrapper.getValue();

        PropertyUtils.setSimpleProperty(entity, fieldname, value1);
    } catch (Exception ex) {
        log.debug("Unmapped control: " + field.getName() + " [" + ex.toString() + "]");
    }
}

From source file:com.shazam.shazamcrest.matcher.GsonProvider.java

private static void markSetAndMapFields(final GsonBuilder gsonBuilder) {
    gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
        @Override//from   ww w .  j  a  v a  2 s. co  m
        public String translateName(Field f) {
            if (Set.class.isAssignableFrom(f.getType()) || Map.class.isAssignableFrom(f.getType())) {
                return MARKER + f.getName();
            }
            return f.getName();
        }
    });
}