First, annotations
The
Annotation acts as an identity. such as JUnit's @test annotations. JUnit checks to see if this annotation exists on the method at run time, and if present, runs your method with reflection,
1, the role of the annotation: alternative configuration file
2, the three basic annotations provided in the JDK
@Override: Indicates whether the method of the parent class is overridden.
@Deprecated: The description is obsolete.
@SuppressWarnings: Suppress warning
@SuppressWarnings ("Specific type of suppressed warning"):
Unused: Variable not used
deprecation: using deprecated A warning for a class or method when
unchecked: An unchecked conversion was performed. For example, there is no generic
Falthrough: Warning when a switch block directly leads to the next situation without a break
path: A warning when there are no paths in the classpath, source file path, and so on.
Serial: Warning when SERIALVERSIONUID definition is missing for serializable Thunder
finally: a warning when any finally clause does not complete properly.
All: A warning about all of these conditions.
3, annotation learning goal:
A, custom annotations
B, reflection annotations
4, custom annotations syntax:
annotation itself: public @interface myannotation{}
Use keyword @ A interface defines a class. This class is an annotation.
Attributes in Annotations:
Basic form: Type attribute name ();
For example: String name ();
using: @MyAnnotationDemo1 (name= "Hello")
Special Note: The type of the annotation's properties can only be: base type, String, Class, enumeration, annotation type, and one-dimensional array of the above types.
Default value for annotation properties:
Type property name () default defaults;
Special attributes in Annotations:
Type value ();
When used, if you only set values for value, you can use the following: @MyAnnotationDemo1 (value= "Hello") or @myannotationdemo1 ("Hello")
When used, if you set a value only for value, and you want to set a value for another property, you can use: @MyAnnotationDemo1 (value= "Hello", name= "AAA")
Type [] Value ();
Use the same way as value.
Usage: Four kinds of all OK
@MyAnnotationDemo1 (value={"A", "B"})
@MyAnnotationDemo1 ({"A", "B"})
@MyAnnotationDemo1 ({"A"})
@MyAnnotationDemo1 ("a")
5, Yuan Annotation:
Definition: Annotations that serve annotations are meta annotations.
@Retention: The retention range of annotations
Retentionpolicy.source: Annotations exist in the source file
Retentionpolicy.class: Annotations exist in source bytecode files
Retentionpolicy.runtime: Annotations exist at run time
@Target: Where the annotation appears
@Documented: The Annotation class that is used to specify that the Annotation is decorated by the meta-javadoc tool is extracted as a document.
@Inherited: The Annotation that it modifies will have inheritance. If a class uses a Annotation that is @Inherited decorated, its subclasses automatically have that annotation
Annotations in Java