Top Description Inners Fields Constructors Methods
java.lang.reflect

public Class AccessibleObject

extends Object
implements AnnotatedElement
Class Inheritance
All Implemented Interfaces
java.lang.reflect.AnnotatedElement
Known Direct Subclasses
java.lang.reflect.Executable, java.lang.reflect.Field
Imports
java.lang.annotation.Annotation, java.lang.invoke.MethodHandle, java.lang.ref.WeakReference, java.security.AccessController, jdk.internal.access.SharedSecrets, jdk.internal.misc.VM, jdk.internal.reflect.CallerSensitive, .Reflection, .ReflectionFactory, sun.security.action.GetPropertyAction, sun.security.util.SecurityConstants

The AccessibleObject class is the base class for Field, Method, and Constructor objects (known as reflected objects). It provides the ability to flag a reflected object as suppressing checks for Java language access control when it is used. This permits sophisticated applications with sufficient privilege, such as Java Object Serialization or other persistence mechanisms, to manipulate objects in a manner that would normally be prohibited.

Java language access control prevents use of private members outside their top-level class; package access members outside their package; protected members outside their package or subclasses; and public members outside their module unless they are declared in an exported package and the user reads their module. By default, Java language access control is enforced (with one variation) when Fields, Methods, or Constructors are used to get or set fields, to invoke methods, or to create and initialize new instances of classes, respectively. Every reflected object checks that the code using it is in an appropriate class, package, or module. The check when invoked by JNI code with no Java class on the stack only succeeds if the member and the declaring class are public, and the class is in a package that is exported to all modules.

The one variation from Java language access control is that the checks by reflected objects assume readability. That is, the module containing the use of a reflected object is assumed to read the module in which the underlying field, method, or constructor is declared.

Whether the checks for Java language access control can be suppressed (and thus, whether access can be enabled) depends on whether the reflected object corresponds to a member in an exported or open package (see setAccessible(boolean)).

Since
1.2
Java Language Specification
6.6 Access Control
External Specification
Java Native Interface Specification

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv volatile Object
pack-priv boolean
private static volatile boolean
private static volatile boolean
pack-priv static final ReflectionFactory

Constructor Summary

AccessConstructor and Description
protected
AccessibleObject()

Deprecated since 17.
Constructor: only used by the Java Virtual Machine.

Method Summary

Modifier and TypeMethod and Description
public final boolean

Returns:

true if the caller can access this reflected object.
canAccess
(Object
an instance object of the declaring class of this reflected object if it is an instance method or field
obj
)

Test if the caller can access this reflected object.

pack-priv final void
checkAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers)

pack-priv void
checkCanSetAccessible(Class<?> caller)

If the given AccessibleObject is a Constructor, Method or Field then checks that its declaring class is in a package that can be accessed by the given caller of setAccessible.

pack-priv final void
checkCanSetAccessible(Class<?> caller, Class<?> declaringClass)

private boolean
checkCanSetAccessible(Class<?> caller, Class<?> declaringClass, boolean throwExceptionIfDenied)

pack-priv static void
public <T extends Annotation> T
getAnnotation(Class<T>
the Class object corresponding to the annotation type
annotationClass
)

Implements java.lang.reflect.AnnotatedElement.getAnnotation.

Returns this element's annotation for the specified type if such an annotation is present, else null.
public Annotation[]
getAnnotations()

Implements java.lang.reflect.AnnotatedElement.getAnnotations.

Returns annotations that are present on this element.
public <T extends Annotation> T[]
getAnnotationsByType(Class<T>
the Class object corresponding to the annotation type
annotationClass
)

Overrides default java.lang.reflect.AnnotatedElement.getAnnotationsByType.

Returns annotations that are associated with this element.
public <T extends Annotation> T
getDeclaredAnnotation(Class<T>
the Class object corresponding to the annotation type
annotationClass
)

Overrides default java.lang.reflect.AnnotatedElement.getDeclaredAnnotation.

Returns this element's annotation for the specified type if such an annotation is directly present, else null.
public Annotation[]
getDeclaredAnnotations()

Implements java.lang.reflect.AnnotatedElement.getDeclaredAnnotations.

Returns annotations that are directly present on this element.
public <T extends Annotation> T[]
getDeclaredAnnotationsByType(Class<T>
the Class object corresponding to the annotation type
annotationClass
)

Overrides default java.lang.reflect.AnnotatedElement.getDeclaredAnnotationsByType.

Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.
pack-priv AccessibleObject
getRoot()

Returns the root AccessibleObject; or null if this object is the root.

private boolean
isAccessChecked(Class<?> caller, Class<?> targetClass)

private boolean
public boolean

Returns:

the value of the object's accessible flag
isAccessible
()

Deprecated since 9. This method is deprecated because its name hints that it checks if the reflected object is accessible when it actually indicates if the checks for Java language access control are suppressed.
Get the value of the accessible flag for this reflected object.
public boolean
isAnnotationPresent(Class<? extends Annotation>
the Class object corresponding to the annotation type
annotationClass
)

Overrides default java.lang.reflect.AnnotatedElement.isAnnotationPresent.

Returns true if an annotation for the specified type is present on this element, else false.
private boolean
isSubclassOf(Class<?> queryClass, Class<?> ofClass)

private static boolean
printStackTraceWhenAccessFails()

Returns true if a stack trace should be printed when access fails.

public static void
setAccessible(AccessibleObject[]
the array of AccessibleObjects
array
,
boolean
the new value for the accessible flag in each object
flag
)

Convenience method to set the accessible flag for an array of reflected objects with a single security check (for efficiency).

public void
setAccessible(boolean
the new value for the accessible flag
flag
)

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

pack-priv boolean
setAccessible0(boolean flag)

Sets the accessible flag and returns the new value

private boolean
slowVerifyAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers)

private void
throwInaccessibleObjectException(Class<?> caller, Class<?> declaringClass)

pack-priv String
toShortString()

Returns a short descriptive string to describe this object in log messages.

public final boolean

Returns:

true if the accessible flag is set to true; false if access cannot be enabled.
trySetAccessible
()

Set the accessible flag for this reflected object to true if possible.

pack-priv final boolean
verifyAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accessCheckCacheback to summary
pack-priv volatile Object accessCheckCache
overrideback to summary
pack-priv boolean override
printStackPropertiesSetback to summary
private static volatile boolean printStackPropertiesSet
printStackWhenAccessFailsback to summary
private static volatile boolean printStackWhenAccessFails
reflectionFactoryback to summary
pack-priv static final ReflectionFactory reflectionFactory
Annotations
@SuppressWarnings:removal

Constructor Detail

AccessibleObjectback to summary
protected AccessibleObject()

Deprecated

since 17.

Constructor: only used by the Java Virtual Machine.

Annotations
@Deprecated
since:17

Method Detail

canAccessback to summary
public final boolean canAccess(Object obj)

Test if the caller can access this reflected object. If this reflected object corresponds to an instance method or field then this method tests if the caller can access the given obj with the reflected object. For instance methods or fields then the obj argument must be an instance of the declaring class. For static members and constructors then obj must be null.

This method returns true if the accessible flag is set to true, i.e. the checks for Java language access control are suppressed, or if the caller can access the member as specified in The Java Language Specification, with the variation noted in the class description. If this method is invoked by JNI code with no caller class on the stack, this method returns true if the member and the declaring class are public, and the class is in a package that is exported unconditionally.

Parameters
obj:Object

an instance object of the declaring class of this reflected object if it is an instance method or field

Returns:boolean

true if the caller can access this reflected object.

Annotations
@CallerSensitive
Exceptions
IllegalArgumentException:
  • if this reflected object is a static member or constructor and the given obj is non-null, or
  • if this reflected object is an instance method or field and the given obj is null or of type that is not a subclass of the declaring class of the member.
Since
9
Java Language Specification
6.6 Access Control
External Specification
Java Native Interface Specification
See Also
trySetAccessible, setAccessible(boolean)
checkAccessback to summary
pack-priv final void checkAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers) throws IllegalAccessException
checkCanSetAccessibleback to summary
pack-priv void checkCanSetAccessible(Class<?> caller)

If the given AccessibleObject is a Constructor, Method or Field then checks that its declaring class is in a package that can be accessed by the given caller of setAccessible.

checkCanSetAccessibleback to summary
pack-priv final void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass)
checkCanSetAccessibleback to summary
private boolean checkCanSetAccessible(Class<?> caller, Class<?> declaringClass, boolean throwExceptionIfDenied)
checkPermissionback to summary
pack-priv static void checkPermission()
getAnnotationback to summary
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)

Implements java.lang.reflect.AnnotatedElement.getAnnotation.

Doc from java.lang.reflect.AnnotatedElement.getAnnotation.

Returns this element's annotation for the specified type if such an annotation is present, else null.

Note that any annotation returned by this method is a declaration annotation.

Implementation Specification

The default implementation throws UnsupportedOperationException; subclasses should override this method.

Parameters
<T>
the type of the annotation to query for and return if present
annotationClass:Class<T>

the Class object corresponding to the annotation type

Returns:T

this element's annotation for the specified annotation type if present on this element, else null

Annotations
@Override
Exceptions
NullPointerException:
if the given annotation class is null
Since
1.5
getAnnotationsback to summary
public Annotation[] getAnnotations()

Implements java.lang.reflect.AnnotatedElement.getAnnotations.

Doc from java.lang.reflect.AnnotatedElement.getAnnotations.

Returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Note that any annotations returned by this method are declaration annotations.

Returns:Annotation[]

annotations present on this element

Annotations
@Override
Since
1.5
getAnnotationsByTypeback to summary
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)

Overrides default java.lang.reflect.AnnotatedElement.getAnnotationsByType.

Doc from java.lang.reflect.AnnotatedElement.getAnnotationsByType.

Returns annotations that are associated with this element. If there are no annotations associated with this element, the return value is an array of length 0. The difference between this method and getAnnotation(Class) is that this method detects if its argument is a repeatable annotation type (JLS 9.6), and if so, attempts to find one or more annotations of that type by "looking through" a container annotation. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Note that any annotations returned by this method are declaration annotations.

Implementation Specification

The default implementation throws UnsupportedOperationException; subclasses should override this method.

Parameters
<T>
the type of the annotation to query for and return if present
annotationClass:Class<T>

the Class object corresponding to the annotation type

Returns:T[]

all this element's annotations for the specified annotation type if associated with this element, else an array of length zero

Annotations
@Override
Exceptions
NullPointerException:
if the given annotation class is null
Since
1.8
getDeclaredAnnotationback to summary
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)

Overrides default java.lang.reflect.AnnotatedElement.getDeclaredAnnotation.

Doc from java.lang.reflect.AnnotatedElement.getDeclaredAnnotation.

Returns this element's annotation for the specified type if such an annotation is directly present, else null. This method ignores inherited annotations. (Returns null if no annotations are directly present on this element.)

Note that any annotation returned by this method is a declaration annotation.

Parameters
<T>
the type of the annotation to query for and return if directly present
annotationClass:Class<T>

the Class object corresponding to the annotation type

Returns:T

this element's annotation for the specified annotation type if directly present on this element, else null

Annotations
@Override
Exceptions
NullPointerException:
if the given annotation class is null
Since
1.8
getDeclaredAnnotationsback to summary
public Annotation[] getDeclaredAnnotations()

Implements java.lang.reflect.AnnotatedElement.getDeclaredAnnotations.

Doc from java.lang.reflect.AnnotatedElement.getDeclaredAnnotations.

Returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Note that any annotations returned by this method are declaration annotations.

Implementation Specification

The default implementation throws UnsupportedOperationException; subclasses should override this method.

Returns:Annotation[]

annotations directly present on this element

Annotations
@Override
Since
1.5
getDeclaredAnnotationsByTypeback to summary
public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)

Overrides default java.lang.reflect.AnnotatedElement.getDeclaredAnnotationsByType.

Doc from java.lang.reflect.AnnotatedElement.getDeclaredAnnotationsByType.

Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present. This method ignores inherited annotations. If there are no specified annotations directly or indirectly present on this element, the return value is an array of length 0. The difference between this method and getDeclaredAnnotation(Class) is that this method detects if its argument is a repeatable annotation type (JLS 9.6), and if so, attempts to find one or more annotations of that type by "looking through" a container annotation if one is present. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Note that any annotations returned by this method are declaration annotations.

Parameters
<T>
the type of the annotation to query for and return if directly or indirectly present
annotationClass:Class<T>

the Class object corresponding to the annotation type

Returns:T[]

all this element's annotations for the specified annotation type if directly or indirectly present on this element, else an array of length zero

Annotations
@Override
Exceptions
NullPointerException:
if the given annotation class is null
Since
1.8
getRootback to summary
pack-priv AccessibleObject getRoot()

Returns the root AccessibleObject; or null if this object is the root. All subclasses override this method.

isAccessCheckedback to summary
private boolean isAccessChecked(Class<?> caller, Class<?> targetClass)
isAccessCheckedback to summary
private boolean isAccessChecked(Class<?> caller)
isAccessibleback to summary
public boolean isAccessible()

Deprecated

since 9.

This method is deprecated because its name hints that it checks if the reflected object is accessible when it actually indicates if the checks for Java language access control are suppressed. This method may return false on a reflected object that is accessible to the caller. To test if this reflected object is accessible, it should use canAccess(Object).

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

Returns:boolean

the value of the object's accessible flag

Annotations
@Deprecated
since:9
isAnnotationPresentback to summary
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)

Overrides default java.lang.reflect.AnnotatedElement.isAnnotationPresent.

Doc from java.lang.reflect.AnnotatedElement.isAnnotationPresent.

Returns true if an annotation for the specified type is present on this element, else false. This method is designed primarily for convenient access to marker annotations.

The truth value returned by this method is equivalent to: getAnnotation(annotationClass) != null

Parameters
annotationClass:Class<? extends Annotation>

the Class object corresponding to the annotation type

Returns:boolean

true if an annotation for the specified annotation type is present on this element, else false

Annotations
@Override
Exceptions
NullPointerException:
if the given annotation class is null
Since
1.5
isSubclassOfback to summary
private boolean isSubclassOf(Class<?> queryClass, Class<?> ofClass)
printStackTraceWhenAccessFailsback to summary
private static boolean printStackTraceWhenAccessFails()

Returns true if a stack trace should be printed when access fails.

setAccessibleback to summary
public static void setAccessible(AccessibleObject[] array, boolean flag)

Convenience method to set the accessible flag for an array of reflected objects with a single security check (for efficiency).

This method may be used to enable access to all reflected objects in the array when access to each reflected object can be enabled as specified by setAccessible(boolean).

If there is a security manager, its checkPermission method is first called with a ReflectPermission("suppressAccessChecks") permission.

A SecurityException is also thrown if any of the elements of the input array is a java.lang.reflect.Constructor object for the class java.lang.Class and flag is true.

Parameters
array:AccessibleObject[]

the array of AccessibleObjects

flag:boolean

the new value for the accessible flag in each object

Annotations
@CallerSensitive
Exceptions
InaccessibleObjectException:
if access cannot be enabled for all objects in the array
SecurityException:
if the request is denied by the security manager or an element in the array is a constructor for java.lang.Class
See Also
SecurityManager#checkPermission, ReflectPermission
setAccessibleback to summary
public void setAccessible(boolean flag)

Set the accessible flag for this reflected object to the indicated boolean value. A value of true indicates that the reflected object should suppress checks for Java language access control when it is used. A value of false indicates that the reflected object should enforce checks for Java language access control when it is used, with the variation noted in the class description.

This method may be used by a caller in class C to enable access to a member of declaring class D if any of the following hold:

  • C and D are in the same module.
  • The member is public and D is public in a package that the module containing D exports to at least the module containing C.
  • The member is protected static, D is public in a package that the module containing D exports to at least the module containing C, and C is a subclass of D.
  • D is in a package that the module containing D opens to at least the module containing C. All packages in unnamed and open modules are open to all modules and so this method always succeeds when D is in an unnamed or open module.

This method may be used by JNI code with no caller class on the stack to enable access to a member of declaring class D if and only if:

  • The member is public and D is public in a package that the module containing D exports unconditionally.

This method cannot be used to enable access to private members, members with default (package) access, protected instance members, or protected constructors when the declaring class is in a different module to the caller and the package containing the declaring class is not open to the caller's module.

This method cannot be used to enable write access to a non-modifiable final field. The following fields are non-modifiable:

  • static final fields declared in any class or interface
  • final fields declared in a hidden class
  • final fields declared in a record

The accessible flag when true suppresses Java language access control checks to only enable read access to these non-modifiable final fields.

If there is a security manager, its checkPermission method is first called with a ReflectPermission("suppressAccessChecks") permission.

Parameters
flag:boolean

the new value for the accessible flag

Annotations
@CallerSensitive
Exceptions
InaccessibleObjectException:
if access cannot be enabled
SecurityException:
if the request is denied by the security manager
External Specification
Java Native Interface Specification
See Also
trySetAccessible, java.lang.invoke.MethodHandles#privateLookupIn
setAccessible0back to summary
pack-priv boolean setAccessible0(boolean flag)

Sets the accessible flag and returns the new value

slowVerifyAccessback to summary
private boolean slowVerifyAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers)
throwInaccessibleObjectExceptionback to summary
private void throwInaccessibleObjectException(Class<?> caller, Class<?> declaringClass)
toShortStringback to summary
pack-priv String toShortString()

Returns a short descriptive string to describe this object in log messages.

trySetAccessibleback to summary
public final boolean trySetAccessible()

Set the accessible flag for this reflected object to true if possible. This method sets the accessible flag, as if by invoking setAccessible(true), and returns the possibly-updated value for the accessible flag. If access cannot be enabled, i.e. the checks or Java language access control cannot be suppressed, this method returns false (as opposed to setAccessible(true) throwing InaccessibleObjectException when it fails).

This method is a no-op if the accessible flag for this reflected object is true.

For example, a caller can invoke trySetAccessible on a Method object for a private instance method p.T::privateMethod to suppress the checks for Java language access control when the Method is invoked. If p.T class is in a different module to the caller and package p is open to at least the caller's module, the code below successfully sets the accessible flag to true.

p.T obj = ....;  // instance of p.T
    :
    Method m = p.T.class.getDeclaredMethod("privateMethod");
    if (m.trySetAccessible()) {
        m.invoke(obj);
    } else {
        // package p is not opened to the caller to access private member of T
        ...
    }

If this method is invoked by JNI code with no caller class on the stack, the accessible flag can only be set if the member and the declaring class are public, and the class is in a package that is exported unconditionally.

If there is a security manager, its checkPermission method is first called with a ReflectPermission("suppressAccessChecks") permission.

Returns:boolean

true if the accessible flag is set to true; false if access cannot be enabled.

Annotations
@CallerSensitive
Exceptions
SecurityException:
if the request is denied by the security manager
Since
9
External Specification
Java Native Interface Specification
See Also
java.lang.invoke.MethodHandles#privateLookupIn
verifyAccessback to summary
pack-priv final boolean verifyAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers)
java.lang.reflect back to summary

private Class AccessibleObject.Cache

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final WeakReference<Class<?>>
pack-priv final WeakReference<Class<?>>

Constructor Summary

AccessConstructor and Description
pack-priv
Cache(Class<?> caller, Class<?> target)

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
isCacheFor(Class<?> caller, Class<?> refc)

pack-priv static Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

callerRefback to summary
pack-priv final WeakReference<Class<?>> callerRef
targetRefback to summary
pack-priv final WeakReference<Class<?>> targetRef

Constructor Detail

Cacheback to summary
pack-priv Cache(Class<?> caller, Class<?> target)

Method Detail

isCacheForback to summary
pack-priv boolean isCacheFor(Class<?> caller, Class<?> refc)
protectedMemberCallerCacheback to summary
pack-priv static Object protectedMemberCallerCache(Class<?> caller, Class<?> refc)