This article brings you relevant knowledge aboutjava, which mainly introduces issues related to annotations and meta-annotations, including a basic introduction to annotations, a basic introduction to Annotation, etc., Let’s take a look at it together, I hope it will be helpful to everyone.
Recommended study: "java Video Tutorial"
Annotation (annotation), also known as metadata (Metadata), is introduced in JDK1.5 and later versions and is used to modify and interpret data information such as packages, classes, methods, properties, constructors, and local variables. It can be used to create documentation, track dependencies in your code, and even perform basic compile-time checks.
Annotations exist in the code with '@annotation name'. According to the number of annotation parameters, we can divide annotations into three categories: mark annotations, single-value annotations, and complete annotations. kind. Like comments, annotations do not affect program logic, but they can be compiled or run and are equivalent to supplementary information embedded in the code.
In addition, you can choose at compile time whether the annotations in the code only exist at the source code level, or it can also appear in class files or runtime (SOURCE/CLASS/ RUNTIME).
In JavaSE, the purpose of using annotations is relatively simple, such as marking obsolete functions, ignoring warnings, etc. Annotations play a more important role in Java EE, such as being used to configure any aspect of the application, replacing the cumbersome code and XML configuration left in the old version of Java EE.
@Override: To limit a method is to override the parent class method,This annotation can only be used for methods
@Deprecated: Used to represent a certain program element(kind,Methods, etc.)Outdated
@SuppressWarnings: Suppress compiler warnings
@Override
class Son extends Father{ @Override public void play(){} }
Note:
@Override indicates that the play method of the subclass overrides the play method of the parent class
If there is nowritten here@OverrideIt will still override the parent class ply
If you write the @Override annotation, the compiler will check whether the method really overrides the parent classMethod, if it is indeed rewritten, the compilation will pass. If it is not rewritten, the compilation error will occur.
@Override can only modify methods, not other classes, packages, properties, etc.
@Deprecated
@Deprecated class A{ @Deprecated public static int name = 1; @Deprecated public void play(){ } }
Note:
It’s obsolete, it doesn’t mean it can’t be used, it’s just not recommended, but it can still be used
Can modify methods, classes, fields, Packages, parameters, etc.
Its function is to achieve compatibility and transition between old and new versions
@SuppressWarnings
@SuppressWarnings ("all") public class word{ }
注意:
关于 SuppressWarnings 作用范围是和你放置的位置相关。比如@SuppressWarnings 放置在 main 方法,那么抑制警告的范围就是 main
通过 @SuppressWarnings 的源码可知,其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。
##▶ What is meta-annotation:
Meta-annotations are annotations that explain annotations. The annotations they annotate are the annotations we introduced earlier, such as:@Override, @Deprecated,@SuppressWarnings
▶ Four meta-annotations:
Retention:Specify the scope of the annotation, three types of SOURCE, CLASS, and RUNTIME
Target:Specify where the annotation can be used
▶ Notes on annotations:
, the annotation will exist in the class bytecode file, but cannot be obtained at runtime,
The annotation will exist in the class bytecode file, and can be obtained through reflection at runtime
①: Generally, if you need to dynamically obtain annotation information atruntime, you can only use RUNTIME annotations; ## For some pre -processing operations, such as generating some auxiliary code (such as Butterknife), use class notes;
③: If you just do some
check -in SuppressWarnings, you can use the SOURCE annotation.
2. Retention annotation
Explanation:
can only be used to modify an Annotation definition,
AnnotationHow long it can be retained, @Rentention contains a member variable of type
RetentionPolicy, which must be thevalue when using@Rentention
##Member variables specify values (there are three types of values).RetentionPolicy.SOURCE:After the compiler uses it, it directly discards the comments of this policy.
RetentionPolicy.CLASS:The compiler will record the annotations in classIn the file.When runningJavaProgram Time, JVMAnnotations will not be retained. It's the default value.
RetentionPolicy.RUNTIME:The compiler will record the annotation in classIn the file.When runningJavaProgram Time, JVMAnnotationswill be retained.The program can obtain this annotation through reflection.
3. Target annotation
Explanation:
is used to modify the Annotation definition and specify which program elements the modified Annotation can be used to modify. @Target also contains a member variable named value.
4,Documented annotation
Explanation:
@Documented: Used to specify that the Annotation class modified by this meta-annotation will be extracted into a document by the javadoc tool, that is, the annotation can be seen when the document is generated.
Notice:
Annotations defined as @Documented must set the Retention value to RUNTIME.
5. Inherited annotation
Explanation:
Annotations modified by @Inherited will have inheritance. If a class uses annotations modified by @Inherited, its subclasses will automatically have inheritance This annotation
is recommended to study: "java video tutorial"
关键字 | 解释 |
---|---|
all | 抑制所有警告 |
boxing | 抑制与封装/拆装作业相关的警告 |
cast | 抑制与强制转型作业相关的警告 |
dep-ann | 抑制与淘汰注释相关的警告 |
deprecation | 抑制与淘汰的相关警告 |
fallthrough | 抑制与switch陈述式中遗漏break相关的警告 |
finally | 抑制与未传回finally区块相关的警告 |
hiding | 抑制与隐藏变数的区域变数相关的警告 |
incomplete-switch | 抑制与switch陈述式(enum case)中遗漏项目相关的警告 |
javadoc | 抑制与 javadoc 相关的警告 |
nls | 抑制与非nls字串文字相关的警告 |
null | 抑制与空值分析相关的警告 |
rawtypes | 抑制与使用raw类型相关的警告 |
resource | 抑制与使用Closeable类型的资源相关的警告 |
restriction | 抑制与使用不建议或禁止参照相关的警告 |
serial | 抑制与可序列化的类别遗漏 serialVersionUID 栏位相关的警告 |
static-access | 抑制与静态存取不正确相关的警告 |
static-method | Suppress warnings related to methods that may be declaredstatic |
super | Suppress warnings related to substitution methods that do not contain super calls |
synthetic-access | Suppress warnings related to non-optimized access to internal classes |
sync-override | Suppresses override sync methods Missed synchronization warning |
unchecked | Suppress with Warnings related to unchecked jobs |
unqualified-field-access | Suppress warnings related to failed field access |
##unused | Suppress warnings related to unused and deactivated code |
The above is the detailed content of Detailed explanation of java knowledge points: annotations and meta-annotations. For more information, please follow other related articles on the PHP Chinese website!