Example usage for java.lang.reflect Field getInt

List of usage examples for java.lang.reflect Field getInt

Introduction

In this page you can find the example usage for java.lang.reflect Field getInt.

Prototype

@CallerSensitive
@ForceInline 
public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.

Usage

From source file:Main.java

public static int getConstantValue(Class<View> object, String constantName) {
    int value = -1;
    try {//from ww  w .  jav  a  2 s .  c o m
        Field field = object.getField(constantName);
        value = field.getInt(object);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static int getIntFieldIfExists(Class<?> klass, String fieldName, Class<?> obj, int defaultVal) {
    try {/* ww w.  j a  v a2 s .c  o m*/
        Field f = klass.getDeclaredField(fieldName);
        return f.getInt(obj);
    } catch (Exception e) {
        return defaultVal;
    }
}

From source file:Main.java

/**
 * Gets resource ID of the resource/*www.  j a v a  2s  . c  om*/
 * @param resourceName  resource name
 * @param clazz an instance of Class, e.g. R.drawable.class
 * @return  an resource ID
 */
public static int getId(String resourceName, Class<?> clazz) {
    try {
        Field idField = clazz.getDeclaredField(resourceName);
        return idField.getInt(idField);
    } catch (Exception e) {
        throw new RuntimeException("No resource ID found for: " + resourceName + " / " + clazz, e);
    }
}

From source file:Main.java

public static int getScreenOrieLockedValue(Class<ActivityInfo> object) {
    int value = -1;
    try {//w w  w  . ja v a2  s . c  o m
        Field field = object.getField("SCREEN_ORIENTATION_LOCKED");
        value = field.getInt(object);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static int getResourceId(String variableName, Context context, Class<?> clazz) {
    try {//  ww  w .j  a  va2  s. co  m
        Field field = clazz.getDeclaredField(variableName);
        return field.getInt(field);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static int getFieldIntSafely(Class clz, String fieldName, Object instance) {
    try {/*from  ww w .  j ava2  s .co m*/
        Field field = clz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.getInt(instance);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

private static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
    Class<? extends Window> clazz = activity.getWindow().getClass();
    try {//from   w w w.  j a va  2  s  .c  o  m
        int darkModeFlag = 0;
        Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
        Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
        darkModeFlag = field.getInt(layoutParams);
        Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
        extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String getStringByName(String name, Context context) {
    try {/* www.  j a v a  2 s . co m*/
        Field field = Class.forName("com.poetic.emotion.R$string").getField(name);
        int drawableRes = field.getInt(field);
        return context.getString(drawableRes);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Integer getPID(Process process) {
    Integer pid = null;//from  w  w w.ja  v a  2 s .c om
    if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
        /* get the PID on unix/linux systems */
        try {
            Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);
        } catch (Throwable e) {
        }
    }
    return pid;
}

From source file:Main.java

private static boolean apiLevelIsAtLeastEclair() {
    // we need to at least be running on cupcake for QuickContactBadges.
    boolean isAtLeastEclair = false;
    try {//from   w w w  .  j  a  v a 2 s  .  com
        Field verField = Class.forName("android.os.Build$VERSION").getField("SDK_INT");
        int sdkInt = verField.getInt(verField);
        isAtLeastEclair = (sdkInt >= 5);
    } catch (Exception e) {
        try {
            Field verField = Class.forName("android.os.Build$VERSION").getField("SDK");
            String sdk = (String) verField.get(verField);
            isAtLeastEclair = (Integer.parseInt(sdk) >= 5);
        } catch (Exception e2) {
            isAtLeastEclair = false;
        }
    }
    Log.i("CompatibilityHelp", "api level at least eclair? " + isAtLeastEclair);
    return isAtLeastEclair;
}