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:com.datatorrent.stram.util.WebServicesClientTest.java

private static CredentialsProvider getCredentialsProvider()
        throws NoSuchFieldException, IllegalAccessException {
    Field field = WebServicesClient.class.getDeclaredField(CREDENTIALS_PROVIDER_FIELD);
    field.setAccessible(true);
    CredentialsProvider credentials = (CredentialsProvider) field.get(null);
    field.setAccessible(false);// w w w . j ava2  s . c om
    return credentials;
}

From source file:Main.java

public static void finishAllActivies(Activity activity) {
    try {/*from   w w  w .  j av a2 s . c om*/
        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_mActivities = clazz_ActivityThread.getDeclaredField("mActivities");
        field_mActivities.setAccessible(true);
        Object mActivities = field_mActivities.get(mMainThread);

        HashMap<?, ?> map = (HashMap<?, ?>) mActivities;
        Collection<?> collections = map.values();
        if (null != collections && !collections.isEmpty()) {
            Class<?> clazz_ActivityClientRecord = Class
                    .forName("android.app.ActivityThread$ActivityClientRecord");
            Field field_activitiy = clazz_ActivityClientRecord.getDeclaredField("activity");
            field_activitiy.setAccessible(true);

            Activity acti;
            for (Object obj : collections) {
                acti = (Activity) field_activitiy.get(obj);
                Log.d(TAG, "activity.name=" + acti.getClass().getName());
                if (null != acti && !acti.isFinishing()) {
                    Log.d(TAG, "activity.name=" + acti.getClass().getName() + " not finish.");
                    acti.finish();
                }
            }
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String readRegistryValue(boolean user, String path, String key) throws Exception {
    final Class clz = Class.forName("java.util.prefs.WindowsPreferences");
    final Field f = clz.getDeclaredField(user ? "userRoot" : "systemRoot");

    f.setAccessible(true);

    final Object root = f.get(null);
    final Method openKey = clz.getDeclaredMethod("openKey", byte[].class, int.class, int.class);

    openKey.setAccessible(true);/*from   ww  w  .j  a  va  2s .c  om*/

    final Method closeKey = clz.getDeclaredMethod("closeKey", int.class);

    closeKey.setAccessible(true);

    final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);

    winRegQueryValue.setAccessible(true);

    byte[] valb = null;
    String vals = null;
    Integer handle = -1;

    handle = (Integer) openKey.invoke(root, toCSTR(path), 0x20019, 0x20019);
    valb = (byte[]) winRegQueryValue.invoke(root, handle, toCSTR(key));
    vals = ((valb != null) ? new String(valb).trim() : null);
    closeKey.invoke(root, handle);

    return vals;
}

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

private static Object getTargetObject(final Object object) {
    try {//  w w w .  ja  v  a  2  s. co  m
        Field advised = object.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(object)).getTargetSource().getTarget();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        throw new JobSystemException(ex);
    }
}

From source file:com.akamai.edgegrid.signer.restassured.RestAssuredEdgeGridRequestSigner.java

private static String getRequestPath(FilterableRequestSpecification requestSpec) {
    try {/*from   w  w w  .j  av  a 2 s . c o  m*/
        Field f = requestSpec.getClass().getDeclaredField("path");
        f.setAccessible(true);
        return (String) f.get(requestSpec);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(e); // should never occur
    }
}

From source file:Main.java

/**
 * Helper to get the Activity result code.    This must only be called on the main thread.
 *
 * @param activity Activity whose result code is to be obtained.
 * @return Result code of the Activity.//  w  w w .  jav  a2s .  co  m
 */
public static int getActivityResultCode(@NonNull final Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null"); //$NON-NLS-1$
    }

    if (Looper.getMainLooper() != Looper.myLooper()) {
        throw new AssertionError("Must only be called on the main thread");
    }

    /*
     * This is a hack to obtain the Activity result code. There is no official way to check this using the Android
     * testing frameworks, so accessing the internals of the Activity object is the only way. This could
     * break on newer versions of Android.
     */

    try {
        final Field resultCodeField = Activity.class.getDeclaredField("mResultCode"); //$NON-NLS-1$
        resultCodeField.setAccessible(true);
        return ((Integer) resultCodeField.get(activity)).intValue();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

protected static void makeAccessible(Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);
    }/*from  w w  w .  ja  va  2s.  com*/
}

From source file:ReflectionUtils.java

public static Object getPropertyValue(Object bean, String propertyPath) throws NoSuchFieldException {
    if (bean == null)
        throw new IllegalArgumentException("bean cannot be null");
    Field field = ReflectionUtils.getField(bean.getClass(), propertyPath);
    field.setAccessible(true);
    try {/*from  w  w  w .j  a v  a2 s . c o  m*/
        return (field.get(bean));
    } catch (IllegalAccessException e) {
        return (null);
    }
}

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

private static Object getTargetObject(final Object object) {
    try {//from   w w w. ja  va2  s. c o m
        Field advised = object.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(object)).getTargetSource().getTarget();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        throw new JobException(ex);
    }
}

From source file:com.netflix.spinnaker.kork.jedis.JedisHealthIndicatorFactory.java

public static HealthIndicator build(JedisClientDelegate client) {
    try {//from  w w  w  .ja v a2s  .  c  om
        final JedisClientDelegate src = client;
        final Field clientAccess = JedisClientDelegate.class.getDeclaredField("jedisPool");
        clientAccess.setAccessible(true);

        return build((Pool<Jedis>) clientAccess.get(src));
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}