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:org.brucalipto.sqlutil.SQLUtilTypes.java

private final static Map fillJavaSQLTypes() {
    Map sqlTypes = new Hashtable();
    try {/*w  w w .  j  a  v a  2s  .  c om*/
        Field[] sqlTypeFields = SQLUtilTypes.class.getFields();
        if (sqlTypeFields == null) {
            sqlTypeFields = new Field[0];
        }
        for (int i = 0; i < sqlTypeFields.length; i++) {
            final Field sqlType = sqlTypeFields[i];
            final String sqlTypeName = sqlType.getName();
            if (!sqlTypeName.equals("SQL_TYPES")) {
                final int value = sqlType.getInt(sqlTypeName);
                log.debug("TypeName: '" + sqlTypeName + "'; TypeValue: '" + value + "'");
                sqlTypes.put(Integer.valueOf("" + value), sqlTypeName);
            }
        }
    } catch (Exception e) {
        log.error("Error adding java.sql.Types to SQLTypes map", e);
    }
    return sqlTypes;
}

From source file:com.bobomee.android.common.util.EditUtil.java

public static void tintCursorDrawable(EditText editText, int color) {
    try {/*from ww  w .j  av  a  2  s .co  m*/
        Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        fCursorDrawableRes.setAccessible(true);
        int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
        Field fEditor = TextView.class.getDeclaredField("mEditor");
        fEditor.setAccessible(true);
        Object editor = fEditor.get(editText);
        Class<?> clazz = editor.getClass();
        Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
        fCursorDrawable.setAccessible(true);

        if (mCursorDrawableRes <= 0) {
            return;
        }

        Drawable cursorDrawable = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
        if (cursorDrawable == null) {
            return;
        }

        Drawable tintDrawable = tintDrawable(cursorDrawable, ColorStateList.valueOf(color));
        Drawable[] drawables = new Drawable[] { tintDrawable, tintDrawable };
        fCursorDrawable.set(editor, drawables);
    } catch (Throwable ignored) {
    }
}

From source file:Main.java

public static Object getStaticField(Class clz, String fieldName, int type) {
    if (null != clz) {
        try {/*  w  ww.  j ava2  s  .  c om*/
            Field field = clz.getField(fieldName);
            switch (type) {
            case TYPE_OBJECT:
                return field.get(clz);
            case TYPE_INT:
                return field.getInt(clz);
            case TYPE_SHORT:
                return field.getShort(clz);
            case TYPE_BYTE:
                return field.getByte(clz);
            case TYPE_BOOLEAN:
                return field.getBoolean(clz);
            case TYPE_FLOAT:
                return field.getFloat(clz);
            case TYPE_LONG:
                return field.getLong(clz);
            case TYPE_DOUBLE:
                return field.getDouble(clz);
            default:
                return field.get(clz);
            }
        } catch (Exception e) {
        }
        return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type));
    }
    return getDefault(type);
}

From source file:Main.java

/**
 * Called internally by installGtkPopupBugWorkaround to fix the thickness
 * of a GTK style field by setting it to a minimum value of 1.
 * //from  www  .  j a  v a  2s  .  c o m
 * @param style
 *            The GTK style object.
 * @param fieldName
 *            The field name.
 * @throws Exception
 *             When reflection fails.
 */
private static void fixGtkThickness(Object style, String fieldName) throws Exception {
    Field field = style.getClass().getDeclaredField(fieldName);
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    field.setInt(style, Math.max(1, field.getInt(style)));
    field.setAccessible(accessible);
}

From source file:org.apache.ojb.broker.platforms.BlobWrapper.java

protected static int staticIntFieldValue(Field field) {
    int value = 0;
    try {//from w  w  w . j a  v  a 2 s  .  c om
        value = field.getInt(null);
    } catch (Exception ignore) {
        value = -1;
    }
    return value;
}

From source file:android.support.v7.internal.widget.DrawableUtils.java

/**
 * Allows us to get the optical insets for a {@link Drawable}. Since this is hidden we need to
 * use reflection. Since the {@code Insets} class is hidden also, we return a Rect instead.
 *///from  w  ww .j  a va  2  s  .  com
public static Rect getOpticalBounds(Drawable drawable) {
    if (sInsetsClazz != null) {
        try {
            // If the Drawable is wrapped, we need to manually unwrap it and process
            // the wrapped drawable.
            drawable = DrawableCompat.unwrap(drawable);

            final Method getOpticalInsetsMethod = drawable.getClass().getMethod("getOpticalInsets");
            final Object insets = getOpticalInsetsMethod.invoke(drawable);

            if (insets != null) {
                // If the drawable has some optical insets, let's copy them into a Rect
                final Rect result = new Rect();

                for (Field field : sInsetsClazz.getFields()) {
                    switch (field.getName()) {
                    case "left":
                        result.left = field.getInt(insets);
                        break;
                    case "top":
                        result.top = field.getInt(insets);
                        break;
                    case "right":
                        result.right = field.getInt(insets);
                        break;
                    case "bottom":
                        result.bottom = field.getInt(insets);
                        break;
                    }
                }
                return result;
            }
        } catch (Exception e) {
            // Eugh, we hit some kind of reflection issue...
            Log.e(TAG, "Couldn't obtain the optical insets. Ignoring.");
        }
    }

    // If we reach here, either we're running on a device pre-v18, the Drawable didn't have
    // any optical insets, or a reflection issue, so we'll just return an empty rect
    return INSETS_NONE;
}

From source file:org.apache.hadoop.ha.ShellCommandFencer.java

/**
 * Attempt to use evil reflection tricks to determine the
 * pid of a launched process. This is helpful to ops
 * if debugging a fencing process that might have gone
 * wrong. If running on a system or JVM where this doesn't
 * work, it will simply return null./*from   ww  w . j a v  a2  s  . co  m*/
 */
private static String tryGetPid(Process p) {
    try {
        Class<? extends Process> clazz = p.getClass();
        if (clazz.getName().equals("java.lang.UNIXProcess")) {
            Field f = clazz.getDeclaredField("pid");
            f.setAccessible(true);
            return String.valueOf(f.getInt(p));
        } else {
            LOG.trace("Unable to determine pid for " + p + " since it is not a UNIXProcess");
            return null;
        }
    } catch (Throwable t) {
        LOG.trace("Unable to determine pid for " + p, t);
        return null;
    }
}

From source file:Main.java

private static void init() {
    try {//from  w  ww  .  j  a  va  2  s.com
        // read all com.android.internal.R
        Class clazz = Class.forName("com.android.internal.R$layout");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // public static final
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())
                    && Modifier.isFinal(field.getModifiers())) {
                try {
                    int id = field.getInt(null);
                    sSystemLayoutResIds.put(id, field.getName());
                } catch (IllegalAccessException e) {
                }
            }
        }
    } catch (Exception e) {
    }
}

From source file:edu.umd.cs.marmoset.utilities.JProcess.java

/**
 * Uses reflection to extract the pid, a private field of the private class UNIXProcess.
 * This will fail on any non-Unix platform that doesn't use UNIXProcess.  It may
 * fail if the UNIXProcess class changes at all.  It may fail anyway for unpredictable
 * reasons.//from  w  w  w.  ja  v  a 2  s. c o  m
 * @param process The process
 * @return the pid of this process
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public static int getPid(Process process) throws NoSuchFieldException, IllegalAccessException {
    Class<? extends Process> processClass = process.getClass();
    Field pidField = processClass.getDeclaredField("pid");
    pidField.setAccessible(true);
    return pidField.getInt(process);
}

From source file:cn.zhangls.android.weibo.utils.TextUtil.java

/**
 * ????id ?getResId("icon", R.drawable.class);
 *
 * @param variableName variableName/*from w  w  w.  ja va 2  s  .c om*/
 * @param c            ??
 * @return ?Id
 */
private static int getResId(String variableName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(variableName);
        return idField.getInt(idField);
    } catch (Exception e) {
        System.out.println("===NoSuchFie===:" + variableName != null ? variableName : "null");
        return -1;
    }
}