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

private static Object getDeclaredField(Object obj, String name)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field f = obj.getClass().getDeclaredField(name);
    f.setAccessible(true);
    Object out = f.get(obj);/* w  ww.j a  v  a2s.  c om*/
    return out;
}

From source file:com.vmware.qe.framework.datadriven.impl.injector.DataInjectorUsingReflection.java

private static Field getField(Class<?> clazz, String fieldName) {
    Class<?> tmpClass = clazz;
    do {//www . j  ava 2  s  . co m
        for (Field field : tmpClass.getDeclaredFields()) {
            String candidateName = field.getName();
            if (!candidateName.equals(fieldName)) {
                continue;
            }
            field.setAccessible(true);
            return field;
        }
        tmpClass = tmpClass.getSuperclass();
    } while (clazz != null);
    throw new RuntimeException("Field '" + fieldName + "' not found on class " + clazz);
}

From source file:io.cloudslang.content.database.services.databases.MSSqlDatabase.java

private static void setJavaLibraryPath(String sqlJdbcAuthFilePath) {
    String javaLibraryPath = System.getProperty(JAVA_LIBRARY_PATH);

    if (StringUtils.isEmpty(javaLibraryPath)) {
        javaLibraryPath = sqlJdbcAuthFilePath;
    } else {//from   w  ww.  j a  va2 s. co m
        javaLibraryPath = javaLibraryPath.substring(0, javaLibraryPath.length() - 1) + sqlJdbcAuthFilePath
                + System.getProperty(PATH_SEPARATOR) + CURRENT_DIRECTORY_NOTATION;
    }
    System.setProperty(JAVA_LIBRARY_PATH, javaLibraryPath);

    try {
        Field sysPathsField = ClassLoader.class.getDeclaredField(SYS_PATHS);
        sysPathsField.setAccessible(true);
        sysPathsField.set(null, null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(INACCESSIBLE_OR_INEXISTENT_SYS_PATHS_FIELD_EXCEPTION);
    }
}

From source file:com.github.pjungermann.config.types.DefaultConfigFactorySelectorTest.java

@SuppressWarnings("unchecked")
static Collection<ConfigFactory> getConfigFactories(DefaultConfigFactorySelector selector) {
    try {/*from ww w. j av a2s  . co m*/
        Field listField = DefaultConfigFactorySelector.class.getDeclaredField("configFactories");
        listField.setAccessible(true);
        return (Collection<ConfigFactory>) listField.get(selector);

    } catch (NoSuchFieldException e) {
        fail("field not found: " + e.getMessage());

    } catch (IllegalAccessException e) {
        fail("field was not accessible: " + e.getMessage());
    }

    return null;
}

From source file:Main.java

public static NetworkInfo createNetworkInfo(final int type, final boolean connected) throws Exception {
    Constructor<NetworkInfo> ctor = NetworkInfo.class.getDeclaredConstructor(int.class);
    ctor.setAccessible(true);/*  w ww . j  av a  2  s  .c  o  m*/
    NetworkInfo networkInfo = ctor.newInstance(0);
    Field typeField = NetworkInfo.class.getDeclaredField("mNetworkType");
    Field connectedField = NetworkInfo.class.getDeclaredField("mState");
    Field detailedStateField = NetworkInfo.class.getDeclaredField("mDetailedState");
    typeField.setAccessible(true);
    connectedField.setAccessible(true);
    detailedStateField.setAccessible(true);
    typeField.setInt(networkInfo, type);
    connectedField.set(networkInfo,
            connected == true ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED);
    detailedStateField.set(networkInfo,
            connected == true ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED);
    return networkInfo;
}

From source file:edu.umd.cs.marmoset.utilities.JProcess.java

/**
 * Uses reflection to extract the pid, a private field of the private class UNIXProcess.
 * This will fail on any non-Unix platform that doesn't use UNIXProcess.  It may
 * fail if the UNIXProcess class changes at all.  It may fail anyway for unpredictable
 * reasons.//w w  w. j a  v  a 2  s.c om
 * @param process The process
 * @return the pid of this process
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public static int getPid(Process process) throws NoSuchFieldException, IllegalAccessException {
    Class<? extends Process> processClass = process.getClass();
    Field pidField = processClass.getDeclaredField("pid");
    pidField.setAccessible(true);
    return pidField.getInt(process);
}

From source file:Main.java

public static Object getField(String paramString, Object paramObject) throws Exception {
    if (TextUtils.isEmpty(paramString))
        throw new RuntimeException("field name can not be empty");
    if (paramObject == null)
        throw new RuntimeException("target object can not be null");
    Field localField = paramObject.getClass().getDeclaredField(paramString);
    if (localField == null)
        throw new RuntimeException("target object: " + paramObject.getClass().getName()
                + " do not have this field: " + paramString);
    localField.setAccessible(true);
    return localField.get(paramObject);
}

From source file:Main.java

public static boolean setStatusBarDarkIcon(Window window, boolean dark) {
    boolean result = false;
    if (window != null) {
        try {/*from w w w  .  j a v  a2 s.c om*/
            WindowManager.LayoutParams lp = window.getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class
                    .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (dark) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            window.setAttributes(lp);
            result = true;
        } catch (Exception ignored) {
        }
    }
    return result;
}

From source file:natalia.dymnikova.cluster.scheduler.impl.Codec.java

private static Object doSanitize(final Object f) {
    final Class<?> aClass = f.getClass();
    if (!isStatic(aClass.getModifiers())) {
        final Class<?> enclosingClass = aClass.getEnclosingClass();
        if (enclosingClass != null) {
            for (final Field field : aClass.getDeclaredFields()) {
                if (enclosingClass.equals(field.getType())) {
                    field.setAccessible(true);
                    try {
                        field.set(f, null);
                    } catch (final IllegalAccessException e) {
                        throw unchecked(e);
                    }/*  w  w  w .java2 s  .c om*/
                }
            }
        }
    }
    return f;
}

From source file:cn.geobeans.web.common.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredField,    ?.//from  www  .jav  a 2s . c o  m
 * 
 * ?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;
}