Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:com.sun.faces.application.ResetUniqueRequestIdBean.java

public static void setPrivateField(String fieldName, Class containingClass, Object target, Object value) {
    try {/*  ww  w .  j a v  a 2s.  c  o  m*/
        Field field = containingClass.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(target, value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

@Deprecated
public static void quitAppliation(Activity activity) {
    try {/*from  w w w  .  j ava 2  s . co  m*/
        Class<?> clazz_Activity = Class.forName("android.app.Activity");
        Field field_mMainThread = clazz_Activity.getDeclaredField("mMainThread");
        field_mMainThread.setAccessible(true);
        Object mMainThread = field_mMainThread.get(activity);

        Class<?> clazz_ActivityThread = Class.forName("android.app.ActivityThread");
        Field field_mAppThread = clazz_ActivityThread.getDeclaredField("mAppThread");
        field_mAppThread.setAccessible(true);
        Object mAppThread = field_mAppThread.get(mMainThread);

        Class<?> clazz_ActivityClientRecord = Class.forName("android.app.ActivityThread$ApplicationThread");
        Method method_scheduleExit = clazz_ActivityClientRecord.getDeclaredMethod("scheduleExit");
        method_scheduleExit.invoke(mAppThread);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Field getDeclaredField(Object object, String fieldName) {
    Class<?> clz = object.getClass();
    for (; clz != Object.class; clz = clz.getSuperclass()) {
        try {/*  w w w  .ja va2  s. com*/
            Field field = clz.getDeclaredField(fieldName);
            return field;
        } catch (Exception e) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the specified field of the given class, or null if it does not exist.
 *//*from  w  w  w .j  av a 2s  .  co m*/
public static Field getField(final Class<?> c, final String fieldName) {
    if (c == null)
        return null;
    try {
        return c.getDeclaredField(fieldName);
    } catch (final NoSuchFieldException e) {
        return null;
    }
}

From source file:Main.java

public static void setField(Object obj, Class<?> cl, String field, Object value)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field localField = cl.getDeclaredField(field);
    localField.setAccessible(true);//  w w w.j ava  2 s  .  c om
    localField.set(obj, value);
}

From source file:Main.java

private static Object getField(Object paramObject, Class<?> paramClass, String paramString)
        throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
    Field localField = paramClass.getDeclaredField(paramString);
    localField.setAccessible(true);/* www  .j av  a2s  . c  om*/
    return localField.get(paramObject);
}

From source file:Main.java

@SuppressWarnings("all")
private static boolean setProxyICS(WebView webview, String host, int port) {
    try {//  ww  w.  j a  va 2 s.  com
        Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
        Class params[] = new Class[1];
        params[0] = Class.forName("android.net.ProxyProperties");
        Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

        Class wv = Class.forName("android.webkit.WebView");
        Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
        Object mWebViewCoreFieldInstance = getFieldValueSafely(mWebViewCoreField, webview);

        Class wvc = Class.forName("android.webkit.WebViewCore");
        Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
        Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldInstance);

        Class bf = Class.forName("android.webkit.BrowserFrame");
        Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
        Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

        Class ppclass = Class.forName("android.net.ProxyProperties");
        Class pparams[] = new Class[3];
        pparams[0] = String.class;
        pparams[1] = int.class;
        pparams[2] = String.class;
        Constructor ppcont = ppclass.getConstructor(pparams);
        updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));
        Log.d(LOG_TAG, "Setting proxy with 4.0 API successful!");
        return true;
    } catch (Exception ex) {
        Log.e(LOG_TAG, "failed to set HTTP proxy: " + ex);
        return false;
    }
}

From source file:Main.java

private static Field findFieldRecursiveImpl(Class<?> clazz, String fieldName) throws NoSuchFieldException {
    try {/* w  w w  .j  av a 2 s . c o m*/
        return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        while (true) {
            clazz = clazz.getSuperclass();
            if (clazz == null || clazz.equals(Object.class))
                break;

            try {
                return clazz.getDeclaredField(fieldName);
            } catch (NoSuchFieldException ignored) {
            }
        }
        throw e;
    }
}

From source file:Main.java

public static void setStaticFieldValue(@SuppressWarnings("rawtypes") final Class clazz, final String name,
        final Object value) throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);/*from w ww. j ava  2s .com*/
    // There seems to be no field "modifiers" in Android/DalvikVM?
    // Field modifiersField = Field.class.getDeclaredField("modifiers");
    // modifiersField.setAccessible(true);
    // modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, value);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean setProxyLollipop(final Context context, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {//from  www  .j  a v  a2s. c o m
        Context appContext = context.getApplicationContext();
        Class applictionClass = Class.forName("android.app.Application");
        Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
        mLoadedApkField.setAccessible(true);
        Object mloadedApk = mLoadedApkField.get(appContext);
        Class loadedApkClass = Class.forName("android.app.LoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
        mReceiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = receiver.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}