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 Field getField(Field[] fields, String fieldName) {
    for (final Field field : fields) {
        if (fieldName.equals(field.getName())) {
            return field;
        }/*from  w ww . j  av  a 2  s.  c o m*/
    }
    return null;
}

From source file:Main.java

private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field, Class<?> typeClass,
        String suffix) {/*from  ww  w.j a va  2 s. c o m*/
    String fieldName = field.getName();
    String methodName = null;
    if (isStartWithIs(field.getName())) {
        methodName = "set" + fieldName.substring(2, 3).toUpperCase(Locale.getDefault()) + fieldName.substring(3)
                + suffix;
    } else {
        methodName = "set" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault()) + fieldName.substring(1)
                + suffix;
    }
    try {
        return entityType.getDeclaredMethod(methodName, typeClass);
    } catch (NoSuchMethodException e) {
        Log.d("L", methodName + " not exist");
    }
    return null;
}

From source file:Main.java

public static String getMobileInfo() {
    StringBuffer sb = new StringBuffer();
    try {/*from   w ww.  j av a 2 s  .c o  m*/

        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            String value = field.get(null).toString();
            sb.append(name + "=" + value);
            sb.append("\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

private static void loadApplicationResources(Context context, Map<String, String> iconPackResources,
        String packageName) {//ww  w.  ja v  a 2  s .  co m
    Field[] drawableItems = null;
    try {
        Context appContext = context.createPackageContext(packageName,
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        drawableItems = Class.forName(packageName + ".R$drawable", true, appContext.getClassLoader())
                .getFields();
    } catch (Exception e) {
        return;
    }
    for (Field f : drawableItems) {
        String name = f.getName();

        String icon = name.toLowerCase();
        name = name.replaceAll("_", ".");

        iconPackResources.put(name, icon);

        int activityIndex = name.lastIndexOf(".");
        if (activityIndex <= 0 || activityIndex == name.length() - 1) {
            continue;
        }

        String iconPackage = name.substring(0, activityIndex);
        if (TextUtils.isEmpty(iconPackage)) {
            continue;
        }
        iconPackResources.put(iconPackage, icon);

        String iconActivity = name.substring(activityIndex + 1);
        if (TextUtils.isEmpty(iconActivity)) {
            continue;
        }
        iconPackResources.put(iconPackage + "." + iconActivity, icon);
    }
}

From source file:Main.java

public static <T> JSONArray beansToJsonArray(T... beans)
        throws IllegalArgumentException, IllegalAccessException, JSONException {
    JSONArray jArr = new JSONArray();
    for (T bean : beans) {
        JSONObject jObj = new JSONObject();
        Field[] fields = bean.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);// www .  ja  va2  s . c  o m
            String name = field.getName();
            Object value = field.get(bean);

            jObj.put(name, value);
        }
        jArr.put(jObj);
    }
    return jArr;
}

From source file:Main.java

private static boolean compareSignatures(Field first, Field second) {
    return first.getName().equals(second.getName());
}

From source file:Main.java

public static Method findGetterMethodForField(Field field, Class<?> objectClass) throws NoSuchMethodException {
    String getterMethodName = "get" + field.getName().toUpperCase().substring(0, 1)
            + field.getName().substring(1);
    return objectClass.getMethod(getterMethodName, (java.lang.Class[]) null);
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Map<String, Class> getClassFields(Class clazz, boolean includeParentClass) {

    Map<String, Class> map = new HashMap<String, Class>();

    Field[] fields = clazz.getDeclaredFields();

    for (Field field : fields) {

        map.put(clazz.getName() + "." + field.getName(), field.getType());// field.getType()

    }//from  w  ww.  j  a va2 s  .co  m

    if (includeParentClass)

        getParentClassFields(map, clazz.getSuperclass());

    return map;

}

From source file:Main.java

private static Method getColumnSetMethod(Class<?> entityType, Field field, Class<?> typeClass, String suffix) {
    String fieldName = field.getName();
    Method setMethod = null;/*from w  ww. jav  a 2s .c om*/
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field, typeClass, suffix);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault())
                + fieldName.substring(1) + suffix;
        try {
            setMethod = entityType.getDeclaredMethod(methodName, typeClass);
        } catch (NoSuchMethodException e) {
            Log.d("T", methodName + " not exist");
        }
    }
    return setMethod;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Map<String, Class> getParentClassFields(Map<String, Class> map, Class clazz) {

    Field[] fields = clazz.getDeclaredFields();

    for (Field field : fields) {

        map.put(clazz.getName() + "." + field.getName(), field.getType());

    }//w w w  .ja v a2s  .c  om

    if (clazz.getSuperclass() == null) {

        return map;

    }

    getParentClassFields(map, clazz.getSuperclass());

    return map;

}