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

/**
 * Key to lowercase String, extracts the effective key that was pressed
 * (without shift, control, alt)//from   w  ww  . ja  v  a 2s  .co m
 */
public static String getKeyText(KeyEvent e) {
    // special cases
    if (e.getKeyCode() == KeyEvent.VK_DELETE) {
        return "delete";
    }
    // prio 1: get text of unresolved code (shift-1 --> '1')
    String s = "" + e.getKeyChar();
    if (e.getKeyCode() > 0) {
        int flags = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
        for (Field f : KeyEvent.class.getFields()) {
            if ((f.getModifiers() & flags) == flags) {
                try {
                    if (f.getName().startsWith("VK_") && ((Integer) f.get(null)) == e.getKeyCode()) {
                        s = f.getName().substring(3).toLowerCase();
                        break;
                    }
                } catch (Throwable t) {
                    // nop
                }
            }
        }
    }
    if (s.length() != 1) {
        // prio 2: check if the resolved char is valid (shift-1 --> '+')
        if (e.getKeyChar() >= 32 && e.getKeyChar() < 128) {
            s = "" + e.getKeyChar();
        }
    }
    return s.toLowerCase();
}

From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java

/**
 * Get system ProcessID of specified process. <p/>
 * Currently it method work only on unix systems.
 * @param process/*from  w  w  w .ja v  a2  s. c  om*/
 * @return PID or -1 on fail
 */
public static int getPid(Process process) {
    Class<? extends Process> clazz = process.getClass();
    try {
        Field field = clazz.getDeclaredField("pid");
        if (field.isAccessible()) {
            field.setAccessible(true);
        }
        return (int) field.get(process);
    } catch (IllegalAccessException | NoSuchFieldException e) {
        return -1;
    }
}

From source file:Main.java

public static void hide(View decorView) {
    if (!hasSmartBar())
        return;// w ww . java2 s . co  m

    try {
        @SuppressWarnings("rawtypes")
        Class[] arrayOfClass = new Class[1];
        arrayOfClass[0] = Integer.TYPE;
        Method localMethod = View.class.getMethod("setSystemUiVisibility", arrayOfClass);
        Field localField = View.class.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION");
        Object[] arrayOfObject = new Object[1];
        try {
            arrayOfObject[0] = localField.get(null);
        } catch (Exception e) {

        }
        localMethod.invoke(decorView, arrayOfObject);
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jongo.model.IdSpecSet.java

public static Object id(Object specInstance) {
    Field field = idField(specInstance.getClass());

    field.setAccessible(true);//from  w  ww.ja  v a  2 s.  c  om
    try {
        return field.get(specInstance);
    } catch (Exception e) {
        throw new RuntimeException("could not set id", e);
    }
}

From source file:com.vivekpanyam.evolve.Utils.java

/**
 * Set the ClassLoader for the running application
 *
 * @param a An activity in the currently running application
 * @param classLoader The ClassLoader used to load the new DEX file
 *///  ww  w  . jav a 2 s.  com

public static void setClassLoader(Activity a, ClassLoader classLoader) {
    try {
        Field mMainThread = getField(Activity.class, "mMainThread");
        Object mainThread = mMainThread.get(a);
        Class<?> threadClass = mainThread.getClass();
        Field mPackages = getField(threadClass, "mPackages");

        HashMap<String, ?> map = (HashMap<String, ?>) mPackages.get(mainThread);
        WeakReference<?> ref = (WeakReference<?>) map.get(a.getPackageName());
        Object apk = ref.get();
        Class<?> apkClass = apk.getClass();
        Field mClassLoader = getField(apkClass, "mClassLoader");

        mClassLoader.set(apk, classLoader);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

private static Object getProxyTargetObjectForCglibAndSpring4(final Object proxy) {
    Field h;
    try {/*from w w  w  . j av a  2  s  .co  m*/
        h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
        h.setAccessible(true);
        Object dynamicAdvisedInterceptor = h.get(proxy);
        Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        throw new JobSystemException(ex);
    }
}

From source file:Main.java

public static Object getField(Field field, Object source) {
    boolean accessible = field.isAccessible();
    field.setAccessible(true);/*from   w  ww.  j a  v  a 2s. c  o  m*/
    Object result = null;
    try {
        result = field.get(source);
    } catch (IllegalAccessException e) {
        // ignore
    }
    field.setAccessible(accessible);
    return result;
}

From source file:Main.java

private static Object getFieldValueSafely(Field field, Object classInstance)
        throws IllegalArgumentException, IllegalAccessException {
    boolean oldAccessibleValue = field.isAccessible();
    field.setAccessible(true);/* w w  w. j a  v a 2 s. co  m*/
    Object result = field.get(classInstance);
    field.setAccessible(oldAccessibleValue);
    return result;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;// w  ww . j a v a  2  s.  c  o m
    Object obj = null;
    Field field = null;
    int x = 0, sbar = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        sbar = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return sbar;
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("all")
private static boolean setProxyKK(WebView webView, String host, int port, String applicationClassName) {
    Context appContext = webView.getContext().getApplicationContext();
    if (null == host) {
        System.clearProperty("http.proxyHost");
        System.clearProperty("http.proxyPort");
        System.clearProperty("https.proxyHost");
        System.clearProperty("https.proxyPort");
    } else {/*from w  w w .  ja  v  a  2  s.com*/
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port + "");
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port + "");
    }
    try {
        Class applictionCls = Class.forName(applicationClassName);
        Field loadedApkField = applictionCls.getField("mLoadedApk");
        loadedApkField.setAccessible(true);
        Object loadedApk = loadedApkField.get(appContext);
        Class loadedApkCls = Class.forName("android.app.LoadedApk");
        Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
        receiversField.setAccessible(true);
        Map receivers = (Map) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object rec : ((Map) receiverMap).keySet()) {
                Class clazz = rec.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                    /*********** optional, may be need in future *************/
                    final String CLASS_NAME = "android.net.ProxyProperties";
                    Class cls = Class.forName(CLASS_NAME);
                    Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
                    constructor.setAccessible(true);
                    Object proxyProperties = constructor.newInstance(host, port, null);
                    intent.putExtra("proxy", (Parcelable) proxyProperties);
                    /*********** optional, may be need in future *************/

                    onReceiveMethod.invoke(rec, appContext, intent);
                }
            }
        }
        Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");
        return true;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    return false;
}