Example usage for java.lang.reflect Field get

List of usage examples for java.lang.reflect Field get

Introduction

In this page you can find the example usage for java.lang.reflect Field get.

Prototype

@CallerSensitive
@ForceInline 
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

From source file:Main.java

private static int getSmartBarHeight(Activity activity) {
    ActionBar actionbar = activity.getActionBar();
    if (actionbar != null)
        try {//from  w w w  .j ava2s .c  om
            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:Main.java

/**
 * Converts a color into a string. If the color is equal to one of the defined
 * constant colors, that name is returned instead. Otherwise the color is
 * returned as hex-string./* w  w  w . ja v a 2 s. c o  m*/
 * 
 * @param c
 *          the color.
 * @return the string for this color.
 */
public static String colorToString(final Color c) {
    try {
        final Field[] fields = Color.class.getFields();
        for (int i = 0; i < fields.length; i++) {
            final Field f = fields[i];
            if (Modifier.isPublic(f.getModifiers()) && Modifier.isFinal(f.getModifiers())
                    && Modifier.isStatic(f.getModifiers())) {
                final String name = f.getName();
                final Object oColor = f.get(null);
                if (oColor instanceof Color) {
                    if (c.equals(oColor)) {
                        return name;
                    }
                }
            }
        }
    } catch (Exception e) {
        //
    }

    // no defined constant color, so this must be a user defined color
    final String color = Integer.toHexString(c.getRGB() & 0x00ffffff);
    final StringBuffer retval = new StringBuffer(7);
    retval.append("#");

    final int fillUp = 6 - color.length();
    for (int i = 0; i < fillUp; i++) {
        retval.append("0");
    }

    retval.append(color);
    return retval.toString();
}

From source file:com.github.piasy.biv.example.App.java

static void fixLeakCanary696(Context context) {
    if (!isEmui()) {
        Log.w(TAG, "not emui");
        return;/*w  w  w  .  j  av  a 2 s  . c o  m*/
    }
    try {
        Class clazz = Class.forName("android.gestureboost.GestureBoostManager");
        Log.w(TAG, "clazz " + clazz);

        Field _sGestureBoostManager = clazz.getDeclaredField("sGestureBoostManager");
        _sGestureBoostManager.setAccessible(true);
        Field _mContext = clazz.getDeclaredField("mContext");
        _mContext.setAccessible(true);

        Object sGestureBoostManager = _sGestureBoostManager.get(null);
        if (sGestureBoostManager != null) {
            _mContext.set(sGestureBoostManager, context);
        }
    } catch (Exception ignored) {
        ignored.printStackTrace();
    }
}

From source file:org.neo4j.ogm.auth.AuthenticationTest.java

private static void initialise(ServerControls controls) throws Exception {

    Field field = InProcessServerControls.class.getDeclaredField("server");
    field.setAccessible(true);//from   w  w w .jav  a  2s.  co  m
    neoServer = (AbstractNeoServer) field.get(controls);
}

From source file:com.prowidesoftware.deprecation.DeprecationUtils.java

/**
 * Helper hack to set environment variables from Java code
 *//*from w  w  w  . j a v a  2  s.co  m*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setEnv(final String key, final String value) {
    try {
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.put(key, value);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.put(key, value);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.clear();
                    map.put(key, value);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:io.tilt.minka.utils.Defaulter.java

private static PropertyEditor edit(final Properties props, final Object configurable, final Field staticField,
        final Field instanceField) throws IllegalAccessException {

    staticField.setAccessible(true);/*  w  w  w  .j  ava  2s .c o m*/
    final String name = instanceField.getName();
    final String staticValue = staticField.get(configurable).toString();
    final Object propertyOrDefault = props.getProperty(name, System.getProperty(name, staticValue));
    final String objName = configurable.getClass().getSimpleName();
    final PropertyEditor editor = PropertyEditorManager.findEditor(instanceField.getType());
    final String setLog = "Defaulter: set <{}> field [{}] = '{}' from {} ";
    try {
        editor.setAsText(propertyOrDefault.toString());
        logger.info(setLog, objName, name, editor.getValue(),
                propertyOrDefault != staticValue ? " property " : staticField.getName());
    } catch (Exception e) {
        logger.error(
                "Defaulter: object <{}> field: {} does not accept property or static "
                        + "default value: {} (reason: {})",
                objName, name, propertyOrDefault, e.getClass().getSimpleName());
        try { // at this moment only prop. might've been failed
            editor.setAsText(staticValue);
            logger.info(setLog, objName, name, editor.getValue(), staticField.getName());
        } catch (Exception e2) {
            final StringBuilder sb = new StringBuilder().append("Defaulter: object <").append(objName)
                    .append("> field: ").append(name).append(" does not accept static default value: ")
                    .append(propertyOrDefault).append(" (reason: ").append(e.getClass().getSimpleName())
                    .append(")");
            throw new RuntimeException(sb.toString());
        }

    }

    return editor;
}

From source file:com.dangdang.ddframe.job.lite.spring.util.AopTargetUtils.java

private static Object getProxyTargetObject(final Object proxy, final String proxyType) {
    Field h;
    try {/*from ww  w .ja v a 2  s.c o  m*/
        h = proxy.getClass().getSuperclass().getDeclaredField(proxyType);
    } catch (final NoSuchFieldException ex) {
        return getProxyTargetObjectForCglibAndSpring4(proxy);
    }
    h.setAccessible(true);
    try {
        return getTargetObject(h.get(proxy));
    } catch (final IllegalAccessException ex) {
        throw new JobSystemException(ex);
    }
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static Object getFieldObject(Field f, Object o) {
    Object fo = null;/*from   w w  w.  j  a v a 2  s  .co m*/
    try {
        fo = f.get(o);
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    }
    return fo;
}

From source file:com.dangdang.ddframe.job.spring.util.AopTargetUtils.java

private static Object getProxyTargetObject(final Object proxy, final String proxyType) {
    Field h;
    try {/* www. j a v  a2 s .  com*/
        h = proxy.getClass().getSuperclass().getDeclaredField(proxyType);
    } catch (final NoSuchFieldException ex) {
        return getProxyTargetObjectForCglibAndSpring4(proxy);
    }
    h.setAccessible(true);
    try {
        return getTargetObject(h.get(proxy));
    } catch (final IllegalAccessException ex) {
        throw new JobException(ex);
    }
}

From source file:de.Keyle.MyPet.util.BukkitUtil.java

public static String getPlayerLanguage(Player player) {
    if (!(player instanceof CraftPlayer)) {
        return "en_US";
    }//from   w w w  .j  a  va 2  s . c om
    EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
    try {
        Field field = entityPlayer.getClass().getDeclaredField("locale");
        field.setAccessible(true);

        return (String) field.get(entityPlayer);
    } catch (Exception e) {
        return "en_US";
    }
}