

void setAccessible(boolean flag) - sets the accessible flag for this reflected object to the given boolean value and throws InaccessibleObjectException if the access control cannot be suppressed.boolean canAccess(Object obj) - returns true if and only if the caller can access this reflected object.

Here are some methods that this class declares: Accessibility is established using the exports directive in the target module. Readability is established using the requires directive in the caller module.Īccessibility is the basis of strong encapsulation and specifies what packages (and public types in them) the target module exposes to the caller module. Readability is the basis of reliable configuration and specifies that the caller module can be guaranteed to read types in the target module. Java language access control is connected with two concepts of the Java Platform Module System: readability and accessibility. The AccessibleObject class is an abstract superclass of the Field, Method, and Constructor classes that allows suppressing checks for Java language access control.

Class getDeclaringClass() - returns the Class object representing the class or interface that declares the member or constructor.This interface declares the following methods: The Member interface is a superinterface for the Field, Method, and Constructor classes. Among other methods, this class has the following methods for discovering fields, methods, constructors, member classes and member interfaces of the given type:Īdditionally, there is the Parameter class that represents reflected parameters of methods and constructors. The entry point for the Core Reflection API is the Class class. Notice that constructors are not class members and therefore are not inherited. A class can declare members in its body or inherit them from its superclass and superinterfaces. Discovery members and constructorsĪccording to the Java Language Specification, class members include fields, methods, member classes and member interfaces. This article is based on the Java 17 implementation in Oracle OpenJDK. The Member interface and its implementations - the Field, Method, and Constructor classes represent reflected fields and methods (which are members of classes and interfaces, according to the Java Language Specification) and constructors (which are not members). The ability to modify behavior consists in the presence of this API for getting and setting field values, invoking methods, and creating new instances using constructors.

The ability to introspect structure consists in the presence of the Core Reflection API for reading classes and their fields, methods, constructors, member classes and member interfaces. Reflection is the ability of an application to examine and modify its structure and behavior at runtime. All the public fields up the entire class hierarchy.Īll the fields, regardless of their accessibility but only for the current class, not any base classes that the current class might be inheriting from.Java Reflection, part 2: fields, methods, constructors Introduction
