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:Main.java

public static <K, V> Map<K, V> collectionToMap(Collection<V> collection, String key) {
    Map<K, V> map = null;/*w  ww . j av  a2 s  . c o m*/
    Iterator<V> iterator = collection.iterator();
    try {
        if (key == null)
            throw new Exception("no key filed");
        else {
            map = new HashMap<K, V>();
            while (iterator.hasNext()) {
                V v = iterator.next();
                Class<? extends Object> c = v.getClass();
                Field field = c.getField(key);
                field.setAccessible(true);
                map.put((K) field.get(v), v);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}

From source file:Main.java

public static Bitmap getAppIcon(Context context) {
    try {/*from  w ww  .ja  va 2s.  co  m*/
        Class<?> drawableClass = Class.forName(context.getPackageName() + ".R$drawable");
        Field f = drawableClass.getField("ic_launcher");
        return BitmapFactory.decodeResource(context.getResources(), f.getInt(null));
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static int getConstantValue(Class<View> object, String constantName) {
    int value = -1;
    try {//from   w  w  w  . ja  v  a2s  . co  m
        Field field = object.getField(constantName);
        value = field.getInt(object);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

private static Field getPropertyField(Class<?> beanClazz, String propertyName) {
    Field property = null;//  w  w  w.j av  a 2 s  .c  o  m

    try {
        property = beanClazz.getField(propertyName);
    } catch (NoSuchFieldException ex) {
        Log.d(TAG, "No such public property '" + propertyName);
    }

    return property;
}

From source file:Main.java

/**
 * Retrieves the a field with the specified name from the given class. Other than Class's getField(String) method,
 * this method will also return protected and private fields.
 *
 * @param clazz     Class from which the field should be obtained.
 * @param fieldName Name of the field./*from  w  w  w . j a  va  2  s  .  c  o  m*/
 * @return field with the specified name.
 */
public static Field getField(final Class clazz, final String fieldName) {
    try {
        return clazz.getField(fieldName);
    } catch (NoSuchFieldException e) {
        /* Means there's no public field, let's keep looking */
    }

    Class runner = clazz;
    while (runner != null) {
        try {
            return runner.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            /* No luck here either */
        }
        runner = runner.getSuperclass();
    }

    return null;
}

From source file:Main.java

public static int getResourceId(String packageName, String resFileName, String parameterName) {
    if ((packageName != null) && (resFileName != null) && (parameterName != null))
        try {/*  w w w  . j  a  va  2s . c  o m*/
            Class localClass = Class.forName(packageName + "$" + resFileName);
            Field localField = localClass.getField(parameterName);
            Object localObject = localField.get(localClass.newInstance());
            return Integer.parseInt(localObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    return -1;
}

From source file:com.mawujun.util.ObjectConvert.java

public static boolean isWrapClass(Class clz) {
    try {/*from   w ww  .j a va2 s . c o  m*/
        return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Called internally by installGtkPopupBugWorkaround. Returns a specific
 * GTK style object./*from ww w .j a va2s. c  o  m*/
 * 
 * @param styleFactory
 *            The GTK style factory.
 * @param component
 *            The target component of the style.
 * @param regionName
 *            The name of the target region of the style.
 * @return The GTK style.
 * @throws Exception
 *             When reflection fails.
 */
private static Object getGtkStyle(Object styleFactory, JComponent component, String regionName)
        throws Exception {
    // Create the region object
    Class<?> regionClass = Class.forName("javax.swing.plaf.synth.Region");
    Field field = regionClass.getField(regionName);
    Object region = field.get(regionClass);

    // Get and return the style
    Class<?> styleFactoryClass = styleFactory.getClass();
    Method method = styleFactoryClass.getMethod("getStyle", new Class<?>[] { JComponent.class, regionClass });
    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object style = method.invoke(styleFactory, component, region);
    method.setAccessible(accessible);
    return style;
}

From source file:Main.java

private static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
    Class<? extends Window> clazz = activity.getWindow().getClass();
    try {/*  ww w. j  av a 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 int getStatusBarHeight(Context context) {
    int statusBarHeight = 0;
    try {/*  w  ww  .  j av a 2  s. c o  m*/
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
    }
    return statusBarHeight;
}