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:net.eledge.android.toolkit.db.internal.SQLBuilder.java

public static String getFieldName(Field field, Column column) {
    return StringUtils.defaultIfBlank(column.name(), field.getName().toLowerCase(Locale.ENGLISH));
}

From source file:Main.java

public static Field findFiledWithName(Class clazz, String filedName) {
    Field[] fields = getAllFiedFromClassAndSuper(clazz, false);
    if (fields != null) {
        for (Field field : fields) {
            field.setAccessible(true);/* w  w w. j  a  v a2s  .  co m*/
            if (field.getName().equals(filedName)) {
                return field;
            }
        }
    }
    return null;
}

From source file:ShowClass.java

public static void printField(Field f) {
    System.out.println("  " + modifiers(f.getModifiers()) + typeName(f.getType()) + " " + f.getName() + ";");
}

From source file:com.vmware.qe.framework.datadriven.impl.injector.DataInjectorUsingReflection.java

private static Field getField(Class<?> clazz, String fieldName) {
    Class<?> tmpClass = clazz;
    do {/*from ww  w  . j a v a  2s .c o m*/
        for (Field field : tmpClass.getDeclaredFields()) {
            String candidateName = field.getName();
            if (!candidateName.equals(fieldName)) {
                continue;
            }
            field.setAccessible(true);
            return field;
        }
        tmpClass = tmpClass.getSuperclass();
    } while (clazz != null);
    throw new RuntimeException("Field '" + fieldName + "' not found on class " + clazz);
}

From source file:Main.java

public static void print_field(Field f) {
    System.out.println("  " + modifiers(f.getModifiers()) + typename(f.getType()) + " " + f.getName() + ";");
}

From source file:de.micromata.mgc.javafx.FXGuiUtils.java

public static void resetErroneousFields(AbstractModelController<?> controller) {
    List<Field> fields = PrivateBeanUtils.findAllFields(controller.getClass(), CommonMatchers
            .and(FieldMatchers.hasNotModifier(Modifier.STATIC), FieldMatchers.assignableTo(Node.class)));
    for (Field field : fields) {
        if (field.getName().equals("thisNode") == true) {
            continue;
        }/*from   ww w.  j  a  v a 2s .c  o m*/
        Node node = (Node) PrivateBeanUtils.readField(controller, field);
        FXCssUtil.replaceStyleClass(CSS_CLASS_ERROR, CSS_CLASS_VALID, node);
    }

}

From source file:Main.java

public static Object isEmpty(Object obj, Class clazz) {
    if (obj == null || clazz == null) {
        return obj;
    }/*  www. j a  va2 s .c  o m*/
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        try {
            String fieldName = field.getName();
            if (fieldName == null || fieldName.length() == 0)
                continue;
            String fieldMethodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            String getfieldName = "get" + fieldMethodName;
            Method getMethod = clazz.getMethod(getfieldName, null);
            if (getMethod == null)
                continue;
            Object value = getMethod.invoke(obj, null);
            Class type = field.getType();
            if (type == String.class) {
                if (value == null || "null".equals(value.toString()) || value.toString().trim().length() == 0) {
                    value = "";
                    String setfieldName = "set" + fieldMethodName;
                    Method setMethod = clazz.getMethod(setfieldName, String.class);
                    if (setMethod == null)
                        continue;
                    setMethod.invoke(obj, value);
                }
            } else if (type == Integer.class || type == int.class || type == Float.class || type == float.class
                    || type == Double.class || type == double.class) {
                if (value == null || "null".equals(value.toString()) || value.toString().length() == 0) {
                    String setfieldName = "set" + fieldMethodName;
                    Method setMethod = clazz.getMethod(setfieldName, type);
                    setMethod.invoke(obj, 0);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
    }
    return obj;
}

From source file:NestUtil.java

/**
 * Get all fields of a class./*from   w w  w.j a  va2  s  .  c  om*/
 * 
 * @param clazz The class.
 * @return All fields of a class.
 */
public static Collection<Field> getFields(Class<?> clazz) {
    //      if (log.isDebugEnabled()) {
    //      log.debug("getFields(Class<?>) - start");
    //}

    Map<String, Field> fields = new HashMap<String, Field>();
    while (clazz != null) {
        for (Field field : clazz.getDeclaredFields()) {
            if (!fields.containsKey(field.getName())) {
                fields.put(field.getName(), field);
            }
        }

        clazz = clazz.getSuperclass();
    }

    Collection<Field> returnCollection = fields.values();
    //   if (log.isDebugEnabled()) {
    //   log.debug("getFields(Class<?>) - end");
    //   }
    return returnCollection;
}

From source file:Main.java

public static Map<String, String> beanToMap(Object object) {
    Map<String, String> params = new HashMap<>();
    if (null == object) {
        return params;
    }//w w  w. j a v a2 s  .  c om
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object fieldObj = field.get(object);
            if (fieldObj != null && fieldObj.getClass() == String.class) {
                String fieldValue = (String) fieldObj;
                if (!TextUtils.isEmpty(fieldValue)) {
                    if (!TextUtils.isEmpty(fieldValue)) {
                        fieldValue = fieldValue.replaceAll("\"", "%22");
                    }
                    params.put(fieldName, fieldValue);
                }
            }
        }
        return params;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return params;
}

From source file:Main.java

/**
 * @param type/*from   w w  w. ja v a2  s . c om*/
 * @param object
 * @param field
 * @return
 */
public static <T> Object getFieldValue(Class<T> type, T object, Field field) {
    try {
        Method m = type.getMethod(getGetIsPrefix(field) + getFirstLetterUppercased(field.getName()),
                (Class<?>[]) null);
        return m.invoke(object, (Object[]) null);
    } catch (Exception e) {
    }

    return null;
}