Example usage for java.lang.reflect AccessibleObject isAccessible

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

Introduction

In this page you can find the example usage for java.lang.reflect AccessibleObject 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:org.omnaest.utils.beans.result.BeanPropertyAccessor.java

/**
 * Returns the property value for the underlying Java Bean property from the given Java Bean object.
 * // w w  w .  ja v  a  2 s  .  c  o m
 * @see PropertyAccessType
 * @param bean
 * @param propertyAccessType
 * @param exceptionHandler
 * @return value or null if no value could be resolved
 */
public Tuple2<Object, Boolean> getPropertyValueAndSuccess(B bean, PropertyAccessType propertyAccessType,
        ExceptionHandler exceptionHandler) {
    //
    Object retval = null;
    boolean success = false;

    //
    final BeanPropertyAccessor<B> beanPropertyAccessor = this.propertyAccessType.equals(propertyAccessType)
            ? this
            : this.newBeanPropertyAccessorWithPropertyAccessType(propertyAccessType);

    //
    if (beanPropertyAccessor.isReadable()) {
        try {
            //
            AccessibleObject accessibleObject = null;
            if (PropertyAccessType.FIELD.equals(propertyAccessType)) {
                accessibleObject = beanPropertyAccessor.field;
            } else if (PropertyAccessType.PROPERTY.equals(propertyAccessType)) {
                accessibleObject = beanPropertyAccessor.methodGetter;
            }

            //
            boolean accessible = accessibleObject.isAccessible();
            if (!accessible) {
                accessibleObject.setAccessible(true);
            }

            //
            if (PropertyAccessType.FIELD.equals(propertyAccessType)) {
                retval = beanPropertyAccessor.field.get(bean);
                success = true;
            } else if (PropertyAccessType.PROPERTY.equals(propertyAccessType)) {
                retval = beanPropertyAccessor.methodGetter.invoke(bean, new Object[] {});
                success = true;
            }
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.handleException(e);
            }
        }
    }

    //
    return new Tuple2<Object, Boolean>(retval, success);
}

From source file:org.omnaest.utils.beans.result.BeanPropertyAccessor.java

/**
 * Sets the property value for the underlying Java Bean property for the given Java Bean object using the given
 * {@link PropertyAccessType}.//from ww  w  .  j av  a 2 s. c  o m
 * 
 * @param bean
 * @param value
 * @param propertyAccessType
 * @param exceptionHandler
 * @return true if no error occurs
 */
public boolean setPropertyValue(B bean, Object value, PropertyAccessType propertyAccessType,
        ExceptionHandler exceptionHandler) {
    //
    boolean retval = false;

    //
    final BeanPropertyAccessor<B> beanPropertyAccessor = this.propertyAccessType.equals(propertyAccessType)
            ? this
            : this.newBeanPropertyAccessorWithPropertyAccessType(propertyAccessType);

    //
    if (beanPropertyAccessor.isWritable() && bean != null) {
        try {
            //
            AccessibleObject accessibleObject = null;
            if (PropertyAccessType.FIELD.equals(propertyAccessType)) {
                accessibleObject = beanPropertyAccessor.field;
            } else if (PropertyAccessType.PROPERTY.equals(propertyAccessType)) {
                accessibleObject = beanPropertyAccessor.methodSetter;
            }

            //
            boolean accessible = accessibleObject.isAccessible();
            if (!accessible) {
                accessibleObject.setAccessible(true);
            }

            //
            if (PropertyAccessType.FIELD.equals(propertyAccessType)) {
                //
                beanPropertyAccessor.field.set(bean, value);
            } else if (PropertyAccessType.PROPERTY.equals(propertyAccessType)) {
                //
                beanPropertyAccessor.methodSetter.invoke(bean, value);
            }

            //
            retval = true;
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.handleException(e);
            }
        }
    }

    //
    return retval;
}

From source file:org.robobinding.util.MemberUtils.java

/**
 * /*w ww . ja  va2 s .c o  m*/
 * When a public class has a default access superclass with public members,
 * these members are accessible. Calling them from compiled code works fine.
 * Unfortunately, on some JVMs, using reflection to invoke these members
 * seems to (wrongly) prevent access even when the modifier is public.
 * Calling setAccessible(true) solves the problem but will only work from
 * sufficiently privileged code. Better workarounds would be gratefully
 * accepted.
 * 
 * @param o
 *            the AccessibleObject to set as accessible
 */
static void setAccessibleWorkaround(final AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    final Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (final SecurityException e) { // NOPMD
            // ignore in favor of
            // subsequent
            // IllegalAccessException
        }
    }
}