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

private static void setField(Object obj, Class<?> cl, String field, Object value)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

    Field localField = cl.getDeclaredField(field);
    localField.setAccessible(true);
    localField.set(obj, value);/*from  w  ww  .  ja v a 2 s.  com*/
}

From source file:Main.java

public static boolean setReflection(Object itemToGetObject, String objectName, Object newValue) {
    try {//from  w w  w. j av a2s  . c  om
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        field.set(itemToGetObject, newValue);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static void startActivityForResult(Fragment fragment, Intent intent, int requestCode, Bundle options) {
    if (Build.VERSION.SDK_INT >= 19) {
        if ((requestCode & 0xffff0000) != 0) {
            throw new IllegalArgumentException("Can only use lower 16 bits" + " for requestCode");
        }/*from   w  w w  .j  a va 2  s . c om*/
        if (requestCode != -1) {
            try {
                Field mIndex = Fragment.class.getDeclaredField("mIndex");
                mIndex.setAccessible(true);
                requestCode = ((mIndex.getInt(fragment) + 1) << 16) + (requestCode & 0xffff);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        ActivityCompat.startActivityForResult(fragment.getActivity(), intent, requestCode, options);
    } else {
        fragment.getActivity().startActivityFromFragment(fragment, intent, requestCode);
    }
}

From source file:Main.java

public static void setAccessibleIfNeeded(Field field) {
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }/* ww w. ja  v  a  2 s. c o m*/
}

From source file:net.projectmonkey.spring.acl.util.reflect.FieldUtil.java

public static Object getFieldValue(final Object domainObject, final String fieldName) {
    Field field = ReflectionUtils.findField(domainObject.getClass(), fieldName);
    field.setAccessible(true);
    try {/*from   w  ww . jav a  2  s .c o  m*/
        return field.get(domainObject);
    } catch (Exception e) {
        throw new AuthenticationServiceException("Exception retrieving field " + fieldName, e);
    }
}

From source file:bigbluej.ReflectionUtils.java

public static SortedMap<String, Object> getFieldsAndValuesInSortedMap(Object object)
        throws IllegalAccessException {
    Validate.notNull(object);/*w w w.  j av  a2s  .  c o  m*/
    SortedMap<String, Object> fieldAndValues = new TreeMap<>();
    for (Field field : object.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(object);
        if (!field.isSynthetic() && value != null) {
            fieldAndValues.put(name, value);
        }
    }
    return fieldAndValues;
}

From source file:Main.java

/**
 * Decode a variable. Variables are in the form $varname. If the incoming
 * expression is not a variable or is not recognised it is simply returned
 * verbatim.//from   w w w  .  j a  v  a  2  s.  c  o  m
 * @param in The incoming attribute
 * @return String
 * @throws Exception
 */
private static String decode(String in) throws Exception {
    if (in != null && in.length() > 1 && in.charAt(0) == '$') {
        String key = in.substring(1);
        if (key.charAt(0) == '#') {
            // It's a class name and reflection job in the form $$full.class.name.member
            int lastIdx = key.lastIndexOf('.');
            String member = key.substring(lastIdx + 1);
            String className = key.substring(1, lastIdx);
            Class<?> clazz = Class.forName(className);
            Field field = clazz.getDeclaredField(member);
            field.setAccessible(true);
            return String.valueOf(field.get(null));
        } else {
            String val = vars.get(key);
            if (val == null) {
                throw new Exception("Unknown variable " + in);
            } else {
                return val;
            }
        }
    } else {
        return in;
    }
}

From source file:Main.java

public static void setDialogPositiveButtonHolder(DialogInterface dialog, Boolean flag) {
    try {/* w  ww  .j a  va  2s  . co m*/
        Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
        field.setAccessible(true);
        field.set(dialog, flag);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void injectClassField(Class clazz, String fieldStr, long newValue) {
    try {/*w w w  . ja v  a  2  s  . c om*/
        final Field field = clazz.getDeclaredField(fieldStr);
        if (field != null) {
            field.setAccessible(true);
            Object obj = null;
            field.set(obj, newValue);
        }
    } catch (Exception e) {
        Log.d("REFLECTION", clazz.getSimpleName() + " injection failed for field: " + fieldStr);
    }
}

From source file:Main.java

/**
 * get GridView vertical spacing/*www .ja  va  2s .  co  m*/
 * 
 * @param view
 * @return
 */
public static int getGridViewVerticalSpacing(GridView view) {
    // get mVerticalSpacing by android.widget.GridView
    Class<?> demo = null;
    int verticalSpacing = 0;
    try {
        demo = Class.forName(CLASS_NAME_GRID_VIEW);
        Field field = demo.getDeclaredField(FIELD_NAME_VERTICAL_SPACING);
        field.setAccessible(true);
        verticalSpacing = (Integer) field.get(view);
        return verticalSpacing;
    } catch (Exception e) {
        /**
         * accept all exception, include ClassNotFoundException, NoSuchFieldException, InstantiationException,
         * IllegalArgumentException, IllegalAccessException, NullPointException
         */
        e.printStackTrace();
    }
    return verticalSpacing;
}