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:ReflectionUtils.java

/**
 * Make the given field accessible, explicitly setting it accessible if necessary.
 * The <code>setAccessible(true)</code> method is only called when actually necessary,
 * to avoid unnecessary conflicts with a JVM SecurityManager (if active).
 * @param field the field to make accessible
 * @see java.lang.reflect.Field#setAccessible
 *///ww  w  .java2 s  .  c  o  m
public static void makeAccessible(Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);
    }
}

From source file:pl.softech.eav.example.SimpleInMemmoryRepository.java

private static void setId(AbstractEntity entity, Long id) {
    try {/*from w ww.  ja v a  2  s . c om*/
        Field f = AbstractEntity.class.getDeclaredField("id");
        f.setAccessible(true);
        f.set(entity, id);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:org.jongo.model.IdSpecSet.java

public static Object id(Object specInstance) {
    Field field = idField(specInstance.getClass());

    field.setAccessible(true);
    try {/*  ww w.  j ava  2s. co  m*/
        return field.get(specInstance);
    } catch (Exception e) {
        throw new RuntimeException("could not set id", e);
    }
}

From source file:org.artifactory.util.HttpClientUtils.java

private static RequestConfig getDefaultConfig(HttpClient client) {
    if (client == null) {
        return null;
    }//from  ww w.  ja v  a2 s .  c  o  m
    try {
        HttpClient httpClient = extractCloseableHttpClient(client);
        Field requestConfigField = httpClient.getClass().getDeclaredField("defaultConfig");
        requestConfigField.setAccessible(true);
        return (RequestConfig) ReflectionUtils.getField(requestConfigField, httpClient);
    } catch (NoSuchFieldException e) {
        throw new IllegalStateException("Failed to get default request config", e);
    }
}

From source file:com.kenai.redminenb.repository.RedmineManagerFactoryHelper.java

public static Transport getTransportFromManager(final RedmineManager rm) {
    return AccessController.doPrivileged(new PrivilegedAction<Transport>() {

        @Override//  w w w  .  j a v a  2 s .c o  m
        public Transport run() {
            try {
                Field transportField = RedmineManager.class.getDeclaredField("transport");
                transportField.setAccessible(true);
                return (Transport) transportField.get(rm);
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                    | IllegalAccessException ex) {
                throw new RuntimeException("Failed to get transport from redmine manager", ex);
            }
        }
    });
}

From source file:api_proto3.TestElf.java

@SuppressWarnings("unchecked")
public static HashMap<Object, HikariPool> getMultiPool(HikariDataSource ds) {
    try {//  w w  w.ja  v a  2 s.  co m
        Field field = ds.getClass().getDeclaredField("multiPool");
        field.setAccessible(true);
        return (HashMap<Object, HikariPool>) field.get(ds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteEsInputFormat.java

private static void enableInputSplitDebugMode(List<InputSplit> list) {
    for (InputSplit is : list) {
        try {/*from   www.  java2s. co m*/
            Class<?> c = is.getClass();

            Field nodeIp = c.getDeclaredField("nodeIp");
            nodeIp.setAccessible(true);
            nodeIp.set(is, "127.0.0.1");
        } catch (Exception e) {
            //DEBUG
            //e.printStackTrace();
        }
    }
}

From source file:com.palantir.atlasdb.keyvalue.remoting.Utils.java

public static void setupRuleHacks(DropwizardClientRule rule) {
    try {//from   www  .  j  a va  2s. c o  m
        Field field = rule.getClass().getDeclaredField("testSupport");
        field.setAccessible(true);
        @SuppressWarnings("unchecked")
        DropwizardTestSupport<Configuration> testSupport = (DropwizardTestSupport<Configuration>) field
                .get(rule);
        ObjectMapper mapper = testSupport.getEnvironment().getObjectMapper();
        mapper.registerModule(Utils.module);
        mapper.registerModule(new GuavaModule());
        testSupport.getApplication();
    } catch (Exception e) {
        throw Throwables.throwUncheckedException(e);
    }
}

From source file:com.snapdeal.archive.util.ArchivalUtil.java

/**
 * Returns a map where key is string and value is object. Caution should be made to use this method iff the property is String 
 * @param objects//from   www  .  j a v a  2s .c o  m
 * @param propertyName
 * @return
 */
public static <K, T> Map<K, T> getPropertyObjectMap(List<T> objects, String propertyName,
        Class<K> propertyClass) {
    Map<K, T> propertyObjectMap = new HashMap<>();
    for (T t : objects) {
        Object value = null;
        try {
            Field field = t.getClass().getDeclaredField(propertyName);
            field.setAccessible(true);
            value = field.get(t);
            // value = PropertyUtils.getProperty(t, propertyName);
            K k = (K) propertyClass.cast(value);
            propertyObjectMap.put(k, t);
        } catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
            e.printStackTrace();
        }
    }
    return propertyObjectMap;
}

From source file:Main.java

private static BitmapFactory.Options getBitmapOptions(Context context) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = true;/*from w  ww . java 2s.co m*/
    options.inPreferredConfig = DEFAULT_BITMAP_CONFIG;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[DECODE_BUFFER_SIZE];
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        Field field = null;
        try {
            field = BitmapFactory.Options.class.getDeclaredField("inNativeAlloc");
            field.setAccessible(true);
            field.setBoolean(options, true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    int displayDensityDpi = context.getResources().getDisplayMetrics().densityDpi;
    float displayDensity = context.getResources().getDisplayMetrics().density;
    int density = 0;
    if (displayDensityDpi > DENSITY_240 && displayDensityDpi <= DENSITY_400 && displayDensity > 1.5f) {
        density = (int) (displayDensityDpi * SCALE_FACTOR);
    } else if (displayDensityDpi > DENSITY_400 && displayDensity > 2.5f) {
        density = (int) (displayDensityDpi * SCALE_FACTOR_2);
    }
    if (density == 0)
        return options;
    options.inDensity = density;
    options.inTargetDensity = density;
    return options;
}