AnnotatedElementExecutable, Field
public class AccessibleObject extends Object implements AnnotatedElement
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)). 
| Modifier | Constructor | Description | 
|---|---|---|
| protected  | Deprecated. | 
| Modifier and Type | Method | Description | 
|---|---|---|
| final boolean | canAccess | Test if the caller can access this reflected object. | 
| <T extends Annotation> | getAnnotation | Returns this element's annotation for the specified type if such an annotation is present, else null. | 
| Annotation[] | getAnnotations() | Returns annotations that are present on this element. | 
| <T extends Annotation> | getAnnotationsByType | Returns annotations that are associated with this element. | 
| <T extends Annotation> | getDeclaredAnnotation | Returns this element's annotation for the specified type if such an annotation is directly present, else null. | 
| Annotation[] | getDeclaredAnnotations() | Returns annotations that are directly present on this element. | 
| <T extends Annotation> | getDeclaredAnnotationsByType | Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present. | 
| boolean | isAccessible() | 
Deprecated.  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. | 
| boolean | isAnnotationPresent | Returns true if an annotation for the specified type is present on this element, else false. | 
| void | setAccessible | Set the  accessibleflag for this reflected object to the indicated boolean value. | 
| static void | setAccessible | Convenience method to set the  accessibleflag for an array of reflected objects with a single security check (for efficiency). | 
| final boolean | trySetAccessible() | Set the  accessibleflag for this reflected object totrueif possible. | 
@Deprecated(since="17") protected AccessibleObject()
public static void setAccessible(AccessibleObject[] array, boolean flag)
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 Constructor object for the class java.lang.Class and flag is true.
array - the array of AccessibleObjectsflag - the new value for the accessible flag in each objectInaccessibleObjectException - if access cannot be enabled for all objects in the arraySecurityException - if the request is denied by the security manager or an element in the array is a constructor for 
         java.lang.Class
public void setAccessible(boolean flag)
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. public and D is public in a package that the module containing D exports to at least the module containing C. 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: 
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:
 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.
flag - the new value for the accessible flagInaccessibleObjectException - if access cannot be enabledSecurityException - if the request is denied by the security managerpublic final boolean trySetAccessible()
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. 
true if the accessible flag is set to true; false if access cannot be enabled.SecurityException - if the request is denied by the security manager@Deprecated(since="9") public boolean isAccessible()
false on a reflected object that is accessible to the caller. To test if this reflected object is accessible, it should use canAccess(Object).accessible flag for this reflected object.accessible flagpublic final boolean canAccess(Object obj)
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. 
obj - an instance object of the declaring class of this reflected object if it is an instance method or fieldtrue if the caller can access this reflected object.IllegalArgumentException - obj is non-null, or obj is null or of type that is not a subclass of the declaring class of the member.public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
Note that any annotation returned by this method is a declaration annotation.
getAnnotation in interface AnnotatedElement
UnsupportedOperationException; subclasses should override this method.T - the type of the annotation to query for and return if presentannotationClass - the Class object corresponding to the annotation typeNullPointerException - if the given annotation class is nullpublic boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
The truth value returned by this method is equivalent to: getAnnotation(annotationClass) != null
isAnnotationPresent in interface AnnotatedElement
annotationClass - the Class object corresponding to the annotation typeNullPointerException - if the given annotation class is nullpublic <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
AnnotatedElement.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.
getAnnotationsByType in interface AnnotatedElement
UnsupportedOperationException; subclasses should override this method.T - the type of the annotation to query for and return if presentannotationClass - the Class object corresponding to the annotation typeNullPointerException - if the given annotation class is nullpublic Annotation[] getAnnotations()
Note that any annotations returned by this method are declaration annotations.
getAnnotations in interface AnnotatedElement
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
Note that any annotation returned by this method is a declaration annotation.
getDeclaredAnnotation in interface AnnotatedElement
T - the type of the annotation to query for and return if directly presentannotationClass - the Class object corresponding to the annotation typeNullPointerException - if the given annotation class is nullpublic <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)
AnnotatedElement.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.
getDeclaredAnnotationsByType in interface AnnotatedElement
T - the type of the annotation to query for and return if directly or indirectly presentannotationClass - the Class object corresponding to the annotation typeNullPointerException - if the given annotation class is nullpublic Annotation[] getDeclaredAnnotations()
Note that any annotations returned by this method are declaration annotations.
getDeclaredAnnotations in interface AnnotatedElement
UnsupportedOperationException; subclasses should override this method.
    © 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
    https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/reflect/AccessibleObject.html