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 void init() {
    try {//from  ww w  .j  ava2s.c o m
        Class AppOpsManager = Class.forName("android.app.AppOpsManager");
        Field field = AppOpsManager.getDeclaredField("OP_SYSTEM_ALERT_WINDOW");
        field.setAccessible(true);
        OP_SYSTEM_ALERT_WINDOW = field.getInt(null);
    } catch (ClassNotFoundException e2) {

    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void startActivityForResult(Fragment fragment, Intent intent, int requestCode, Bundle options) {
    if (Build.VERSION.SDK_INT >= 19) {
        if ((requestCode & 0xffff0000) != 0) {
            throw new IllegalArgumentException("Can only use lower 16 bits" + " for requestCode");
        }/*w  w w  . ja  v a 2 s.c  o  m*/
        if (requestCode != -1) {
            try {
                Field mIndex = Fragment.class.getDeclaredField("mIndex");
                mIndex.setAccessible(true);
                requestCode = ((mIndex.getInt(fragment) + 1) << 16) + (requestCode & 0xffff);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        ActivityCompat.startActivityForResult(fragment.getActivity(), intent, requestCode, options);
    } else {
        fragment.getActivity().startActivityFromFragment(fragment, intent, requestCode);
    }
}

From source file:ReflectionHelper.java

/**
 * Set the value of the field of the object to the given value.
 * /* ww w  . j av a  2  s . c om*/
 * @param original
 *            object to modify
 * @param field
 *            field to modify
 * @param value
 *            new value for the given object and field
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public static void setStaticFinalField(Object original, Field field, Object value)
        throws NoSuchFieldException, IllegalAccessException {
    // we mark the field to be public
    field.setAccessible(true);
    // next we change the modifier in the Field instance to
    // not be final anymore, thus tricking reflection into
    // letting us modify the static final field
    Field modifiersField = Field.class.getDeclaredField(MODIFIERS_FIELD);
    modifiersField.setAccessible(true);
    int modifiers = modifiersField.getInt(field);
    // blank out the final bit in the modifiers int
    modifiers &= ~Modifier.FINAL;
    modifiersField.setInt(field, modifiers);
    FieldAccessor fa = reflection.newFieldAccessor(field, false);
    fa.set(original, value);
}

From source file:Main.java

public static final int getStaticIntValue(String paramString, int paramInt) {
    Field localField = getField(paramString);
    if (localField == null) {
        return paramInt;
    } else {//w  ww  .  j  av a  2  s. co  m
        try {
            paramInt = localField.getInt((Object) null);
            return paramInt;
        } catch (IllegalArgumentException var4) {
            return paramInt;
        } catch (IllegalAccessException var5) {
            return paramInt;
        }
    }
}

From source file:Main.java

public static int getMoveType(PackageInfo pInfo, ApplicationInfo info) {
    int moveType = MOVEAPPTYPE_NONE;
    if ((FLAG_EXTERNAL_STORAGE & info.flags) != 0) {
        moveType = MOVEAPPTYPE_MOVETOPHONE;
    } else if (pInfo != null) {
        int installLocation = 1;
        try {//from www  . j a  va  2s  .  com
            Field field = pInfo.getClass().getDeclaredField("installLocation");
            field.setAccessible(true);
            installLocation = field.getInt(pInfo);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if (installLocation == 0) {
            moveType = MOVEAPPTYPE_MOVETOSDCARD;
        } else if (installLocation == -1 || installLocation == 1) {
            moveType = MOVEAPPTYPE_NONE;
        }
    }
    return moveType;
}

From source file:Main.java

public static int getMoveType(PackageInfo pInfo, ApplicationInfo info) {
    int moveType = MOVEAPPTYPE_NONE;
    if ((FLAG_EXTERNAL_STORAGE & info.flags) != 0) {
        moveType = MOVEAPPTYPE_MOVETOPHONE;
    } else if (pInfo != null) {
        int installLocation = 1;
        try {//w  ww.  jav  a2  s.  co m
            Field field = pInfo.getClass().getDeclaredField("installLocation");
            field.setAccessible(true);
            installLocation = field.getInt(pInfo);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if (installLocation == 0) {
            moveType = MOVEAPPTYPE_MOVETOSDCARD;
        } else if (installLocation == -1 || installLocation == 1) {
            moveType = MOVEAPPTYPE_NONE;
        }
    }
    System.out.println("moveType..............." + moveType);
    return moveType;
}

From source file:Main.java

public static final int getIntValue(Object classObject, String paramString, int paramInt) {
    setClass(classObject.getClass().getName());
    Field localField = getField(paramString);
    if (localField != null) {
        return paramInt;
    } else {/*from  w  ww .  j  a  v a2s.  co m*/
        try {
            paramInt = localField.getInt(classObject);
            return paramInt;
        } catch (IllegalArgumentException var5) {
            return paramInt;
        } catch (IllegalAccessException var6) {
            return paramInt;
        }
    }
}

From source file:Main.java

/**
 * Returns the Jarvis Api Version or -1 if it is not supported
 * @return api version//from   ww  w . j  av a 2  s  . co  m
 */
public static int getJarvisApiVersion() {
    Field[] declaredFields = Build.class.getDeclaredFields();
    for (Field field : declaredFields) {
        if (Modifier.isStatic(field.getModifiers()) && field.getName().equals("JARVIS_VERSION")) {
            try {
                return field.getInt(null);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            }
        }
    }
    return -1;
}

From source file:com.puppycrawl.tools.checkstyle.utils.TokenUtils.java

/**
 * 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. Does not throw any checked exceptions.
 * @param field from which the int should be extracted
 * @param object to extract the int value from
 * @return the value of the field converted to type int
 * @throws IllegalStateException if this Field object is enforcing Java language access control
 *         and the underlying field is inaccessible
 * @see Field#getInt(Object)//w  ww  .  j ava 2  s  .  c  o  m
 */
public static int getIntFromField(Field field, Object object) {
    try {
        return field.getInt(object);
    } catch (final IllegalAccessException exception) {
        throw new IllegalStateException(exception);
    }
}

From source file:nl.b3p.catalog.arcgis.ArcObjectsInitializer.java

private static int[] getProductCodeIntegers(String[] productCodes) {
    Class clazz = esriLicenseProductCode.class;

    List<Integer> codes = new ArrayList<Integer>();
    for (String c : productCodes) {
        try {/*from  w w  w  . ja v a  2s  . c om*/
            Field f = clazz.getDeclaredField("esriLicenseProductCode" + c);
            codes.add(f.getInt(null));
        } catch (Exception e) {
            log.warn("Invalid product code: " + c);
        }
    }
    int[] c = new int[codes.size()];
    for (int i = 0; i < c.length; i++) {
        c[i] = codes.get(i);
    }
    return c;
}