Java Code Examples for java.lang.reflect.AccessibleObject#isAccessible()

The following examples show how to use java.lang.reflect.AccessibleObject#isAccessible() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigTreeBuilder.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
private static Object readValue(final AnnotatedMember member, final Object object) {
    if (object == null) {
        return null;
    }
    final Object res;
    final AccessibleObject accessor = (AccessibleObject) member.getMember();
    // case: private field
    if (!accessor.isAccessible()) {
        accessor.setAccessible(true);
        try {
            res = member.getValue(object);
        } finally {
            accessor.setAccessible(false);
        }
    } else {
        // public access (most likely getter)
        res = member.getValue(object);
    }
    return res;
}
 
Example 2
Source File: VMBridge_jdk13.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected boolean tryToMakeAccessible(Object accessibleObject)
{
    if (!(accessibleObject instanceof AccessibleObject)) {
        return false;
    }
    AccessibleObject accessible = (AccessibleObject)accessibleObject;
    if (accessible.isAccessible()) {
        return true;
    }
    try {
        accessible.setAccessible(true);
    } catch (Exception ex) { }

    return accessible.isAccessible();
}
 
Example 3
Source File: VMBridge_jdk13.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected boolean tryToMakeAccessible(Object accessibleObject)
{
    if (!(accessibleObject instanceof AccessibleObject)) {
        return false;
    }
    AccessibleObject accessible = (AccessibleObject)accessibleObject;
    if (accessible.isAccessible()) {
        return true;
    }
    try {
        accessible.setAccessible(true);
    } catch (Exception ex) { }

    return accessible.isAccessible();
}
 
Example 4
Source File: FieldUtils.java    From arthas with Apache License 2.0 6 votes vote down vote up
/**
 * XXX Default access superclass workaround.
 *
 * When a {@code public} class has a default access superclass with {@code 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 {@code public}.
 * Calling {@code 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
 * @return a boolean indicating whether the accessibility of the object was set to true.
 */
static boolean setAccessibleWorkaround(final AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return false;
    }
    final Member m = (Member) o;
    if (!o.isAccessible() && Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
            return true;
        } catch (final SecurityException e) { // NOPMD
            // ignore in favor of subsequent IllegalAccessException
        }
    }
    return false;
}
 
Example 5
Source File: ReflectionUtil.java    From common-utils with GNU General Public License v2.0 5 votes vote down vote up
public static void forceAccess(AccessibleObject object) {
    if (object == null || object.isAccessible()) {
        return;
    }
    try {
        object.setAccessible(true);
    } catch (SecurityException e) {
        throw ExceptionUtil.toRuntimeException(e);
    }
}
 
Example 6
Source File: JJJVMImplUtils.java    From j-j-jvm with Apache License 2.0 5 votes vote down vote up
public static void makeAccessible(final AccessibleObject obj) {
  if (obj != null && !obj.isAccessible()) {
    AccessController.doPrivileged(new PrivilegedAction() {
      public Object run() {
        obj.setAccessible(true);
        return null;
      }
    });
  }
}
 
Example 7
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * XXX Default access superclass workaround
 *
 * 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) to prevent access even when the modifer 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(AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers())
            && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) {
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}
 
Example 8
Source File: AccessibleRunner.java    From jrestless with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that the AccessibleObject is accessible before the function
 * gets invoked and resets the accessibility after invocation.
 *
 * @param ao
 *            the accessible object
 * @param fn
 *            the function to invoke
 * @return the return value of the passed function
 */
static <T> T run(AccessibleObject ao, Supplier<T> fn) {
	boolean origAccessibility = true;
	try {
		origAccessibility = ao.isAccessible();
		if (!origAccessibility) {
			ao.setAccessible(true);
		}
		return fn.get();
	} finally {
		if (!origAccessibility) {
			ao.setAccessible(origAccessibility);
		}
	}
}
 
Example 9
Source File: MemberUtils.java    From hasor with Apache License 2.0 5 votes vote down vote up
/**
 * XXX Default access superclass workaround
 *
 * 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) to prevent access even when the modifer 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;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers()) && MemberUtils.isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) {
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}
 
Example 10
Source File: MemberUtils.java    From DroidPlugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
static boolean setAccessibleWorkaround(final AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return false;
    }
    final Member m = (Member) o;
    if (!o.isAccessible() && Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
            return true;
        } catch (final SecurityException e) { // NOPMD
            // ignore in favor of subsequent IllegalAccessException
        }
    }
    return false;
}
 
Example 11
Source File: OgnlMemberAccess.java    From Milkomeda with MIT License 5 votes vote down vote up
public Object setup(Map context, Object target, Member member, String propertyName) {
    Object result = null;
    if (isAccessible(context, target, member, propertyName)) {
        AccessibleObject accessible = (AccessibleObject) member;
        if (!accessible.isAccessible()) {
            result = Boolean.FALSE;
            accessible.setAccessible(true);
        }
    }
    return result;
}
 
Example 12
Source File: ReflectHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static void ensureAccessibility(AccessibleObject accessibleObject) {
	if ( accessibleObject.isAccessible() ) {
		return;
	}

	accessibleObject.setAccessible( true );
}
 
Example 13
Source File: MemberUtils.java    From AndHook with MIT License 5 votes vote down vote up
/**
 * XXX Default access superclass workaround
 * <p>
 * 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(AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers())
            && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) { // NOPMD
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}
 
Example 14
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * XXX Default access superclass workaround
 *
 * 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
        }
    }
}
 
Example 15
Source File: MemberUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
/**
 * XXX Default access superclass workaround
 * <p>
 * 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(AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers())
            && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) { // NOPMD
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}
 
Example 16
Source File: DefaultMemberAccess.java    From anyline with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public Object setup(Map context, Object target, Member member,
		String propertyName) {
	Object result = null;

	if (isAccessible(context, target, member, propertyName)) {
		AccessibleObject accessible = (AccessibleObject) member;

		if (!accessible.isAccessible()) {
			result = Boolean.FALSE;
			accessible.setAccessible(true);
		}
	}
	return result;
}
 
Example 17
Source File: AccessibilityChanger.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * changes the accessibleObject accessibility and returns true if accessibility was changed
 */
public void enableAccess(AccessibleObject accessibleObject) {
    wasAccessible = accessibleObject.isAccessible();
    accessibleObject.setAccessible(true);
}
 
Example 18
Source File: ClosureTriggerBinding.java    From groovy with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private boolean isAccessible(AccessibleObject accessibleObject) {
    return accessibleObject.isAccessible();
}
 
Example 19
Source File: FrameworkUtil.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void setAccessible(AccessibleObject accessible) {
	if (!accessible.isAccessible()) {
		AccessController.doPrivileged(new SetAccessibleAction(accessible));
	}
}
 
Example 20
Source File: ReflectionUtils.java    From oval with Eclipse Public License 2.0 4 votes vote down vote up
public static void setAccessible(final AccessibleObject obj, final boolean accessible) {
   if (obj.isAccessible() == accessible)
      return;

   AccessController.doPrivileged(new SetAccessibleAction(obj, accessible));
}