The annotations commonly used in the 1.java.lang package are @override, @Deprected (deprecated), @SupressWarning (block out some warnings. We can customize the annotations.
The @retention of 2.Java annotations, @Documented, @Inherited.
- retention annotations, which retain annotations, indicate the stage at which this type of pig will be retained, with three values: Retentionpolicy.source (only the source level is preserved, compile-time is ignored), Retentionpolicy.class ( class file, the JVM is ignored), Retentionpolicy.runtime (JVM reserved, so they can be read and used by the JVM or other code that uses the reflection mechanism at runtime).
- Document annotations, the purpose is to display this annotation information on the JAVAAPI document, if not this annotation, the API document does not display annotation information. This annotation should be recorded by the Javadoc tool, which does not include annotations by default, and if annotated, is handled by tools such as Javadoc, and information about the type of annotations is also included in the generated document.
- inherited annotations, the child class's parent class is marked as @inherited, and the subclass automatically inherits all the properties of the parent class, in addition to implementing the parent-class interface method, the method in the object class and the Annotationtype ().
The main function of annotations in 3.java is to make development easier, and development and deployment tools can read and process them in some way. Programs that contain annotations can replace additional Java source files, XML files, or error detection mechanisms.
The purpose of the annotations in 4.Java is to spend less time in the dead-end details, focusing more on business logic. The use of annotations can significantly reduce the amount of code, so that the structure of the code is clear.
Reflection mechanism in 5.Java: Java is considered a key property of the dynamic (or quasi-dynamic) language, which allows the program to get the class internal information of any known name by running the Reflectionapis given by the JDK. including its modifier (such as: public,static, etc.), superclass (for example: Object), interface (for example: cloneable), and fileds and methods all information, And can be run to change fields and methods. Java can load a runtime to know the name of the class, get its full structure.
6. Function of the reflection mechanism: Get class object, get the class's fields, get the method of the class, get the constructor of the class, and the instance function of the new class (Class<t> function Newinstance).
The ElementType inside the 7.Target is used to specify which elements the annotation type can be used on. For example: Type, FIELD (attribute), method (methods), PARAMETER (parameter), CONSTRUCTOR (constructor), local_variable (local variable), package (bundle). Where type types are referred to as class,interface,enum,annotatin types.
Let's take a look at the viewmapping annotations in Android:
From what we can see, on this note there is a retention phase that defines the annotations in the JVM. Can be used on the types and attributes above.
viewmapping Annotations in Android