In Java, reflection is a powerful mechanism that allows a program to obtain internal information of any class at runtime and can directly manipulate internal properties and methods of any object.
Reading annotations using reflection is an important part of Java annotation applications.
The following will introduce in detail how to read annotations using Java reflection, and provide corresponding code examples and running results.
1. Basic steps for reflective reading annotations
- Definition annotation
- Use Notes
- Obtain annotation information through reflection
- Process annotation information
- Run the program and view the results
2. Code examples
Defining annotations First, we define a simple annotationMethodInfo
, information used to describe the method:
import ; import ; import ; import ; @Retention() @Target() public @interface MethodInfo { String author() default "unknown"; String date(); int version() default 1; }
Using annotations Next, we use it in a classMethodInfo
annotation:
public class ReflectionTest { @MethodInfo(author = "John Doe", date = "2022-01-01", version = 2) public void printMessage() { ("Hello, World!"); } }
Get annotation information through reflection. The following is a reading using reflection.MethodInfo
Annotation example:
import ; public class AnnotationReader { public static void readAnnotations() { try { // Get the Class object of the ReflectionTest class Class<?> clazz = ("ReflectionTest"); // Get all methods Method[] methods = (); // traversal method and find MethodInfo annotation for (Method method : methods) { if (()) { MethodInfo methodInfo = (); // Output annotation information ("Method Name: " + ()); ("Author: " + ()); ("Date: " + ()); ("Version: " + ()); ("----------------------"); } } } catch (ClassNotFoundException e) { (); } } }
Process the annotation information on the abovereadAnnotations
In the method, we have processed the annotation information, that is, print out the attribute value of the annotation.
Run the program and view the results
public class Main { public static void main(String[] args) { (); } }
Running results:
Method Name: printMessage
Author: John Doe
Date: 2022-01-01
Version: 2
----------------------
3. Detailed explanation
Annotation definition When defining annotations, we use@Retention()
, which means that the annotation will be preserved until runtime so that we can read it through reflection.@Target()
It means that this annotation can only be used on methods.
Use annotations inReflectionTest
In the class, weprintMessage
Method addedMethodInfo
Annotation and provide corresponding attribute values.
Reflection reading annotationsAnnotationReader
In the class, we first passGet
ReflectionTest
ClassicClass
Object. Then, we callgetDeclaredMethods
Method gets all methods declared in the class. By traversing these methods, we useisAnnotationPresent
Method Check whether the method is usedMethodInfo
annotation. If used, we passgetAnnotation
The method takes an annotation instance and reads its attribute value.
Processing annotation information After reading the annotation information, we can process it according to actual needs. In this example, we simply print out the annotation information.
Summarize
Through the above steps, we have detailed how to use reflection to read annotations in Java.
The reflection mechanism provides the possibility for us to analyze and operate annotations at runtime, which is widely used in many frameworks and tools.
For example, the Spring framework uses reflective reading annotations to implement dependency injection, transaction management and other functions.
In actual development, we can customize more powerful annotations according to our needs and process these annotations through reflection mechanisms, thereby achieving decoupling and scaling of the code.
Mastering the skills of reflective reading annotations is of great significance to improving Java programming level. Due to space limitations, this article provides only one simple example.
In actual projects, the application of annotation and reflection will be more complex and in-depth.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.