Home > Java > JavaBase > body text

What is the implementation principle of java annotations

hzc
Release: 2020-06-13 14:39:41
Original
13567 people have browsed it

What is the implementation principle of java annotations

Annotation implementation principle

1. What are annotations?


Many first-time developers should have this question? Annotation is a new feature introduced in Java5, and its Chinese name is annotation. It provides a safe annotation-like mechanism for associating any information or metadata with program elements (classes, methods, member variables, etc.). Add more intuitive and clear descriptions to the elements of the program (classes, methods, member variables). These descriptions have nothing to do with the business logic of the program and are used by designated tools or frameworks. Annotation is like a modifier, applied to the declaration statements of packages, types, constructors, methods, member variables, parameters and local variables.

Java annotations are some meta-information attached to the code, which are used by some tools to parse and use during compilation and runtime, and serve the function of explanation and configuration. Annotations will not and cannot affect the actual logic of the code, they only play a supporting role. Contained in the java.lang.annotation package.

2. The use of annotations:


1. Generate documents. This is the most common and the earliest annotation provided by java. Commonly used ones include @param @return, etc.

2. Track code dependencies and implement alternative configuration file functions. For example, Dagger 2 dependency injection, in future java development, will be configured with a large number of annotations, which is of great use;
3. Perform format checking at compile time. For example, if @override is placed before a method, if your method does not override the superclass method, it can be checked at compile time.

3. The principle of annotation:


The essence of annotation is a special interface that inherits Annotation, and its specific implementation class is a dynamic proxy generated by Java runtime. kind. When we obtain annotations through reflection, what is returned is the dynamic proxy object $Proxy1 generated by the Java runtime. Calling a custom annotation (interface) method through a proxy object will eventually call the invoke method of AnnotationInvocationHandler. This method will index the corresponding value from the memberValues ​​Map. The source of memberValues ​​is the Java constant pool.

Recommended tutorial: "java tutorial"

The above is the detailed content of What is the implementation principle of java annotations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!