java annotation

chenitu 2022-06-24 08:01:11 阅读数:793


java Classification of annotations : Standard notes 、 Yuan notes 、 Custom annotation

1、Java Own standard notes

Include @Override、@Deprecated、@SuppressWarnings etc. , After using these annotations, the compiler checks .

JDK The following comments are built in :

  • Override    Check whether the method overrides the method , If the parent class is found , Or when there is no such method in the reference interface , Compile error will be reported .
  • @Deprecated Mark obsolete methods , If this method is used , Compile warning will be reported
  • @SuppressWarnings Instructs the compiler to ignore warnings declared in annotations
  • @FunctionnalInterface java8 Support , Identify an anonymous function or functional interface

2、 Yuan notes

Meta annotations are annotations used to define annotations , Include @Retention、@Target、@Inherited、@Documented、@Repeatable etc. . Meta annotation is also Java Own standard notes , Just used to decorate other annotations .

Meta annotations are java API Provided , Is a comment used to modify an annotation , Usually used in the definition of annotation :

  • @ Retention Used to define the level at which the annotation is available , In the source code (SOURCE)、 Class file (CLASS) Or runtime (RUNTIME).
  • @Documented: Keep annotations when generating document information , Auxiliary description of class

  • @Target  Illustrates the Annotation The scope of the object being decorated :Annotation Can be used for packages、types( class 、 Interface 、 enumeration 、Annotation type )、 Type members ( Method 、 Construction method 、 Member variables 、 Enumerated values )、 Method parameters and local variables ( Like loop variables 、catch Parameters ). stay Annotation Used in the declaration of type target Can be more clear about its embellishment objectives .

3、 Custom annotation

Users can define annotations according to their own needs . When we understand the built-in annotation , After meta annotation and reflection interface to get annotation , We can start customizing annotations . Creating custom annotations is similar to creating an interface , But annotated interface Keywords need to be @ Symbol at the beginning , We can declare methods for annotations .

copyright:author[chenitu],Please bring the original link to reprint, thank you.