Package java.lang.reflect
Class AccessibleObject
java.lang.Object
java.lang.reflect.AccessibleObject
- All Implemented Interfaces:
AnnotatedElement
- Direct Known Subclasses:
Constructor,Field,Method
public class AccessibleObject extends Object implements AnnotatedElement
AccessibleObject is the superclass of all member reflection classes
(Field, Constructor, Method). AccessibleObject provides the ability to toggle
a flag controlling access checks for these objects. By default, accessing a
member (for example, setting a field or invoking a method) checks the
validity of the access (for example, invoking a private method from outside
the defining class is prohibited) and throws IllegalAccessException if the
operation is not permitted. If the accessible flag is set to true, these
checks are omitted. This allows privileged code, such as Java object
serialization, object inspectors, and debuggers to have complete access to
objects.- See Also:
Field,Constructor,Method
-
Constructor Summary
Constructors Modifier Constructor Description protectedAccessibleObject()Constructs a newAccessibleObjectinstance. -
Method Summary
Modifier and Type Method Description <T extends Annotation>
TgetAnnotation(Class<T> annotationType)Returns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).Annotation[]getAnnotations()Returns, for this element, an array containing all annotations (including inherited annotations).Annotation[]getDeclaredAnnotations()Returns, for this element, all annotations that are explicitly declared (not inherited).protected Annotation[]getDeclaredAnnotations(boolean copy)booleanisAccessible()Indicates whether this object is accessible without access checks being performed.booleanisAnnotationPresent(Class<? extends Annotation> annotationType)Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).voidsetAccessible(boolean flag)Attempts to set the value of the accessible flag.static voidsetAccessible(AccessibleObject[] objects, boolean flag)Attempts to set the value of the accessible flag for all the objects in the array provided.
-
Constructor Details
-
AccessibleObject
protected AccessibleObject()Constructs a newAccessibleObjectinstance.AccessibleObjectinstances can only be constructed by the virtual machine.
-
-
Method Details
-
setAccessible
Attempts to set the value of the accessible flag for all the objects in the array provided. Setting this flag tofalsewill enable access checks, setting totruewill disable them.- Parameters:
objects- the accessible objectsflag- the new value for the accessible flag- See Also:
setAccessible(boolean)
-
isAccessible
public boolean isAccessible()Indicates whether this object is accessible without access checks being performed. Returns the accessible flag.- Returns:
trueif this object is accessible without access checks,falseotherwise
-
setAccessible
public void setAccessible(boolean flag)Attempts to set the value of the accessible flag. Setting this flag tofalsewill enable access checks, setting totruewill disable them.- Parameters:
flag- the new value for the accessible flag
-
isAnnotationPresent
Description copied from interface:AnnotatedElementIndicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- Parameters:
annotationType- the type of the annotation to search for- Returns:
trueif the annotation exists,falseotherwise
-
getDeclaredAnnotations
Description copied from interface:AnnotatedElementReturns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Returns:
- an array of annotations declared for this element
-
getDeclaredAnnotations
-
getAnnotations
Description copied from interface:AnnotatedElementReturns, for this element, an array containing all annotations (including inherited annotations). If there are no annotations present, this method returns a zero length array.- Specified by:
getAnnotationsin interfaceAnnotatedElement- Returns:
- an array of all annotations for this element
-
getAnnotation
Description copied from interface:AnnotatedElementReturns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).- Specified by:
getAnnotationin interfaceAnnotatedElement- Parameters:
annotationType- the type of the annotation to search for- Returns:
- the annotation with the specified type or
null
-