- java.lang.Object
-
- java.lang.reflect.AccessibleObject
-
- All Implemented Interfaces:
AnnotatedElement
- Direct Known Subclasses:
Executable,Field
public class AccessibleObject extends Object implements AnnotatedElement
TheAccessibleObjectclass is the base class forField,Method, andConstructorobjects (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
exportedpackage and the userreadstheir module. By default, Java language access control is enforced (with one variation) whenFields,Methods, orConstructors 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 Summary
Constructors Modifier Constructor Description protectedAccessibleObject()Constructor: only used by the Java Virtual Machine.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleancanAccess(Object obj)Test if the caller can access this reflected object.<T extends Annotation>
TgetAnnotation(Class<T> annotationClass)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>
T[]getAnnotationsByType(Class<T> annotationClass)Returns annotations that are associated with this element.<T extends Annotation>
TgetDeclaredAnnotation(Class<T> annotationClass)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>
T[]getDeclaredAnnotationsByType(Class<T> annotationClass)Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.booleanisAccessible()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.booleanisAnnotationPresent(Class<? extends Annotation> annotationClass)Returns true if an annotation for the specified type is present on this element, else false.voidsetAccessible(boolean flag)Set theaccessibleflag for this reflected object to the indicated boolean value.static voidsetAccessible(AccessibleObject[] array, boolean flag)Convenience method to set theaccessibleflag for an array of reflected objects with a single security check (for efficiency).booleantrySetAccessible()Set theaccessibleflag for this reflected object totrueif possible.
-
-
-
Method Detail
-
setAccessible
public static void setAccessible(AccessibleObject[] array, boolean flag)
Convenience method to set theaccessibleflag 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
checkPermissionmethod is first called with aReflectPermission("suppressAccessChecks")permission.A
SecurityExceptionis also thrown if any of the elements of the inputarrayis aConstructorobject for the classjava.lang.Classandflagis true.- Parameters:
array- the array of AccessibleObjectsflag- the new value for theaccessibleflag in each object- Throws:
InaccessibleObjectException- 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 forjava.lang.Class- See Also:
SecurityManager.checkPermission(java.security.Permission),ReflectPermission
-
setAccessible
public void setAccessible(boolean flag)
Set theaccessibleflag for this reflected object to the indicated boolean value. A value oftrueindicates that the reflected object should suppress checks for Java language access control when it is used. A value offalseindicates 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
Cto enable access to amemberofdeclaring classDif any of the following hold:-
CandDare in the same module. - The member is
publicandDispublicin a package that the module containingDexportsto at least the module containingC. - The member is
protectedstatic,Dispublicin a package that the module containingDexports to at least the module containingC, andCis a subclass ofD. -
Dis in a package that the module containingDopensto at least the module containingC. All packages in unnamed and open modules are open to all modules and so this method always succeeds whenDis 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
checkPermissionmethod is first called with aReflectPermission("suppressAccessChecks")permission.- Parameters:
flag- the new value for theaccessibleflag- Throws:
InaccessibleObjectException- if access cannot be enabledSecurityException- if the request is denied by the security manager- See Also:
trySetAccessible(),MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)
-
-
trySetAccessible
public final boolean trySetAccessible()
Set theaccessibleflag for this reflected object totrueif possible. This method sets theaccessibleflag, as if by invokingsetAccessible(true), and returns the possibly-updated value for theaccessibleflag. If access cannot be enabled, i.e. the checks or Java language access control cannot be suppressed, this method returnsfalse(as opposed tosetAccessible(true)throwingInaccessibleObjectExceptionwhen it fails).This method is a no-op if the
accessibleflag for this reflected object istrue.For example, a caller can invoke
trySetAccessibleon aMethodobject for a private instance methodp.T::privateMethodto suppress the checks for Java language access control when theMethodis invoked. Ifp.Tclass is in a different module to the caller and packagepis open to at least the caller's module, the code below successfully sets theaccessibleflag totrue.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
checkPermissionmethod is first called with aReflectPermission("suppressAccessChecks")permission.- Returns:
trueif theaccessibleflag is set totrue;falseif access cannot be enabled.- Throws:
SecurityException- if the request is denied by the security manager- Since:
- 9
- See Also:
MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)
-
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 returnfalseon a reflected object that is accessible to the caller. To test if this reflected object is accessible, it should usecanAccess(Object).Get the value of theaccessibleflag for this reflected object.- Returns:
- the value of the object's
accessibleflag
-
canAccess
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 givenobjwith the reflected object. For instance methods or fields then theobjargument must be an instance of thedeclaring class. For static members and constructors thenobjmust benull.This method returns
trueif theaccessibleflag is set totrue, 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:
trueif the caller can access this reflected object.- Throws:
IllegalArgumentException-- if this reflected object is a static member or constructor and
the given
objis non-null, or - if this reflected object is an instance method or field
and the given
objisnullor of type that is not a subclass of thedeclaring classof the member.
- if this reflected object is a static member or constructor and
the given
- Since:
- 9
- See Also:
trySetAccessible(),setAccessible(boolean)- See The Java™ Language Specification:
- 6.6 Access Control
-
getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
Description copied from interface:AnnotatedElementReturns this element's annotation for the specified type if such an annotation is present, else null.- Specified by:
getAnnotationin interfaceAnnotatedElement- 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:
NullPointerException- if the given annotation class is null- Since:
- 1.5
-
isAnnotationPresent
public boolean isAnnotationPresent(Class<? extends 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) != nullThe body of the default method is specified to be the code above.
- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- 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:
NullPointerException- if the given annotation class is null- Since:
- 1.5
-
getAnnotationsByType
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
Description copied from interface:AnnotatedElementReturns 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 andAnnotatedElement.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:
getAnnotationsByTypein interfaceAnnotatedElement- 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:
NullPointerException- if the given annotation class is null- Since:
- 1.8
-
getAnnotations
public Annotation[] getAnnotations()
Description copied from interface:AnnotatedElementReturns 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:
getAnnotationsin interfaceAnnotatedElement- Returns:
- annotations present on this element
- Since:
- 1.5
-
getDeclaredAnnotation
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
Description copied from interface:AnnotatedElementReturns 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:
getDeclaredAnnotationin interfaceAnnotatedElement- 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:
NullPointerException- if the given annotation class is null- Since:
- 1.8
-
getDeclaredAnnotationsByType
public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)
Description copied from interface:AnnotatedElementReturns 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 andAnnotatedElement.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:
getDeclaredAnnotationsByTypein interfaceAnnotatedElement- 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:
NullPointerException- if the given annotation class is null- Since:
- 1.8
-
getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
Description copied from interface:AnnotatedElementReturns 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:
getDeclaredAnnotationsin interfaceAnnotatedElement- Returns:
- annotations directly present on this element
- Since:
- 1.5
-
-