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:io.tilt.minka.utils.Defaulter.java

/**
 * @param   props the properties instance to look up keys for 
 * @param   configurable /*  w ww .j  a  v a2s  .com*/
 *  applying object with pairs of "default" sufixed static fields in the format "some_value_default"
 *  and instance fields in the propercase format without underscores like "someValue" 
 * 
 * @return  TRUE if all defaults were applied. FALSE if some was not !
 */
public static boolean apply(final Properties props, Object configurable) {
    Validate.notNull(props);
    Validate.notNull(configurable);
    boolean all = true;
    for (final Field staticField : getStaticDefaults(configurable.getClass())) {
        final String name = properCaseIt(staticField.getName());
        if (staticField.getName().endsWith(SUFFIX)) {
            final String nameNoDef = name.substring(0, name.length() - SUFFIX.length());
            try {
                final Field instanceField = configurable.getClass().getDeclaredField(nameNoDef);
                try {
                    final PropertyEditor editor = edit(props, configurable, staticField, instanceField);
                    instanceField.setAccessible(true);
                    instanceField.set(configurable, editor.getValue());
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    all = false;
                    logger.error("Defaulter: object <{}> cannot set value for field: {}",
                            configurable.getClass().getSimpleName(), nameNoDef, e);
                }
            } catch (NoSuchFieldException | SecurityException e) {
                all = false;
                logger.error("Defaulter: object <{}> has no field: {} for default static: {} (reason: {})",
                        configurable.getClass().getSimpleName(), nameNoDef, staticField.getName(),
                        e.getClass().getSimpleName());
            }
        }
    }
    return all;
}

From source file:MySuperClass.java

public static ArrayList<String> getFieldsDesciption(Field[] fields) {
    ArrayList<String> fieldList = new ArrayList<>();

    for (Field f : fields) {
        int mod = f.getModifiers() & Modifier.fieldModifiers();
        String modifiers = Modifier.toString(mod);

        Class<?> type = f.getType();
        String typeName = type.getSimpleName();

        String fieldName = f.getName();

        fieldList.add(modifiers + "  " + typeName + "  " + fieldName);
    }/*  ww  w  .  j  a  v  a  2s .c  o m*/

    return fieldList;
}

From source file:net.eledge.android.toolkit.db.internal.SQLBuilder.java

public static String getIdField(Class<?> clazz) {
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            Column column = field.getAnnotation(Column.class);
            return StringUtils.defaultIfBlank(column.name(), field.getName().toLowerCase(Locale.ENGLISH));
        }//from w w w . j av a 2s  .c  o m
    }
    return null;
}

From source file:com.bsi.summer.core.util.ReflectionUtils.java

/**
 * ?, ?DeclaredField,   ?filedName//from   w w  w.  j av a  2s.c o m
 * 
 * ?Object?, null.
 */
public static String getAccessibleFieldName(final Object obj, final String fieldName) {
    Assert.notNull(obj, "object?");
    Assert.hasText(fieldName, "fieldName");
    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        Field fields[] = superClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().toUpperCase().equals(fieldName.toUpperCase())) {
                return field.getName();
            }
        }
    }
    return null;
}

From source file:com.qrmedia.commons.persistence.hibernate.clone.HibernateEntityBeanCloner.java

private static Collection<String> getFieldNames(Collection<Field> fields) {
    Collection<String> fieldNames = new ArrayList<String>(fields.size());

    for (Field field : fields) {
        fieldNames.add(field.getName());
    }/*w ww  . jav  a  2s  . com*/

    return fieldNames;
}

From source file:fi.jumi.core.util.EqualityMatchers.java

private static String findDifference(String path, Object obj1, Object obj2) {
    if (obj1 == null || obj2 == null) {
        return obj1 == obj2 ? null : path;
    }/*  w ww  . j av a 2s.c o m*/
    if (obj1.getClass() != obj2.getClass()) {
        return path;
    }

    // collections have a custom equals, but we want deep equality on every collection element
    // TODO: support other collection types? comparing Sets should be order-independent
    if (obj1 instanceof List) {
        List<?> col1 = (List<?>) obj1;
        List<?> col2 = (List<?>) obj2;

        int size1 = col1.size();
        int size2 = col2.size();
        if (size1 != size2) {
            return path + ".size()";
        }

        for (int i = 0; i < Math.min(size1, size2); i++) {
            String diff = findDifference(path + ".get(" + i + ")", col1.get(i), col2.get(i));
            if (diff != null) {
                return diff;
            }
        }
        return null;
    }

    // use custom equals method if exists
    try {
        Method equals = obj1.getClass().getMethod("equals", Object.class);
        if (equals.getDeclaringClass() != Object.class) {
            return obj1.equals(obj2) ? null : path;
        }
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }

    // arrays
    if (obj2.getClass().isArray()) {
        int length1 = Array.getLength(obj1);
        int length2 = Array.getLength(obj2);
        if (length1 != length2) {
            return path + ".length";
        }

        for (int i = 0; i < Math.min(length1, length2); i++) {
            String diff = findDifference(path + "[" + i + "]", Array.get(obj1, i), Array.get(obj2, i));
            if (diff != null) {
                return diff;
            }
        }
        return null;
    }

    // structural equality
    for (Class<?> cl = obj2.getClass(); cl != null; cl = cl.getSuperclass()) {
        for (Field field : cl.getDeclaredFields()) {
            try {
                String diff = findDifference(path + "." + field.getName(),
                        FieldUtils.readField(field, obj1, true), FieldUtils.readField(field, obj2, true));
                if (diff != null) {
                    return diff;
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return null;
}

From source file:com.px100systems.util.PropertyAccessor.java

@SuppressWarnings("unused")
public static String findAnnotatedField(Class<?> topClass, Class<? extends Annotation> annotation) {
    for (Class<?> cls = topClass; cls != null; cls = cls.getSuperclass())
        for (Field field : cls.getDeclaredFields())
            if (field.getAnnotation(annotation) != null)
                return field.getName();
    return null;/*from   w  w  w.ja  v a  2s  .c  o m*/
}

From source file:it.nicola_amatucci.util.Json.java

public static <T> T object_from_json(JSONObject json, Class<T> objClass) throws Exception {
    T t = null;//from  www . jav a 2  s .  com
    Object o = null;

    try {
        //create new object instance
        t = objClass.newInstance();

        //object fields
        Field[] fields = objClass.getFields();

        for (Field field : fields) {
            //field name
            o = json.get(field.getName());

            if (o.equals(null))
                continue;

            //field value
            try {
                String typeName = field.getType().getSimpleName();

                if (typeName.equals("String")) {
                    o = json.getString(field.getName()); //String
                } else if (typeName.equals("boolean")) {
                    o = Integer.valueOf(json.getInt(field.getName())); //boolean
                } else if (typeName.equals("int")) {
                    o = Integer.valueOf(json.getInt(field.getName())); //int
                } else if (typeName.equals("long")) {
                    o = Long.valueOf(json.getLong(field.getName())); //long                  
                } else if (typeName.equals("double")) {
                    o = Double.valueOf(json.getDouble(field.getName())); //double
                } else if (typeName.equals("Date")) {
                    o = new SimpleDateFormat(Json.DATA_FORMAT).parse(o.toString()); //data
                } else if (field.getType().isArray()) {
                    JSONArray arrayJSON = new JSONArray(o.toString());
                    T[] arrayOfT = (T[]) null;

                    try {
                        //create object array
                        Class c = Class.forName(field.getType().getName()).getComponentType();
                        arrayOfT = (T[]) Array.newInstance(c, arrayJSON.length());

                        //parse objects                  
                        for (int i = 0; i < json.length(); i++)
                            arrayOfT[i] = (T) object_from_json(arrayJSON.getJSONObject(i), c);
                    } catch (Exception e) {
                        throw e;
                    }

                    o = arrayOfT;
                } else {
                    o = object_from_json(new JSONObject(o.toString()), field.getType()); //object
                }

            } catch (Exception e) {
                throw e;
            }

            t.getClass().getField(field.getName()).set(t, o);
        }

    } catch (Exception e) {
        throw e;
    }

    return t;
}

From source file:com.tridion.storage.si4t.JPASearchDAOFactory.java

private static Field getPrivateFieldRec(final Class<?> clazz, final String fieldName, Logger log) {
    for (Field field : clazz.getDeclaredFields()) {
        if (fieldName.equals(field.getName())) {
            return field;
        }/*from w w w. j a  v a  2 s  .  c om*/
    }
    final Class<?> superClazz = clazz.getSuperclass();

    if (superClazz != null) {
        return getPrivateFieldRec(superClazz, fieldName, log);
    }

    return null;
}

From source file:fit.Binding.java

private static Field findField(Fixture fixture, String simpleName) {
    Field[] fields = getAllDeclaredFields(fixture.getTargetClass());
    Field field = null;/* w  w w. j  a va 2s  .  co m*/
    for (int i = 0; i < fields.length; i++) {
        Field possibleField = fields[i];
        if (simpleName.equals(possibleField.getName().toLowerCase())) {
            field = possibleField;
            break;
        }
    }
    return field;
}