Javareflect 简明教程
java.lang.reflect - AccessibleObject Class
Introduction
java.lang.reflect.AccessibleObject 类是 Field、Method 和 Constructor 对象的基类。它提供了在使用反射对象时将其标记为抑制默认 Java 语言访问控制检查的能力。当 Fields、Methods 或 Constructors 用于设置或获取字段、调用方法或创建和初始化类的实例时,会执行 public、default(包)访问、protected 和 private 成员的访问检查。在反射对象中设置 accessible 标志允许具有足够权限的高级应用程序,例如 Java 对象序列化或其他持久机制,以通常禁止的方式操作对象。
The java.lang.reflect.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.
Class declaration
以下是 java.lang.reflect.AccessibleObject 类的声明 −
Following is the declaration for java.lang.reflect.AccessibleObject class −
public class AccessibleObject
extends Object
implements AnnotatedElement
Constructors
Sr.No. |
Constructor & Description |
1 |
*protected AccessibleObject()*Constructor: only used by the Java Virtual Machine. |
Class methods
Sr.No. |
Method & Description |
1 |
<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null. |
2 |
Annotation[] getAnnotations()Returns all annotations present on this element. |
3 |
Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element. |
4 |
boolean isAccessible()Get the value of the accessible flag for this object. |
5 |
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)Returns true if an annotation for the specified type is present on this element, else false. |
6 |
static void setAccessible(AccessibleObject[] array, boolean flag)Convenience method to set the accessible flag for an array of objects with a single security check (for efficiency). |
7 |
void setAccessible(boolean flag)Set the accessible flag for this object to the indicated boolean value. |