1.2 built in notes
- @Override: defined in Java In lang. override, this annotation is only applicable to rhetorical methods, indicating that one method declaration intends to override another method declaration of superclass China.
- @Deprecated: defined in Java In lang. deprecated, this annotation can be used for rhetorical devices, attributes, and classes, indicating that programmers are not encouraged to use such elements. Usually, outdated and useless elements will be marked with @ deprecated.
- @SuppressWarnings: defined in Java Lang. SuppressWarnings, used to suppress warnings at compile time.
@SuppressWarnings is different from the first two annotations. You need to add a parameter to use it correctly. These parameters have been defined. We just need to use it selectively.
- @SuppressWarnings("all")
- @SuppressWarnings("unchecked")
- @SuppressWarnings(value = {"unchecked", "depreciation"}) / / transfer of multiple parameters.
- Wait
For example, when there is no write inhibit warning:
After the suppression warning is written, the warning will continue!!
- SuppressWarnings source code simple explanation
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package java.lang; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE, ElementType.MODULE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { String[] value(); }
@Target is the target type targeted by this annotation! (this is a meta annotation)
String[] value(); Is the type and parameter of the parameter.
1.3 yuan notes
- The function of meta annotation is to annotate other annotations! Java defines four standard meta annotation types, which are used to provide descriptions of other annotation types.
- These types and the classes they support are in Java Lang.annotation package (@ Target, @ Retention, @ Documented, @ Inherited)
-
@Target: used to describe the scope of use of annotations (i.e. where the described annotations can be used and what are the objectives of the annotations?)
-
@Retention: indicates the level at which the annotation information needs to be saved. It is used to describe the annotation life cycle (source < class < runtime)
SOURCE: the annotation is valid only for the current SOURCE file. It will not be valid after being compiled into a CLASS file.
CLASS: it is still valid after being compiled into a CLASS file! Compiling into a dex file discards this annotation. (that is, the annotation is also valid at SOURCE!!)
RUNTIME: annotations are still valid at RUNTIME. (that is, annotations are valid in CLASS and SOURCE!!) -
@Document: note that the annotation will be included in JavaDoc
-
@Inherited: indicates that the subclass can inherit the annotation in the parent class. That is, if inheritance occurs, there is an annotation in the parent class, and this annotation is also described by inherited, then the child class will certainly inherit this annotation!!!
1.4 user defined annotation
- You can customize annotations by using @ interface. The principle is (it automatically becomes the implementation of java.lang.annotation.Annotation interface, so you can customize annotations!)
- analysis:
-
@Interface is used to declare an annotation. The format is public @interface annotation name {definition content}
-
What looks like a method is actually a declared configuration parameter.
-
Parameter format: type parameter name ();
-
The type of the parameter (can only be the basic type Class, String, enum... Etc.).
-
You can declare the default value of the parameter through default
Type parameter name (default) default value; -
If there is only one parameter member, the general parameter name is value. In this way, when we use this annotation, the parameter name can not be written by default, and only the parameter value needs to be written.
-
Annotation parameters must have values. When defining annotation elements, we often use an empty string with 0 as the default value.
- Parameter has default value
package www.muquanyu.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Meta annotation exercise { @MyAnnotation public static void main(String[] args) { } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{ String name() default "";//String name parameter with default value '' int age() default 0;//int age parameter with default value 0 }
- There is no default value
package www.muquanyu.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Meta annotation exercise { @MyAnnotation public static void main(String[] args) { } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{ String name() default "";//String name parameter with default value '' int age() default 0;//int age parameter with default value 0 int num();//If there is no default value, you must write the parameter value }
Only by writing the value of the parameter can it let you go...
- When there is only one parameter, value is not used as the parameter name
package www.muquanyu.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Meta annotation exercise { @MyAnnotation("sss") public static void main(String[] args) { } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{ String name() default "";//String name parameter with default value '' }
It will prompt you that when a parameter is, your parameter name must be value to write the parameter value directly like this. Otherwise, only parameter name = parameter value.
- When a parameter, the parameter name is value
package www.muquanyu.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Meta annotation exercise { @MyAnnotation("sss") public static void main(String[] args) { } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{ String value() default "";//String name parameter with default value '' }
Annotation parameters are essentially methods, so it may prompt you about methods rather than annotation parameters. You just need to know that this thing is an annotation parameter. Instead of method (), don't confuse it!