Package Summary  Overview Summary

class:AccessibleObject [CHANGED]

  • All Implemented Interfaces:
    java.lang.reflect.AnnotatedElement


    public class AccessibleObject
    extends java.lang.Object
    implements java.lang.reflect.AnnotatedElement
    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 defaultchecks for Java language access control checkswhen it is used. TheThis 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 checks--for public, default (control prevents use of private members outside their class; package )access ,members outside their package; protected ,members outside their package or subclasses; and privatepublic members --outside their module unless they are performeddeclared in an exported package and the user reads their module. By default, Java language access control is enforced (with one variation) when Fields, MethodsField s, Methods, or ConstructorsConstructor s are used to get or set or getfields, to invoke methods, or to create and initialize new instances of classes, respectively. SettingEvery reflected object checks that the accessible flagcode using it is in an appropriate class, package, or module.

    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 permits sophisticated applications with sufficient privilege, such as Java Object Serialization or other persistence mechanisms,is assumed to manipulate objectsread the module in a manner that would normallywhich 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 prohibited.By default, aenabled) depends on whether the reflected object is not accessiblecorresponds to a member in an exported or open package (see setAccessible(boolean)).

    Since:
    1.2
    See Also:
    Field, Method, Constructor, ReflectPermission
    The Java™ Language Specification :
    6.6 Access Control
  • All Implemented Interfaces:
    java.lang.reflect.AnnotatedElement


    public class AccessibleObject
    extends java.lang.Object
    implements java.lang.reflect.AnnotatedElement
    The AccessibleObject class is the base class for Field, Method and Constructor objects. It provides the ability to flag a reflected object as suppressing default Java language access control checks when it is used. The access checks--for public, default (package) access, protected, and private members--are performed when Fields, Methods or Constructors are used to set or get fields, to invoke methods, or to create and initialize new instances of classes, respectively.

    Setting the accessible flag in a reflected object 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.

    By default, a reflected object is not accessible.

    Since:
    1.2
    See Also:
    Field, Method, Constructor, ReflectPermission
  • All Implemented Interfaces:
    java.lang.reflect.AnnotatedElement


    public class AccessibleObject
    extends java.lang.Object
    implements java.lang.reflect.AnnotatedElement
    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 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 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
    See The Java™ Language Specification :
    6.6 Access Control

constructor:AccessibleObject() [NONE]

  • AccessibleObject

    protected AccessibleObject()
    Constructor: only used by the Java Virtual Machine.

method:setAccessible(java.lang.reflect.AccessibleObject[], boolean) [CHANGED]

  • setAccessible

    public static void setAccessible(AccessibleObject[] array,
                                     boolean flag)throwsjava.lang.SecurityException
    
    Convenience method to set the accessible flag for an array of reflected objects with a single security check (for efficiency).

    First, ifThis 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 raisedalso thrown if flag is true but accessibility ofany of the elements of the input array may not be changed (for example, if the element objectis a Constructor object for the class java.lang.Class ). In the event of such a SecurityException, the accessibility of objects is set toand flag for array elements up to (and excluding) the element for which the exception occurred; the accessibility of elements beyond (and including) the element for which the exception occurredis unchangedtrue.

    Parameters:
    array - the array of AccessibleObjects
    flag - the new value for the accessible flag in each object
    Throws:
    InaccessibleObjectException - if access cannot be enabled for all objects in the array
    java.lang.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(java.security.Permission), RuntimePermissionReflectPermission
  • setAccessible

    public static void setAccessible(AccessibleObject[] array,
                                     boolean flag)
                              throws java.lang.SecurityException
    Convenience method to set the accessible flag for an array of objects with a single security check (for efficiency).

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

    A SecurityException is raised if flag is true but accessibility of any of the elements of the input array may not be changed (for example, if the element object is a Constructor object for the class Class). In the event of such a SecurityException, the accessibility of objects is set to flag for array elements up to (and excluding) the element for which the exception occurred; the accessibility of elements beyond (and including) the element for which the exception occurred is unchanged.

    Parameters:
    array - the array of AccessibleObjects
    flag - the new value for the accessible flag in each object
    Throws:
    java.lang.SecurityException - if the request is denied.
    See Also:
    SecurityManager.checkPermission(java.security.Permission), RuntimePermission
  • setAccessible

    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 Constructor object for the class java.lang.Class and flag is true.

    Parameters:
    array - the array of AccessibleObjects
    flag - the new value for the accessible flag in each object
    Throws:
    InaccessibleObjectException - if access cannot be enabled for all objects in the array
    java.lang.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(java.security.Permission), ReflectPermission

method:setAccessible(boolean) [CHANGED]

  • setAccessible

    public void setAccessible(boolean flag)throwsjava.lang.SecurityException
    
    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 checkingcontrol 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 checks.First,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 Dexports to at least the module containing C.
    • The member is protectedstatic, 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 Dopens 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 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.

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

    A SecurityException is raised if flag is true but accessibility of this object may not be changed (for example, if this element object is a Constructor object for the class Class).

    A SecurityException is raised if this object is a Constructor object for the class java.lang.Class, and flag is true.

    Parameters:
    flag - the new value for the accessible flag
    Throws:
    InaccessibleObjectException - if access cannot be enabled
    java.lang.SecurityException - if the request is denied .by the security manager
    See Also:
    SecurityManager.checkPermissiontrySetAccessible(), MethodHandles.privateLookupIn(java.security.Permission), RuntimePermissionlang.Class<?>, java.lang.invoke.MethodHandles.Lookup)
  • setAccessible

    public void setAccessible(boolean flag)
                       throws java.lang.SecurityException
    Set the accessible flag for this object to the indicated boolean value. A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates that the reflected object should enforce Java language access checks.

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

    A SecurityException is raised if flag is true but accessibility of this object may not be changed (for example, if this element object is a Constructor object for the class Class).

    A SecurityException is raised if this object is a Constructor object for the class java.lang.Class, and flag is true.

    Parameters:
    flag - the new value for the accessible flag
    Throws:
    java.lang.SecurityException - if the request is denied.
    See Also:
    SecurityManager.checkPermission(java.security.Permission), RuntimePermission
  • setAccessible

    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 Dexports to at least the module containing C.
    • The member is protectedstatic, 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 Dopens 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 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.

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

    Parameters:
    flag - the new value for the accessible flag
    Throws:
    InaccessibleObjectException - if access cannot be enabled
    java.lang.SecurityException - if the request is denied by the security manager
    See Also:
    trySetAccessible(), MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)

method:trySetAccessible() [ADDED]

  • trySetAccessible

    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 there is a security manager, its checkPermission method is first called with a ReflectPermission("suppressAccessChecks") permission.

    Returns:
    true if the accessible flag is set to true; false if access cannot be enabled.
    Throws:
    java.lang.SecurityException - if the request is denied by the security manager
    Since:
    9
    See Also:
    MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)

method:isAccessible() [CHANGED]

  • isAccessible

    @Deprecated(since="9")
    public 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. 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:
    the value of the object's accessible flag
  • isAccessible

    public boolean isAccessible()
    Get the value of the accessible flag for this object.
    Returns:
    the value of the object's accessible flag
  • isAccessible

    @Deprecated(since="9")
    public 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. 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:
    the value of the object's accessible flag

method:canAccess(java.lang.Object) [ADDED]

  • canAccess

    public final boolean canAccess(java.lang.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.

    Parameters:
    obj - an instance object of the declaring class of this reflected object if it is an instance method or field
    Returns:
    true if the caller can access this reflected object.
    Throws:
    java.lang.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
    See Also:
    trySetAccessible(), setAccessible(boolean)
    See The Java™ Language Specification :
    6.6 Access Control

method:getAnnotation(java.lang.Class) [NONE]

  • getAnnotation

    public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> annotationClass)
    Description copied from interface: java.lang.reflect.AnnotatedElement
    Returns this element's annotation for the specified type if such an annotation is present, else null.
    Specified by:
    getAnnotation in interface java.lang.reflect.AnnotatedElement
    Type Parameters:
    T - the type of the annotation to query for and return if present
    Parameters:
    annotationClass - the Class object corresponding to the annotation type
    Returns:
    this element's annotation for the specified annotation type if present on this element, else null
    Throws:
    java.lang.NullPointerException - if the given annotation class is null
    Since:
    1.5

method:isAnnotationPresent(java.lang.Class) [NONE]

  • isAnnotationPresent

    public boolean isAnnotationPresent(Class<? extends java.lang.annotation.Annotation> annotationClass)
    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

    The body of the default method is specified to be the code above.

    Specified by:
    isAnnotationPresent in interface java.lang.reflect.AnnotatedElement
    Parameters:
    annotationClass - the Class object corresponding to the annotation type
    Returns:
    true if an annotation for the specified annotation type is present on this element, else false
    Throws:
    java.lang.NullPointerException - if the given annotation class is null
    Since:
    1.5

method:getAnnotationsByType(java.lang.Class) [NONE]

  • getAnnotationsByType

    public <T extends java.lang.annotation.Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
    Description copied from interface: java.lang.reflect.AnnotatedElement
    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 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.
    Specified by:
    getAnnotationsByType in interface java.lang.reflect.AnnotatedElement
    Type Parameters:
    T - the type of the annotation to query for and return if present
    Parameters:
    annotationClass - the Class object corresponding to the annotation type
    Returns:
    all this element's annotations for the specified annotation type if associated with this element, else an array of length zero
    Throws:
    java.lang.NullPointerException - if the given annotation class is null
    Since:
    1.8

method:getAnnotations() [NONE]

  • getAnnotations

    public java.lang.annotation.Annotation[] getAnnotations()
    Description copied from interface: java.lang.reflect.AnnotatedElement
    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.
    Specified by:
    getAnnotations in interface java.lang.reflect.AnnotatedElement
    Returns:
    annotations present on this element
    Since:
    1.5

method:getDeclaredAnnotation(java.lang.Class) [NONE]

  • getDeclaredAnnotation

    public <T extends java.lang.annotation.Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
    Description copied from interface: java.lang.reflect.AnnotatedElement
    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.)
    Specified by:
    getDeclaredAnnotation in interface java.lang.reflect.AnnotatedElement
    Type Parameters:
    T - the type of the annotation to query for and return if directly present
    Parameters:
    annotationClass - the Class object corresponding to the annotation type
    Returns:
    this element's annotation for the specified annotation type if directly present on this element, else null
    Throws:
    java.lang.NullPointerException - if the given annotation class is null
    Since:
    1.8

method:getDeclaredAnnotationsByType(java.lang.Class) [NONE]

  • getDeclaredAnnotationsByType

    public <T extends java.lang.annotation.Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)
    Description copied from interface: java.lang.reflect.AnnotatedElement
    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 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.
    Specified by:
    getDeclaredAnnotationsByType in interface java.lang.reflect.AnnotatedElement
    Type Parameters:
    T - the type of the annotation to query for and return if directly or indirectly present
    Parameters:
    annotationClass - the Class object corresponding to the annotation type
    Returns:
    all this element's annotations for the specified annotation type if directly or indirectly present on this element, else an array of length zero
    Throws:
    java.lang.NullPointerException - if the given annotation class is null
    Since:
    1.8

method:getDeclaredAnnotations() [NONE]

  • getDeclaredAnnotations

    public java.lang.annotation.Annotation[] getDeclaredAnnotations()
    Description copied from interface: java.lang.reflect.AnnotatedElement
    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.
    Specified by:
    getDeclaredAnnotations in interface java.lang.reflect.AnnotatedElement
    Returns:
    annotations directly present on this element
    Since:
    1.5