Example usage for java.lang.reflect Field isAccessible

List of usage examples for java.lang.reflect Field isAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field isAccessible.

Prototype

@Deprecated(since = "9")
public boolean isAccessible() 

Source Link

Document

Get the value of the accessible flag for this reflected object.

Usage

From source file:xyz.klinker.android.article.Utils.java

/**
 * Changes the text selection handle colors.
 *//*from   w w w. j a  v a  2  s. c  o  m*/
static void changeTextSelectionHandleColors(TextView textView, int color) {
    textView.setHighlightColor(Color.argb(40, Color.red(color), Color.green(color), Color.blue(color)));

    try {
        Field editorField = TextView.class.getDeclaredField("mEditor");
        if (!editorField.isAccessible()) {
            editorField.setAccessible(true);
        }

        Object editor = editorField.get(textView);
        Class<?> editorClass = editor.getClass();

        String[] handleNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };
        String[] resNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" };

        for (int i = 0; i < handleNames.length; i++) {
            Field handleField = editorClass.getDeclaredField(handleNames[i]);
            if (!handleField.isAccessible()) {
                handleField.setAccessible(true);
            }

            Drawable handleDrawable = (Drawable) handleField.get(editor);

            if (handleDrawable == null) {
                Field resField = TextView.class.getDeclaredField(resNames[i]);
                if (!resField.isAccessible()) {
                    resField.setAccessible(true);
                }
                int resId = resField.getInt(textView);
                handleDrawable = ContextCompat.getDrawable(textView.getContext(), resId);
            }

            if (handleDrawable != null) {
                Drawable drawable = handleDrawable.mutate();
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                handleField.set(editor, drawable);
            }
        }
    } catch (Exception ignored) {
    }
}

From source file:arun.com.chromer.browsing.article.util.ArticleUtil.java

/**
 * Changes the text selection handle colors.
 *//*from ww w  .  jav a  2s .co  m*/
public static void changeTextSelectionHandleColors(TextView textView, int color) {
    textView.setHighlightColor(Color.argb(40, Color.red(color), Color.green(color), Color.blue(color)));

    try {
        Field editorField = TextView.class.getDeclaredField("mEditor");
        if (!editorField.isAccessible()) {
            editorField.setAccessible(true);
        }

        Object editor = editorField.get(textView);
        Class<?> editorClass = editor.getClass();

        String[] handleNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };
        String[] resNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" };

        for (int i = 0; i < handleNames.length; i++) {
            Field handleField = editorClass.getDeclaredField(handleNames[i]);
            if (!handleField.isAccessible()) {
                handleField.setAccessible(true);
            }

            Drawable handleDrawable = (Drawable) handleField.get(editor);

            if (handleDrawable == null) {
                Field resField = TextView.class.getDeclaredField(resNames[i]);
                if (!resField.isAccessible()) {
                    resField.setAccessible(true);
                }
                int resId = resField.getInt(textView);
                handleDrawable = ContextCompat.getDrawable(textView.getContext(), resId);
            }

            if (handleDrawable != null) {
                Drawable drawable = handleDrawable.mutate();
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                handleField.set(editor, drawable);
            }
        }
    } catch (Exception ignored) {
    }
}

From source file:org.talend.commons.utils.BeanUtils.java

public static void forceSetProperty(Object object, String propertyName, Object newValue)
        throws NoSuchFieldException {
    Field field = getDeclaredField(object, propertyName);
    boolean accessible = field.isAccessible();
    field.setAccessible(true);//from   w  w w  .j a  v  a  2 s  .  co  m
    try {
        field.set(object, newValue);
    } catch (IllegalAccessException e) {
        log.info("Error won't happen");
    }
    field.setAccessible(accessible);
}

From source file:org.talend.commons.utils.BeanUtils.java

public static Object forceGetProperty(Object object, String propertyName) throws NoSuchFieldException {
    Field field = getDeclaredField(object, propertyName);

    boolean accessible = field.isAccessible();
    field.setAccessible(true);//from   w ww. j  a  v a  2  s .  co  m

    Object result = null;
    try {
        result = field.get(object);
    } catch (IllegalAccessException e) {
        log.info("error wont' happen");
    }
    field.setAccessible(accessible);
    return result;
}

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

/**
 * string?trim?//from w  w  w  .  ja va2  s .  c o m
 * 
 * @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.npower.dm.util.BeanHelper.java

/**
 * private/protected/*from  w  w w . j  a  va  2 s . c om*/
 */
static public Object getDeclaredProperty(Object object, Field field) throws IllegalAccessException {
    assert object != null;
    assert field != null;
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    Object result = field.get(object);
    field.setAccessible(accessible);
    return result;
}

From source file:org.rhq.enterprise.server.cloud.util.StorageNodeConfigurationUtil.java

/**
 * If new value is null, replace that with existing configuration value
 * @param newConfig// w  w  w . jav a 2 s . c  o  m
 * @param oldConfig
 */
public static void syncConfigs(StorageNodeConfigurationComposite newConfig,
        StorageNodeConfigurationComposite oldConfig) {
    try {
        for (Field field : StorageNodeConfigurationComposite.class.getDeclaredFields()) {
            field.setAccessible(true);
            if (field.isAccessible() && field.getType() != StorageNode.class) {
                Object o = field.get(newConfig);
                if (o == null) {
                    Object oldValue = field.get(oldConfig);
                    if (oldValue != null) {
                        field.set(newConfig, oldValue);
                    }
                }
            }
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Could not process StorageNodeConfigurationComposite, ", e);
    }
}

From source file:Main.java

public static Collection<Field> getDeepDeclaredFields(Class<?> c) {
    if (_reflectedFields.containsKey(c)) {
        return _reflectedFields.get(c);
    }//from  ww  w .  j  a v a2  s  . co m
    Collection<Field> fields = new ArrayList<Field>();
    Class<?> curr = c;

    while (curr != null) {
        try {
            Field[] local = curr.getDeclaredFields();

            for (Field field : local) {
                if (!field.isAccessible()) {
                    try {
                        field.setAccessible(true);
                    } catch (Exception ignored) {
                    }
                }

                int modifiers = field.getModifiers();
                if (!Modifier.isStatic(modifiers) && !field.getName().startsWith("this$")
                        && !Modifier.isTransient(modifiers)) {
                    fields.add(field);
                }
            }
        } catch (ThreadDeath t) {
            throw t;
        } catch (Throwable ignored) {
        }

        curr = curr.getSuperclass();
    }
    _reflectedFields.put(c, fields);
    return fields;
}

From source file:com.pmarlen.model.controller.EntityJPAToPlainPojoTransformer.java

public static void copyPlainValues(Entity entity, Object plain) {
    final Class entityClass;
    entityClass = entity.getClass();/* w  ww . j a v a 2s . c o  m*/
    final String entityClassName = entityClass.getName();
    Hashtable<String, Object> propertiesToCopy = new Hashtable<String, Object>();
    Hashtable<String, Object> propertiesM2MToCopy = new Hashtable<String, Object>();
    List<String> propertiesWithNULLValueToCopy = new ArrayList<String>();
    List<String> propertiesKey = new ArrayList<String>();
    try {
        final Field[] declaredFields = entityClass.getDeclaredFields();
        for (Field f : declaredFields) {
            logger.trace("->create: \tcopy: " + entityClassName + "." + f.getName() + " accesible ? "
                    + (f.isAccessible()));
            if (!f.isAccessible()) {

                if (f.isAnnotationPresent(javax.persistence.Id.class)) {
                    propertiesKey.add(f.getName());
                }
                if (f.isAnnotationPresent(javax.persistence.Id.class)
                        && !f.isAnnotationPresent(javax.persistence.GeneratedValue.class)
                        && !Modifier.isStatic(f.getModifiers())) {

                    Object valueCopyed = PropertyUtils.getProperty(entity, f.getName());
                    if (valueCopyed != null) {
                        propertiesToCopy.put(f.getName(), valueCopyed);
                    } else {
                        propertiesWithNULLValueToCopy.add(f.getName());
                    }
                    logger.trace("->create:\t\t ID elegible 2 be copied, added to copy list: " + f.getName()
                            + " = " + valueCopyed + ", is really null?" + (valueCopyed == null));
                } else if (!f.isAnnotationPresent(javax.persistence.Id.class)
                        && !f.isAnnotationPresent(javax.persistence.OneToMany.class)
                        && !f.isAnnotationPresent(javax.persistence.ManyToMany.class)
                        && !Modifier.isStatic(f.getModifiers())) {

                    Object valueCopyed = PropertyUtils.getProperty(entity, f.getName());
                    if (valueCopyed != null) {
                        propertiesToCopy.put(f.getName(), valueCopyed);
                    } else {
                        propertiesWithNULLValueToCopy.add(f.getName());
                    }
                    logger.trace("->create:\t\t elegible 2 be copied, added to copy list: " + f.getName()
                            + " = " + valueCopyed + ", is really null?" + (valueCopyed == null));
                } else if (!f.isAnnotationPresent(javax.persistence.Id.class)
                        && f.isAnnotationPresent(javax.persistence.ManyToMany.class)) {

                    Object valueCopyed = PropertyUtils.getProperty(entity, f.getName());
                    if (valueCopyed != null) {
                        propertiesM2MToCopy.put(f.getName(), valueCopyed);
                    } else {
                        propertiesWithNULLValueToCopy.add(f.getName());
                    }
                    logger.trace("->create:\t\t M2M elegible 2 be copied, added to copy list: " + f.getName()
                            + " = " + valueCopyed + ", is really null?" + (valueCopyed == null));
                }
            }
        }
        logger.trace("->create:copy values ?");
        for (String p2c : propertiesToCopy.keySet()) {
            Object valueCopyed = propertiesToCopy.get(p2c);
            logger.trace("->create:\t\t copy value with SpringUtils: " + p2c + " = " + valueCopyed
                    + ", is null?" + (valueCopyed == null));
            BeanUtils.copyProperty(plain, p2c, valueCopyed);
        }
        for (String p2c : propertiesWithNULLValueToCopy) {
            logger.trace("->create:\t\t copy null with SpringUtils");
            BeanUtils.copyProperty(plain, p2c, null);
        }
    } catch (Exception e) {
        logger.error("..in copy", e);
    }
}

From source file:com.fengduo.bee.commons.util.ObjectUtils.java

/**
 * string?trim?/*from  w  ww . j a  v  a 2s  .  c om*/
 * 
 * @param object
 * @throws Exception
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void trimStringField(Object object, Class<?> clazz) throws Exception {
    if (object instanceof Map<?, ?>) {
        Map<Object, Object> target = new HashMap<Object, Object>();
        for (Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();

            if (key instanceof String) {
                key = StringUtils.trim((String) key);
            } else {
                trim(key);
            }

            if (value instanceof String) {
                value = StringUtils.trim((String) value);
                value = StringUtils.replace((String) value, "\"", StringUtils.EMPTY);
            } else {
                trim(value);
            }
            target.put(key, value);
        }
        ((Map<?, ?>) object).clear();
        ((Map) object).putAll((Map) target);
        return;
    }
    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);
            }
        }
    }
}