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.cloudera.recordservice.tests.ClusterController.java

/**
 * This method allows the caller to add environment variables to the JVM.
 * There is no easy way to do this through a simple call, such as there is to
 * read env variables using System.getEnv(variableName). Much of the method
 * was written with guidance from stack overflow:
 * http://stackoverflow.com/questions//from w  w w  . j  a  v  a2s . co m
 * /318239/how-do-i-set-environment-variables-from-java
 */
protected static void setEnv(Map<String, String> newenv) {
    try {
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.putAll(newenv);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.putAll(newenv);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.clear();
                    map.putAll(newenv);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:com.elasticbox.jenkins.k8s.util.TestUtils.java

public static void setEnv(Map<String, String> newenv) {
    try {/* www.  jav  a2s .co m*/
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        env.putAll(newenv);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        cienv.putAll(newenv);
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    map.putAll(newenv);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:com.framework.infrastructure.utils.ReflectionUtils.java

/**
 * , DeclaredField, ./*w w w  . j av a  2s  .  com*/
 * 
 * Object, null.
 */
public static Field getAccessibleField(final Object obj, final String 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.zhuangjy.dao.ReflectionUtil.java

private static void getFieldsByAnnotation(List<Field> list, Class<? extends Annotation> annotation, Class clz) {
    if (clz.equals(Object.class)) {
        return;/*  w  w w .  j  a  v  a  2s. co m*/
    }
    Field[] fields = clz.getDeclaredFields();
    if (fields == null || fields.length == 0) {
        return;
    }
    for (Field f : fields) {
        f.setAccessible(true);
        if (f.getAnnotation(annotation) != null) {
            list.add(f);
        }
    }
}

From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java

/**
 * objectmap/*from w  w  w  . j a  v  a  2 s  . com*/
* @param obj
* @return
* @throws Exception
*/
public static Map<String, Object> objectToMap(Object obj) throws Exception {
    if (obj == null) {
        return null;
    }

    Map<String, Object> map = new HashMap<String, Object>();

    Field[] declaredFields = obj.getClass().getDeclaredFields();
    for (Field field : declaredFields) {
        field.setAccessible(true);
        map.put(field.getName(), field.get(obj));
    }

    return map;
}

From source file:net.sf.keystore_explorer.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Hack to disable crypto restrictions until Java 9 is out.
 *
 * See http://stackoverflow.com/a/22492582/2672392
 *///  ww w  .  jav  a2 s .c  om
public static void removeRestrictions() {
    try {
        Class<?> jceSecurityClass = Class.forName("javax.crypto.JceSecurity");
        Class<?> cryptoPermissionsClass = Class.forName("javax.crypto.CryptoPermissions");
        Class<?> cryptoAllPermissionClass = Class.forName("javax.crypto.CryptoAllPermission");

        Field isRestrictedField = jceSecurityClass.getDeclaredField("isRestricted");
        isRestrictedField.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL);
        isRestrictedField.set(null, false);

        Field defaultPolicyField = jceSecurityClass.getDeclaredField("defaultPolicy");
        defaultPolicyField.setAccessible(true);
        PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null);

        Field permsField = cryptoPermissionsClass.getDeclaredField("perms");
        permsField.setAccessible(true);
        ((Map<?, ?>) permsField.get(defaultPolicy)).clear();

        Field cryptoAllPermissionInstanceField = cryptoAllPermissionClass.getDeclaredField("INSTANCE");
        cryptoAllPermissionInstanceField.setAccessible(true);
        defaultPolicy.add((Permission) cryptoAllPermissionInstanceField.get(null));
    } catch (Exception e) {
        // ignore
    }
}

From source file:de.Keyle.MyPet.util.BukkitUtil.java

public static String getPlayerLanguage(Player player) {
    if (!(player instanceof CraftPlayer)) {
        return "en_US";
    }/*from  w w w . j  a  v a2 s.  c  om*/
    EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
    try {
        Field field = entityPlayer.getClass().getDeclaredField("locale");
        field.setAccessible(true);

        return (String) field.get(entityPlayer);
    } catch (Exception e) {
        return "en_US";
    }
}

From source file:com.savor.ads.core.ApiRequestFactory.java

@NonNull
private static JSONObject getJSONObject(Object object) {
    Field[] fields = object.getClass().getDeclaredFields();
    JSONObject jObject = new JSONObject();
    for (Field field : fields) {
        try {/*from w  ww .j a v  a 2  s. c o m*/
            field.setAccessible(true);
            Object fieldValue = field.get(object);
            if (fieldValue instanceof String || fieldValue instanceof Number || fieldValue == null) {
                jObject.put(field.getName(), fieldValue);
            } else if (fieldValue instanceof List) {
                jObject.put(field.getName(), getJSONArray((List<?>) fieldValue));
            } else {
                jObject.put(field.getName(), getJSONObject(fieldValue));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return jObject;
}

From source file:com.elasticbox.jenkins.k8s.util.TestUtils.java

public static void clearEnv(String... keys) {
    try {/*www.jav a 2 s  . c  om*/
        Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
        for (String key : keys) {
            env.remove(key);
        }
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
                .getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
        for (String key : keys) {
            cienv.remove(key);
        }
    } catch (NoSuchFieldException e) {
        try {
            Class[] classes = Collections.class.getDeclaredClasses();
            Map<String, String> env = System.getenv();
            for (Class cl : classes) {
                if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                    Field field = cl.getDeclaredField("m");
                    field.setAccessible(true);
                    Object obj = field.get(env);
                    Map<String, String> map = (Map<String, String>) obj;
                    for (String key : keys) {
                        map.remove(key);
                    }
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:io.servicecomb.config.TestConfigUtil.java

@SuppressWarnings("unchecked")
private static void setEnv(String key, String value) throws IllegalAccessException, NoSuchFieldException {
    Class<?>[] classes = Collections.class.getDeclaredClasses();
    Map<String, String> env = System.getenv();
    for (Class<?> cl : classes) {
        if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
            Field field = cl.getDeclaredField("m");
            field.setAccessible(true);
            Object obj = field.get(env);
            Map<String, String> map = (Map<String, String>) obj;
            map.put(key, value);//from   w ww  .j av a2 s . c o  m
        }
    }
}