Example usage for java.lang.reflect AccessibleObject setAccessible

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

Introduction

In this page you can find the example usage for java.lang.reflect AccessibleObject setAccessible.

Prototype

@CallerSensitive 
public void setAccessible(boolean flag) 

Source Link

Document

Set the accessible flag for this reflected object to the indicated boolean value.

Usage

From source file:org.acoveo.tools.Reflection.java

/**
 * Return a PrivilegeAction object for aObj.setAccessible().
 * /*from   w w  w .  j av  a2s .  co  m*/
 * This method is from:
 * https://svn.apache.org/repos/asf/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
 * 
 * Requires security policy: 'permission java.lang.reflect.ReflectPermission
 * "suppressAccessChecks";'
 */
public static final PrivilegedAction<Object> setAccessibleAction(final AccessibleObject aObj,
        final boolean flag) {
    return new PrivilegedAction<Object>() {
        public Object run() {
            aObj.setAccessible(flag);
            return (Object) null;
        }
    };
}

From source file:org.apache.axis2.jaxws.server.endpoint.injection.impl.WebServiceContextInjectorImpl.java

/**
 * Set accessible.  This method must remain private
 *
 * @param obj   AccessibleObject//w  w  w  .  ja v  a  2s .  co  m
 * @param value true or false
 */
private static void setAccessible(final AccessibleObject obj, final boolean value) {
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            obj.setAccessible(value);
            return null;
        }
    });

}

From source file:org.apache.bval.util.reflection.Reflection.java

/**
 * Set the accessibility of {@code o} to {@code accessible}. If running without a {@link SecurityManager}
 * and {@code accessible == false}, this call is ignored (because any code could reflectively make any
 * object accessible at any time)./*from  w w w. ja v a  2  s  .  c om*/
 * @param o
 * @param accessible
 * @return whether a change was made.
 */
public static boolean setAccessible(final AccessibleObject o, boolean accessible) {
    if (o == null || o.isAccessible() == accessible) {
        return false;
    }
    if (!accessible && System.getSecurityManager() == null) {
        return false;
    }
    final Member m = (Member) o;

    // For public members whose declaring classes are public, we need do nothing:
    if (Modifier.isPublic(m.getModifiers()) && Modifier.isPublic(m.getDeclaringClass().getModifiers())) {
        return false;
    }
    o.setAccessible(accessible);
    return true;
}

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.
 * /* ww  w.  jav  a2  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  w  w w  . j  a  v  a  2s  .c om
 * 
 * @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.openstreetmap.josm.tools.Utils.java

/**
 * Sets {@code AccessibleObject}(s) accessible.
 * @param objects objects//from   w  ww  . java2  s . co  m
 * @see AccessibleObject#setAccessible
 * @since 10223
 */
public static void setObjectsAccessible(final AccessibleObject... objects) {
    if (objects != null && objects.length > 0) {
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            for (AccessibleObject o : objects) {
                o.setAccessible(true);
            }
            return null;
        });
    }
}

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

/**
 * //  ww  w .ja va 2  s .c om
 * 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
        }
    }
}