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:com.xlythe.engine.theme.Theme.java

/**
 * Gets android theme from theme apk. Can be 0 (no theme).
 *///w  w  w.j  a v a2 s .  c om
public static int getTheme(Context context) {
    int id = getId(context, "string", "app_theme");
    if (id == 0) {
        return 0;
    }

    String fieldName = getResources(context).getString(id).replace(".", "_");
    try {
        Field field = android.R.style.class.getField(fieldName);
        return field.getInt(null);
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return 0;
}

From source file:com.xlythe.engine.theme.Theme.java

/**
 * Gets android theme from theme apk. Can be 0 (no theme). This is for apps that want an
 * actionbar in their Settings but not in their main app.
 *//*w  ww  . j  a v  a 2 s  .  c  o  m*/
public static int getSettingsTheme(Context context) {
    int id = getId(context, "string", "app_settings_theme");
    if (id == 0) {
        return 0;
    }

    String fieldName = getResources(context).getString(id).replace(".", "_");
    try {
        Field field = android.R.style.class.getField(fieldName);
        return field.getInt(null);
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return 0;
}

From source file:com.waz.zclient.utils.ViewUtils.java

public static long getNextAnimationDuration(Fragment fragment) {
    try {/*  www.  j  a v a  2  s. com*/
        // Attempt to get the resource ID of the next animation that
        // will be applied to the given fragment.
        Field nextAnimField = Fragment.class.getDeclaredField("mNextAnim");
        nextAnimField.setAccessible(true);
        int nextAnimResource = nextAnimField.getInt(fragment);
        Animation nextAnim = AnimationUtils.loadAnimation(fragment.getActivity(), nextAnimResource);
        // ...and if it can be loaded, return that animation's duration
        return (nextAnim == null) ? DEFAULT_CHILD_ANIMATION_DURATION : nextAnim.getDuration();
    } catch (NoSuchFieldException | IllegalAccessException | Resources.NotFoundException ex) {
        return DEFAULT_CHILD_ANIMATION_DURATION;
    }
}

From source file:com.microsoft.tfs.client.common.ui.framework.helper.ColorUtils.java

/**
 * Gets the windows system color id identified by name, which will be
 * resolved to the SWT Win32 specific color names (mostly a mirror of the
 * constants used by GetSysColor, but not necessarily. See
 * org.eclipse.swt.internal.win32.OS for color names.)
 *
 * @param colorName/*from   w  w  w . j av a2  s .c  om*/
 *        The name of the color to resolve.
 * @throws IllegalArgumentException
 *         if the current platform is not win32
 * @return The color id identified by this name, or -1 if it could not be
 *         looked up.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static int getWin32SystemColorID(final String colorName) {
    Check.notNull(colorName, "colorName"); //$NON-NLS-1$
    Check.isTrue(WindowSystem.isCurrentWindowSystem(WindowSystem.WIN32), "WindowSystem.WIN32"); //$NON-NLS-1$

    try {
        final Class osClass = Class.forName("org.eclipse.swt.internal.win32.OS"); //$NON-NLS-1$

        if (osClass == null) {
            log.warn("Could not load win32 constants class"); //$NON-NLS-1$
        } else {
            final Field swtColorIdField = osClass.getField(colorName);

            if (swtColorIdField == null) {
                log.warn(MessageFormat.format("Could not load swt win32 color constant {0}", colorName)); //$NON-NLS-1$
            } else {
                /*
                 * Get the SWT constant id for this color (this is not the
                 * windows color id)
                 */
                final Integer swtColorIdValue = swtColorIdField.getInt(osClass);

                if (swtColorIdValue == null) {
                    log.warn(MessageFormat.format("Could not load swt win32 color constant {0}", colorName)); //$NON-NLS-1$
                } else {
                    /* Now look up the windows color ID */
                    final Method sysColorMethod = osClass.getMethod("GetSysColor", new Class[] { //$NON-NLS-1$
                            int.class });

                    if (sysColorMethod == null) {
                        log.warn("Could not load win32 GetSysColor method"); //$NON-NLS-1$
                    } else {
                        final Object winColorId = sysColorMethod.invoke(osClass,
                                new Object[] { swtColorIdValue.intValue() });

                        if (winColorId == null) {
                            log.warn(MessageFormat.format("Could not query win32 color constant {0}", //$NON-NLS-1$
                                    colorName));
                        } else if (!(winColorId instanceof Integer)) {
                            log.warn(MessageFormat.format("Received non-integer win32 color constant for {0}", //$NON-NLS-1$
                                    colorName));
                        } else {
                            return ((Integer) winColorId).intValue();
                        }
                    }
                }
            }
        }
    } catch (final Throwable t) {
        log.warn("Could not load win32 constants", t); //$NON-NLS-1$
    }

    return -1;
}

From source file:com.cardvlaue.sys.util.ScreenUtil.java

/**
 * ???Flyme4.0 ???Flyme//from   ww  w  . j  a v a 2  s  .c  o m
 *
 * @param window ??
 * @param dark ????
 * @return boolean ?true
 */
public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
    boolean result = false;
    if (window != null) {
        try {
            WindowManager.LayoutParams lp = window.getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class
                    .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (dark) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            window.setAttributes(lp);
            result = true;
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }
    return result;
}

From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java

public static int getPid(Process process) {
    try {//from w w  w  .  j  a v a 2  s  .co m
        Class<?> cProcessImpl = process.getClass();
        java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid");
        if (!fPid.isAccessible()) {
            fPid.setAccessible(true);
        }
        return fPid.getInt(process);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        return -1;
    }
}

From source file:eu.vital.vitalcep.restApp.alert.Alerts.java

public static int getPid(Process process) {
    try {/*from w w w  .j  a v  a2  s .  com*/
        Class<?> cProcessImpl = process.getClass();
        java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid");
        if (!fPid.isAccessible()) {
            fPid.setAccessible(true);
        }
        return fPid.getInt(process);
    } catch (Exception e) {
        return -1;
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

private static void readDataBaseTypes(Document xmlConfiguration) {
    dataBaseTypes = new HashMap<String, Integer>();

    Element doc = xmlConfiguration.getDocumentElement();
    NodeList nodesDataBaseTypes = doc.getElementsByTagName("dataBaseType");

    // iterate over the data base types
    for (int i = 0; i < nodesDataBaseTypes.getLength(); i++) {
        // get the data base type node
        Node dataBaseType = nodesDataBaseTypes.item(i);

        if (dataBaseType instanceof Element) {
            Element dataBaseTypeElem = (Element) dataBaseType;

            String dataBaseTypeLabel = getStringNodeValue("label", dataBaseTypeElem);
            String sqlTypeStr = getStringNodeValue("sqlType", dataBaseTypeElem);
            Integer sqlType = null;
            if (!StringUtils.isEmpty(sqlTypeStr)) {
                Field typeField;
                try {
                    typeField = Types.class.getDeclaredField(sqlTypeStr);
                    if (typeField != null) {
                        sqlType = typeField.getInt(null);
                    }/*from w w w.java2  s  . c  o m*/
                } catch (SecurityException e) {
                    log.error(
                            "Error al intentar obtener los tipos de base de datos del fichero de configuracin: "
                                    + e.getLocalizedMessage());
                } catch (NoSuchFieldException e) {
                    log.error(
                            "Error al intentar obtener los tipos de base de datos del fichero de configuracin: "
                                    + e.getLocalizedMessage());
                } catch (IllegalArgumentException e) {
                    log.error(
                            "Error al intentar obtener los tipos de base de datos del fichero de configuracin: "
                                    + e.getLocalizedMessage());
                } catch (IllegalAccessException e) {
                    log.error(
                            "Error al intentar obtener los tipos de base de datos del fichero de configuracin: "
                                    + e.getLocalizedMessage());
                }
            }
            dataBaseTypes.put(dataBaseTypeLabel, sqlType);
        }
    }
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
  * Using a little reflection here for a lack of any better way 
  * to access locale-specific char codes for menu mnemonics.
  * We could at least defer this in the future.
  *//from  w w  w  .j a v a2s. co m
  * @param str the key for the locale-specific char resource to
  *  look up -- the key as it appears in the locale-specific
  *  properties file
  * @return the code for the passed-in key as defined in 
  *  <tt>java.awt.event.KeyEvent</tt>, or -1 if no key code
  *  could be found
  */
public static int getCodeForCharKey(String str) {
    int charCode = -1;
    String charStr = str.toUpperCase(Locale.US);
    if (charStr.length() > 1)
        return -1;
    try {
        Field charField = KeyEvent.class.getField("VK_" + charStr);
        charCode = charField.getInt(KeyEvent.class);
    } catch (NoSuchFieldException e) {
        LOG.error("can't get key for: " + charStr, e);
    } catch (SecurityException e) {
        LOG.error("can't get key for: " + charStr, e);
    } catch (IllegalAccessException e) {
        LOG.error("can't get key for: " + charStr, e);
    }
    return charCode;
}

From source file:me.anon.lib.Views.java

private static void inject(final Object target, Object source, Finder finder) {
    if (target.getClass().getDeclaredFields() != null) {
        ArrayList<Method> methods = new ArrayList<Method>();
        ArrayList<Field> fields = new ArrayList<Field>();
        Class objOrSuper = target.getClass();

        if (!objOrSuper.isAnnotationPresent(Injectable.class)) {
            Log.e("InjectView", "No Injectable annotation for class " + objOrSuper);
            return;
        }/* w  w  w .  j  ava  2 s. c o  m*/

        while (objOrSuper.isAnnotationPresent(Injectable.class)) {
            for (Field field : objOrSuper.getDeclaredFields()) {
                if (field.isAnnotationPresent(InjectView.class) || field.isAnnotationPresent(InjectViews.class)
                        || field.isAnnotationPresent(InjectFragment.class)
                        || field.isAnnotationPresent(OnClick.class)) {
                    fields.add(field);
                }
            }

            for (Method method : objOrSuper.getDeclaredMethods()) {
                if (method.isAnnotationPresent(OnClick.class)) {
                    methods.add(method);
                }
            }

            objOrSuper = objOrSuper.getSuperclass();
        }

        for (Field field : fields) {
            if (field.isAnnotationPresent(InjectView.class)) {
                InjectView a = (InjectView) field.getAnnotation(InjectView.class);

                try {
                    field.setAccessible(true);

                    int id = ((InjectView) a).value();
                    if (id < 1) {
                        String key = ((InjectView) a).id();
                        if (TextUtils.isEmpty(key)) {
                            key = field.getName();
                            key = key.replaceAll("(.)([A-Z])", "$1_$2").toLowerCase(Locale.ENGLISH);
                        }

                        Field idField = R.id.class.getField(key);
                        id = idField.getInt(null);
                    }

                    View v = finder.findById(source, id);

                    if (v != null) {
                        field.set(target, v);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (field.isAnnotationPresent(InjectViews.class)) {
                try {
                    InjectViews annotation = (InjectViews) field.getAnnotation(InjectViews.class);
                    field.setAccessible(true);

                    int[] ids = annotation.value();
                    String[] strIds = annotation.id();
                    Class[] instances = annotation.instances();

                    List<View> views = new ArrayList<View>(ids.length);

                    if (ids.length > 0) {
                        for (int index = 0; index < ids.length; index++) {
                            View v = finder.findById(source, ids[index]);
                            views.add(index, v);
                        }
                    } else if (strIds.length > 0) {
                        for (int index = 0; index < ids.length; index++) {
                            String key = annotation.id()[index];
                            Field idField = R.id.class.getField(key);
                            int id = idField.getInt(null);

                            View v = finder.findById(source, id);
                            views.add(index, v);
                        }
                    } else if (instances.length > 0) {
                        for (int index = 0; index < instances.length; index++) {
                            List<View> v = finder.findByInstance(source, instances[index]);
                            views.addAll(v);
                        }
                    }

                    field.set(target, views);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (field.isAnnotationPresent(InjectFragment.class)) {
                InjectFragment annotation = (InjectFragment) field.getAnnotation(InjectFragment.class);

                try {
                    field.setAccessible(true);

                    int id = ((InjectFragment) annotation).value();
                    Object fragment = null;

                    if (id < 1) {
                        String tag = ((InjectFragment) annotation).tag();

                        fragment = finder.findFragmentByTag(source, tag);
                    } else {
                        fragment = finder.findFragmentById(source, id);
                    }

                    if (fragment != null) {
                        field.set(target, fragment);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (field.isAnnotationPresent(OnClick.class)) {
                OnClick annotation = (OnClick) field.getAnnotation(OnClick.class);

                try {
                    if (field.get(target) != null) {
                        final View view = ((View) field.get(target));

                        if (!TextUtils.isEmpty(annotation.method())) {
                            final String clickName = annotation.method();
                            view.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    try {
                                        Class<?> c = Class.forName(target.getClass().getCanonicalName());
                                        Method m = c.getMethod(clickName, View.class);
                                        m.invoke(target, v);
                                    } catch (Exception e) {
                                        throw new IllegalArgumentException("Method not found " + clickName);
                                    }
                                }
                            });
                        } else {
                            view.setOnClickListener((View.OnClickListener) target);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        for (final Method method : methods) {
            if (method.isAnnotationPresent(OnClick.class)) {
                final OnClick annotation = (OnClick) method.getAnnotation(OnClick.class);
                final String clickName = method.getName();

                try {
                    int id = annotation.value();
                    if (id < 1) {
                        String key = annotation.id();

                        if (TextUtils.isEmpty(key)) {
                            key = clickName;
                            key = key.replaceAll("^(on)?(.*)Click$", "$2");
                            key = key.replaceAll("(.)([A-Z])", "$1_$2").toLowerCase(Locale.ENGLISH);
                        }

                        Field field = R.id.class.getField(key);
                        id = field.getInt(null);
                    }

                    View view = finder.findById(source, id);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                if (method != null && method.getParameterTypes().length > 0) {
                                    Class<?> paramType = method.getParameterTypes()[0];
                                    method.setAccessible(true);
                                    method.invoke(target, paramType.cast(v));
                                } else if (method != null && method.getParameterTypes().length < 1) {
                                    method.setAccessible(true);
                                    method.invoke(target);
                                } else {
                                    new IllegalArgumentException(
                                            "Failed to find method " + clickName + " with nil or View params")
                                                    .printStackTrace();
                                }
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}