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:com.relicum.ipsum.Reflection.ReflectionUtil.java

/**
 * Gets a {@link Field} in a given {@link Class} object.
 *
 * @param clazz Class object/*from ww w .  ja  v a2s .  c  om*/
 * @param name  Field nameame
 * @return The field, or null if none exists.
 */
public static final Field getField(Class<?> clazz, String name) {
    Validate.notNull(clazz, "clazz cannot be null!");
    Validate.notNull(name, "name cannot be null!");

    try {
        Field field = clazz.getDeclaredField(name);
        if (field != null)
            return field;

        return clazz.getField(name);
    } catch (Throwable ex) {
    }
    return null;
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param owner/*from w  w  w. ja va2s  .c o m*/
 *            17
 * @param fieldName
 *            ??
 * @return 1717
 */
public static Object getProperty(Object owner, String fieldName)

{
    if (owner == null) {
        return "";
    }
    Class ownerClass = owner.getClass();
    Object property = null;
    try {
        Field field = ownerClass.getField(fieldName);

        property = field.get(owner);
    } catch (Exception ex)

    {
        log.error(ex);
        //throw ex;
    }

    return property;
}

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

private static int getSmartBarHeight(AppCompatActivity activity) {
    ActionBar actionbar = activity.getSupportActionBar();
    if (actionbar != null) {
        try {//from  w w w .  ja v  a 2  s  .co m
            Class c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("mz_action_button_min_height");
            int height = Integer.parseInt(field.get(obj).toString());
            return activity.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
            actionbar.getHeight();
        }
    }
    return 0;
}

From source file:net.sf.jasperreports.engine.query.JRHibernateQueryExecuter.java

private static final Type loadTypeConstant(Class<?> typeConstantsClass, String name) {
    try {/*from   ww  w. ja v a 2  s . com*/
        Field constant = typeConstantsClass.getField(name);
        if (!Modifier.isStatic(constant.getModifiers()) || !Type.class.isAssignableFrom(constant.getType())) {
            throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_UNRESOLVED_TYPE_CONSTANT,
                    new Object[] { name, typeConstantsClass.getName() });
        }
        Type type = (Type) constant.get(null);
        return type;
    } catch (NoSuchFieldException e) {
        throw new JRRuntimeException(e);
    } catch (SecurityException e) {
        throw new JRRuntimeException(e);
    } catch (IllegalArgumentException e) {
        throw new JRRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new JRRuntimeException(e);
    }
}

From source file:com.xxxifan.devbox.core.util.ViewUtils.java

/**
 * set status bar icon to light theme, which is called dark mode.
 * should be called in onCreate()// w ww .  j  a va2  s  .  c o  m
 */
public static void setStatusBarLightMode(Activity activity, boolean lightMode) {
    if (activity == null || activity.getWindow() == null) {
        return;
    }

    Window window = activity.getWindow();
    boolean changed = false;
    // try miui
    try {
        Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
        Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
        int darkIcon = field.getInt(layoutParams);
        Method extraFlagField = window.getClass().getMethod("setExtraFlags", int.class, int.class);
        extraFlagField.invoke(window, lightMode ? darkIcon : 0, darkIcon);
        changed = true;
    } catch (Exception ignored) {
    }

    // try flyme
    try {
        WindowManager.LayoutParams lp = window.getAttributes();
        Field darkIcon = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
        Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
        darkIcon.setAccessible(true);
        meizuFlags.setAccessible(true);
        int bit = darkIcon.getInt(null);
        int value = meizuFlags.getInt(lp);
        if (lightMode) {
            value |= bit;
        } else {
            value &= ~bit;
        }
        meizuFlags.setInt(lp, value);
        window.setAttributes(lp);
        changed = true;
    } catch (Exception ignored) {
    }

    if (!changed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int visibility = window.getDecorView().getSystemUiVisibility();
        if (lightMode) {
            visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        } else {
            visibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }
        window.getDecorView().setSystemUiVisibility(visibility);
    }
}

From source file:org.latticesoft.util.common.ClassUtil.java

/** Returns the integer value from the attribute name */
public static int getAttributeIntFromName(Class clazz, String name) {
    if (name == null)
        return 0;
    int retVal = 0;
    try {/*from   w  ww .  j a  va2s  .  co m*/
        retVal = clazz.getField(name.toUpperCase()).getInt(clazz);
    } catch (Exception e) {

    }
    return retVal;
}

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 ww  .  j a  v a  2  s.co m*/
 *        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:org.talend.core.utils.ReflectionUtils.java

/**
 * DOC ycbai Comment method "getStaticField".
 * //from www . j  a v a2  s  .c  om
 * Returns the value of a static field.
 * 
 * @param className
 * @param loader
 * @param fieldName
 * @return
 * @throws ClassNotFoundException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
public static Object getStaticField(String className, ClassLoader loader, String fieldName)
        throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException {
    Object fieldValue = null;
    Class ownerClass = null;
    if (loader != null) {
        ownerClass = Class.forName(className, true, loader);
    } else {
        ownerClass = Class.forName(className);
    }
    Field field = ownerClass.getField(fieldName);
    fieldValue = field.get(ownerClass);

    return fieldValue;
}

From source file:com.google.dart.tools.core.internal.builder.AnalysisMarkerManager.java

/**
 * @return the {@link ErrorCode} enumeration constant for string from
 *         {@link #encodeErrorCode(ErrorCode)}.
 *//*from  w  w w . ja  v  a 2s  .com*/
private static ErrorCode decodeErrorCode(String encoding) {
    try {
        String className = StringUtils.substringBeforeLast(encoding, ".");
        String fieldName = StringUtils.substringAfterLast(encoding, ".");
        Class<?> errorCodeClass = Class.forName(className);
        return (ErrorCode) errorCodeClass.getField(fieldName).get(null);
    } catch (Throwable e) {
        return null;
    }
}

From source file:Main.java

private static int getExifOrientation(String src) throws IOException {
    int orientation = 1;

    try {/*from   www . j  a  v  a2s  .  c  o m*/
        /**
         * if your are targeting only api level >= 5
         * ExifInterface exif = new ExifInterface(src);
         * orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
         */
        if (Build.VERSION.SDK_INT >= 5) {
            Class<?> exifClass = Class.forName("android.media.ExifInterface");
            Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class });
            Object exifInstance = exifConstructor.newInstance(new Object[] { src });
            Method getAttributeInt = exifClass.getMethod("getAttributeInt",
                    new Class[] { String.class, int.class });
            Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
            String tagOrientation = (String) tagOrientationField.get(null);
            orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1 });
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return orientation;
}