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:ca.psiphon.tunneledwebview.WebViewProxySettings.java

@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressWarnings("rawtypes")
private static boolean setWebkitProxyKitKat(Context appContext, 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 {//www  .  j ava 2s  .  c  o  m
        Class applicationClass = Class.forName("android.app.Application");
        Field loadedApkField = applicationClass.getDeclaredField("mLoadedApk");
        loadedApkField.setAccessible(true);
        Object loadedApk = loadedApkField.get(appContext);
        Class loadedApkClass = Class.forName("android.app.LoadedApk");
        Field receiversField = loadedApkClass.getDeclaredField("mReceivers");
        receiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                Class receiverClass = receiver.getClass();
                if (receiverClass.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class,
                            Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                    final String CLASS_NAME = "android.net.ProxyProperties";
                    Class proxyPropertiesClass = Class.forName(CLASS_NAME);
                    Constructor constructor = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE,
                            String.class);
                    constructor.setAccessible(true);
                    Object proxyProperties = constructor.newInstance(host, port, null);
                    intent.putExtra("proxy", (Parcelable) proxyProperties);

                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
        return true;
    } catch (ClassNotFoundException e) {
    } catch (NoSuchFieldException e) {
    } catch (IllegalAccessException e) {
    } catch (IllegalArgumentException e) {
    } catch (NoSuchMethodException e) {
    } catch (InvocationTargetException e) {
    } catch (InstantiationException e) {
    }
    return false;
}

From source file:net.buffalo.protocal.util.ClassUtil.java

private static HashMap getFieldMap(Class cl) {
    HashMap fieldMap = new HashMap();
    for (; cl != null; cl = cl.getSuperclass()) {
        Field[] fields = cl.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers()))
                continue;
            field.setAccessible(true);
            fieldMap.put(field.getName(), field);
        }//ww  w .  ja  v  a2 s.  c  om
    }

    return fieldMap;
}

From source file:Main.java

private static Field getField(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Field field = null;
    while (objectClass != null && field == null) {
        try {/*from  ww  w.java 2  s  . c o m*/
            field = objectClass.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (field != null) {
        field.setAccessible(true);
    } else {
        throw new NoSuchFieldException(fieldName);
    }
    return field;
}

From source file:com.dosport.system.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredField, ?./*from w  ww  .j  a  v a2s.c om*/
 * 
 * ?Object?, null.
 */
public static Field getAccessibleField(final Object obj, final String fieldName) {
    Assert.notNull(obj, "object?");
    Assert.hasText(fieldName, "fieldName");
    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Field field = superClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {// NOSONAR
            // Field??,?
        }
    }
    return null;
}

From source file:com.ms.commons.lang.ObjectUtils.java

/**
 * string?trim?/*w w w .jav  a  2  s.  c  om*/
 * 
 * @param object
 * @throws Exception
 */
private static void trimStringField(Object object, Class<?> clazz) throws Exception {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.getType() == String.class) {
            boolean isFoolback = false;
            if (field.isAccessible() == false) {
                isFoolback = true;
                field.setAccessible(true);
            }
            String value = (String) field.get(object);
            if (StringUtils.isNotEmpty(value)) {
                value = value.trim();
                field.set(object, value);
            }
            if (isFoolback) {
                field.setAccessible(false);
            }
        }
    }
}

From source file:com.cloudbees.plugins.credentials.ContextMenuIconUtils.java

/**
 * Gets the qualified URL of the specified icon.
 *
 * @param icon the icon.//from w  w  w  .j  a  v a  2  s  .c o  m
 * @return the qualified URL of the icon.
 */
@CheckForNull
public static String getQualifiedUrl(@CheckForNull Icon icon) {
    if (icon == null) {
        return null;
    }
    try {
        Field iconType = Icon.class.getDeclaredField("iconType");
        iconType.setAccessible(true);
        IconType type = (IconType) iconType.get(icon);
        switch (type) {
        case CORE: {
            return Functions.getResourcePath() + "/images/" + icon.getUrl();
        }
        case PLUGIN: {
            return Functions.getResourcePath() + "/plugin/" + icon.getUrl();
        }
        }
        return null;
    } catch (NoSuchFieldException e) {
        // ignore we'll use a JellyContext
    } catch (IllegalAccessException e) {
        // ignore we'll use a JellyContext
    }
    JellyContext ctx = new JellyContext();
    ctx.setVariable("resURL", Functions.getResourcePath());
    return icon.getQualifiedUrl(ctx);
}

From source file:brooklyn.entity.software.SshEffectorTasksTest.java

public static Integer getMyPid() {
    try {/*from  ww w . j ava2  s . c  om*/
        java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        //            sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
        Object mgmt = jvm.get(runtime);
        java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);

        return (Integer) pid_method.invoke(mgmt);
    } catch (Exception e) {
        throw new PropagatedRuntimeException(
                "Test depends on (fragile) getMyPid method which does not work here", e);
    }
}

From source file:Main.java

public static void setDataEnabled(Context context, boolean enabled) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Class<?> conMgrClass = null;
    Field iConMgrField = null;
    Object iConMgr = null;/*from www  .  ja  va  2  s.com*/
    Class<?> iConMgrClass = null;
    Method setMobileDataEnabledMethod = null;
    try {
        conMgrClass = Class.forName(conMgr.getClass().getName());
        iConMgrField = conMgrClass.getDeclaredField("mService");
        iConMgrField.setAccessible(true);
        iConMgr = iConMgrField.get(conMgr);
        iConMgrClass = Class.forName(iConMgr.getClass().getName());
        setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConMgr, enabled);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.assertj.db.common.AbstractTest.java

/**
 * Returns an instance of a {@code Changes}.
 *
 * @param changesList The list of changes.
 * @return An instance.//  w  w w  . ja  v a2  s . c  o  m
 * @throws Exception Exception
 */
protected static Changes getChanges(List<Change> changesList) throws Exception {
    Constructor<Changes> constructor = Changes.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    Changes changes = constructor.newInstance();
    Field field = Changes.class.getDeclaredField("changesList");
    field.setAccessible(true);
    field.set(changes, changesList);
    return changes;
}

From source file:com.ikanow.aleph2.data_model.utils.ProcessUtils.java

/**
 * Returns the pid for the given process
 * /*from  w w w .jav a 2 s  .co  m*/
 * @param px
 * @return
 */
private static String getPid(Process px) {
    try {
        final Class<?> ProcessImpl = px.getClass();
        final Field field = ProcessImpl.getDeclaredField("pid");
        field.setAccessible(true);
        return Integer.toString(field.getInt(px));
    } catch (Throwable t) {
        return "unknown";
    }
}