Example usage for java.lang Class getField

List of usage examples for java.lang Class getField

Introduction

In this page you can find the example usage for java.lang Class getField.

Prototype

@CallerSensitive
public Field getField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    if (sStatusBarHeight == 0) {
        try {/*from w ww .j  a  v a  2s.  c om*/
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object o = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = (Integer) field.get(o);
            sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sStatusBarHeight;
}

From source file:Main.java

private static void putKeyValue(Class<?> clazz, String buildField, SortedMap<String, String> keysToValues) {
    try {/*from  w w  w  . j a  v  a 2 s .  c o m*/
        Field field = clazz.getField(buildField);
        Object value = field.get(null);
        String key = clazz.getSimpleName().toLowerCase() + "." + buildField.toLowerCase();
        keysToValues.put(key, String.valueOf(value));
    } catch (SecurityException | NoSuchFieldException | IllegalAccessException e) {
        // ignore
    }
}

From source file:Main.java

public static Field findFields(Class<?> clazz, String fieldName) {
    Field result = null;//  w w  w  .j av a  2s . com
    if (clazz != null) {
        try {
            result = clazz.getField(fieldName);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static int getResourceIdByName(Context context, String className, String name) {
    int id = 0;/*from  www.j  a va 2 s .  co  m*/
    if (context == null) {
        return id;
    } else {
        String packageName = context.getPackageName();

        try {
            String var10 = packageName + ".R$" + className;
            Class desireClass = Class.forName(var10);
            if (desireClass != null) {
                id = desireClass.getField(name).getInt(desireClass);
            }
        } catch (ClassNotFoundException var7) {
            Log.d("dou361", "ClassNotFoundException: class=" + className + " fieldname=" + name);
        } catch (IllegalArgumentException var8) {
            Log.d("dou361", "IllegalArgumentException: class=" + className + " fieldname=" + name);
        } catch (SecurityException var9) {
            Log.d("dou361", "SecurityException: class=" + className + " fieldname=" + name);
        } catch (IllegalAccessException var101) {
            Log.d("dou361", "IllegalAccessException: class=" + className + " fieldname=" + name);
        } catch (NoSuchFieldException var11) {
            Log.d("dou361", "NoSuchFieldException: class=" + className + " fieldname=" + name);
        }

        return id;
    }
}

From source file:Main.java

private static void putKeyValue(Class<?> clazz, String buildField, SortedMap<String, String> keysToValues) {
    try {/*from   ww  w .  j  av a2 s . c o m*/
        Field field = clazz.getField(buildField);
        Object value = field.get(null);
        String key = clazz.getSimpleName().toLowerCase() + "." + buildField.toLowerCase();
        keysToValues.put(key, String.valueOf(value));
    } catch (SecurityException e) {
        // ignore
    } catch (NoSuchFieldException e) {
        // ignore
    } catch (IllegalAccessException e) {
        // ignore
    }
}

From source file:Main.java

public static <ENUM extends Enum> ENUM fromValue(Class<ENUM> aClass, String value) {
    try {/*from  w ww .j  a v  a2  s  . c om*/
        for (ENUM c : aClass.getEnumConstants()) {
            Field field = aClass.getField(c.name());

            XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);

            if (annotation.value().equals(value)) {
                return c;
            }
        }
    } catch (NoSuchFieldException e) {
    }
    throw new IllegalArgumentException(value);
}

From source file:Main.java

public static int getStatusHeight(Context context) {

    int statusHeight = -1;
    try {// w ww.  j  a  va2  s . co  m
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

From source file:Main.java

public static boolean isMeizuSecurity(Context context) {
    if (!isSecVer(context)) {
        return false;
    }//from   w w  w  .  j av  a 2s .c  om
    Boolean valueOf = Boolean.valueOf(false);
    try {
        Boolean bool;
        Class cls = Class.forName("android.os.Build");
        Field field = cls.getField("MeizuSecurity");
        field.setAccessible(true);
        bool = (Boolean) field.get(cls);
        return bool;
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (Error e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return valueOf.booleanValue();
}

From source file:Main.java

/**
 * @param clz class name/* ww  w  .  j  a v  a 2  s . com*/
 * @param fieldName field name
 * @return the Field object if the class is exists, else null
 */
public static Field getField(Class clz, String fieldName) {
    if (null != clz) {
        try {
            return clz.getField(fieldName);
        } catch (Exception e) {
        }
        return (clz == Object.class ? null : getField(clz.getSuperclass(), fieldName));
    }
    return null;
}

From source file:org.openlegacy.utils.ReflectionUtil.java

public static Object getStaticFieldValue(Class<?> class1, String value) {
    try {//from  w w  w.  j  a  v  a2  s  . c  om
        Field field = class1.getField(value);
        return field.get(class1.newInstance());
    } catch (Exception e) {
        // do nothing
    }
    return null;
}