The Reflection API
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
-
Introducing the Reflection API
Introducing the Reflection API: how to examine or modify the runtime behavior of applications running in the Java virtual machine.
-
Retrieving Classes
How to retrieve a class instance, from its name, or from and object, including arrays an primitive types.
-
Reading Class Names
What is the name of a class, and how you can read it, including arrays and primitive types.
-
Reading Modifiers
How to discover the modifiers of a class or of any class member: fields, methods, and constructors.
-
Reading and Writing Fields
A field has a type and an associated value for a given object. Methods in the Field class can retrieve information about the field, such as its name, type, modifiers, and annotations. There are also methods which enable dynamic access and modification of the value of the field.
-
Invoking Methods
A method contains executable code which may be invoked. Methods are inherited and in non-reflective code behaviors such as overloading, overriding, and hiding are enforced by the compiler. In contrast, reflective code makes it possible for method selection to be restricted to a specific class without considering its superclasses. Superclass methods may be accessed but it is possible to determine their declaring class; this is impossible to discover programmatically without reflection and is the source of many subtle bugs.
-
Invoking Constructors
A constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.
-
Working with Arrays
An array is an object of reference type which contains a fixed number of components of the same type; the length of an array is immutable. Creating an instance of an array requires knowledge of the length and component type. Each component may be a primitive type (such as byte, int, or double), a reference type (such as String, Object, or java.nio.CharBuffer), or an array. Multi-dimensional arrays are really just arrays which contain components of array type.
-
Working with Enumerations
An enum is a language construct that is used to define type-safe enumerations which can be used when a fixed set of named values is desired. All enums implicitly extend java.lang.Enum. Enums may contain one or more enum constants, which define unique instances of the enum type. An enum declaration defines an enum type which is very similar to a class in that it may have members such as fields, methods, and constructors (with some restrictions).
-
Working with Records
How to discover the modifiers of a class or of any class member.
-
Reading Annotations
How to discover annotations on elements of classes, and on types.
-
Creating an Interceptor with Annotations
This section takes you through the creation of a simple interceptor on a service method, using annotations and the Reflection API.
-
Creating a Dependency Injection Framework
This section takes you through the creation of a simple Dependency Injection framework, using annotations and the Reflection API.