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:Main.java

private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    String methodName = null;/*w w  w  .j av a 2  s .co  m*/
    if (isStartWithIs(field.getName())) {
        methodName = "set" + fieldName.substring(2, 3).toUpperCase() + fieldName.substring(3);
    } else {
        methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }
    try {
        return entityType.getDeclaredMethod(methodName, field.getType());
    } catch (NoSuchMethodException e) {
        //Logger.d(methodName + " not exist");
    }
    return null;
}

From source file:Main.java

private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    String methodName = null;// w  w  w.j a v  a 2  s. c  om
    if (isStartWithIs(field.getName())) {
        methodName = "set" + fieldName.substring(2, 3).toUpperCase() + fieldName.substring(3);
    } else {
        methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }
    try {
        return entityType.getDeclaredMethod(methodName, field.getType());
    } catch (NoSuchMethodException e) {
        Log.d("getBooleanColumnSetMethod", methodName + " not exist");
    }
    return null;
}

From source file:Main.java

/**
 * Attempt to find a {@link Field field} on the supplied {@link Class} with the
 * supplied <code>name</code>. Searches all superclasses up to {@link Object}.
 *
 * @param clazz the class to introspect//from w  ww  . j a  va 2 s  .  c o  m
 * @param name  the name of the field
 * @return the corresponding Field object, or <code>null</code> if not found
 */
public static Field findField(Class<?> clazz, String name) {
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Field[] fields = searchType.getDeclaredFields();
        for (Field field : fields) {
            if (name.equals(field.getName())) {
                field.setAccessible(true);
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:Main.java

public static void classInspector(Object object) {
    Class cls = object.getClass();
    System.out.println(cls.toString());
    Field[] fieldlist = cls.getDeclaredFields();
    for (int i = 0; i < fieldlist.length; i++) {
        Field fld = fieldlist[i];

        System.out.println("name = " + fld.getName());
        System.out.println("decl class = " + fld.getDeclaringClass());
        System.out.println("type = " + fld.getType());
        System.out.println("-----");
    }//from w w w. j a  v a 2  s. co  m
}

From source file:Main.java

public static void doGetAllField(Class<?> type, List<Field> fields, boolean ignoreSuperClasses) {
    for (Field newField : type.getDeclaredFields()) {
        boolean found = false;
        for (Field oldField : fields) {
            if (oldField.getName().equals(newField.getName())) {
                found = true;/*from www  . j  a v  a 2 s  .  c om*/
                break;
            }
        }
        if (!found) {
            fields.add(newField);
        }
    }

    if (type.getSuperclass() != null && !ignoreSuperClasses) {
        doGetAllField(type.getSuperclass(), fields, ignoreSuperClasses);
    }
}

From source file:Main.java

public static String toString(Field field) {
    StringBuffer sb = new StringBuffer(field.getName());
    sb.append("(").append(field.getName()).append(")");
    return sb.toString();
}

From source file:Main.java

private static void sortFieldsByName(Field[] array) {
    Arrays.sort(array, new Comparator<Field>() {
        @Override//  ww  w . j  a v a 2  s. c  om
        public int compare(Field current, Field next) {
            String currentName = current.getName();
            String nextName = next.getName();
            return currentName.compareTo(nextName);
        }
    });
}

From source file:Main.java

/**
 * Set the fields' value.//w  ww  . j  a  v a 2  s  .c om
 * 
 * @param bean
 * @param valMap
 */
public static void setFieldValues(Object bean, Map<String, Object> valMap) {
    Class<?> cls = bean.getClass();
    //Get all fields.
    Field[] fields = cls.getDeclaredFields();

    for (Field field : fields) {
        if (valMap.containsKey(field.getName())) {
            field.setAccessible(true);
            try {
                field.set(bean, valMap.get(field.getName()));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static Map<String, String> convertBeanToMap(Object bean)
        throws IllegalArgumentException, IllegalAccessException {
    Field[] fields = bean.getClass().getDeclaredFields();
    HashMap<String, String> data = new HashMap<String, String>();
    for (Field field : fields) {
        field.setAccessible(true);//  w  ww  . j  a v  a  2 s.  c  om
        data.put(field.getName(), (String) field.get(bean));
    }
    return data;
}

From source file:Main.java

public static boolean isObf() {
    try {/*w  ww  . j  av a 2 s  . c  om*/
        Field[] fields = Class.forName("net.minecraft.world.World").getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            if (f.getName().equalsIgnoreCase("loadedEntityList"))
                return false;
        }
    } catch (Exception e) {
        return true;
    }
    return true;
}