Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:jp.terasoluna.fw.beans.jxpath.JXPATH152PatchActivator.java

/**
 * ??//  ww  w  . ja  v a2 s . co  m
 */
private static void activate() {
    try {
        // ?????Map?
        Field byClassField = JXPathIntrospector.class.getDeclaredField("byClass");
        byClassField.setAccessible(true);
        Map<?, ?> byClass = (Map<?, ?>) byClassField.get(null);

        Field byInterfaceField = JXPathIntrospector.class.getDeclaredField("byInterface");
        byInterfaceField.setAccessible(true);
        Map<?, ?> byInterface = (Map<?, ?>) byInterfaceField.get(null);

        // Map??
        byClassField.set(null, new HashMapForJXPathIntrospector<Object, Object>(byClass));
        byInterfaceField.set(null, new HashMapForJXPathIntrospector<Object, Object>(byInterface));

        log.info("JXPATH-152 Patch activation succeeded.");
    } catch (Exception e) {
        // ?????
        // ???????commons-JXPath?????
        log.fatal("JXPATH-152 Patch activation failed.", e);
    }
}

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  . j  a  v a 2s.  c o  m
 * @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:com.github.piasy.biv.example.App.java

static void fixLeakCanary696(Context context) {
    if (!isEmui()) {
        Log.w(TAG, "not emui");
        return;//from  w ww .ja v  a  2s  .  com
    }
    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:com.dangdang.ddframe.job.lite.spring.util.AopTargetUtils.java

private static Object getProxyTargetObjectForCglibAndSpring4(final Object proxy) {
    Field h;
    try {/* w ww.j a v a 2 s. c om*/
        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:com.dangdang.ddframe.job.spring.util.AopTargetUtils.java

private static Object getProxyTargetObjectForCglibAndSpring4(final Object proxy) {
    Field h;
    try {/*  ww  w. j  a v  a  2  s.com*/
        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 JobException(ex);
    }
}

From source file:Main.java

/**
 * Returns the value in the current thread's copy of this
 * thread-local variable.  If the variable has no value for the
 * current thread, it is first initialized to the value returned
 * by an invocation of the {@link #initialValue} method.
 *
 * @return the current thread's value of this thread-local
 * @throws NoSuchMethodException /*  w  w  w  . java  2s  . com*/
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 * @throws NoSuchFieldException 
 */
@SuppressWarnings("unchecked")
public static <T extends Object> T get(Thread thread, ThreadLocal<T> threadLocal)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException {
    Object map = getMap(thread); // ThreadLocalMap
    if (map != null) {
        // calling getEntry returns a ThreadLocal.Entry instance, which is a
        // mapping from a ThreadLocal to an Entry, and an Entry is an 
        // extension of WeakReference.
        // ThreadLocalMap.Entry e = map.getEntry(this);
        Method getEntryMethod = map.getClass().getDeclaredMethod("getEntry", new Class[] { ThreadLocal.class });
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(map, new Object[] { threadLocal }); // ThreadLocalMap.Entry

        if (entry != null) {
            Field valueField = entry.getClass().getDeclaredField("value");
            valueField.setAccessible(true);
            return (T) valueField.get(entry);
        }
    }
    return setInitialValue(thread, threadLocal);
}

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  ww .  java  2s .c o m
        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;
}

From source file:api_proto3.TestElf.java

public static HikariPool getPool(HikariDataSource ds) {
    try {/*w  ww .j  ava2  s  .c  o  m*/
        Field field = ds.getClass().getDeclaredField("pool");
        field.setAccessible(true);
        return (HikariPool) field.get(ds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.npower.dm.util.BeanHelper.java

/**
 * private/protected/*  ww  w.j av  a2 s  .  c om*/
 */
static public Object getPrivateProperty(Object object, String propertyName)
        throws IllegalAccessException, NoSuchFieldException {
    assert object != null;
    assert StringUtils.isNotEmpty(propertyName);
    Field field = object.getClass().getDeclaredField(propertyName);
    field.setAccessible(true);
    return field.get(object);
}

From source file:com.npower.dm.util.BeanHelper.java

/**
 * private/protected/*w ww  .j  av  a  2  s.com*/
 */
static public void setPrivateProperty(Object object, String propertyName, Object newValue)
        throws IllegalAccessException, NoSuchFieldException {
    assert object != null;
    assert StringUtils.isNotEmpty(propertyName);

    Field field = object.getClass().getDeclaredField(propertyName);
    field.setAccessible(true);
    field.set(object, newValue);
}