<> annotation

There are colored eggs at the end of the article .

<> One , What is annotation ?

Annotaion

*
annotation (Annotaion) It's from JDK5.0 A new technology that began to be introduced is called annotation mechanism .

*
annotation (Annotaion) Format of :

* The annotation is based on "@ Comment name " Used in code , You can add some parameter values , for example :@GetMapping("/get")
*
annotation (Annotaion) Scope of use :

*
Can be in package,class,method,field Use it later . for example :

* @Controller public class RequestController { @DeleteMapping("/delete")
@ResponseBody public String delete(String name,Integer id){ JSONObject json =
new JSONObject(); json.put("requestType","deleteType"); json.put("name",name);
json.put("id",id); return json.toString(); } }
*
We can program access to these metadata through reflection mechanism .

*
Annotations have some specific functions , for example :

*
When you want to rewrite toString() Methods , If it's not written in the name of the rules , You'll report a mistake :

*
*
Normally , There is no mistake :

*

*
explain @Override Annotations have the function of checking .

<> Two , Built in annotations

Java A set of annotations is defined internally , share 7 individual :

Note name effect
@Override Check whether the method is an override method . If its parent class is found , Or when there is no such method in the referenced interface , Compilation errors will be reported .
@Deprecated Marking obsolete methods . If you use this method , Compile warning will be reported .
@SuppressWarnings Instructs the compiler to ignore warnings declared in annotations .
Notes acting on other annotations ( Meta annotation ): stay java.lang.annotaion In the bag

Note name effect
@Retention Identify how this annotation is saved , It's only in the code , Or incorporated class In the file , Or it can be accessed through reflection at runtime .
@Documented Mark whether these annotations are included in the user document .
@Target What kind of annotation should be marked Java member .
@Inherited Mark which annotation class this annotation inherits from ( default Annotations do not inherit from any subclasses )
from Java 7 start , Added extra 3 Notes :

Note name effect
@SafeVarargs Java 7 Start supporting , Ignore any warnings generated by method or constructor calls that use parameters as generic variables .
@FunctionalInterface Java 8 Start supporting , Identifies an anonymous function or functional interface .
@Repeatable Java 8 Start supporting , Identifies that an annotation can be used more than once on the same declaration .
<> Three , Use of meta annotations

<>( One ) preparation in advance

If you want to customize annotations , Then meta annotation is necessary to know and understand .

Meta annotations are annotations for custom annotations . Maybe it's a little bit lax , I'll see an example later , Straight white point is to give you custom annotations must add notes .

Notes acting on other annotations ( Meta annotation ): stay java.lang.annotaion In the bag

Note name effect
@Retention Identify how this annotation is saved , It's only in the code , Or incorporated class In the file , Or it can be accessed through reflection at runtime .
@Documented Mark whether these annotations are included in the user document .
@Target What kind of annotation should be marked Java member .
@Inherited Mark which annotation class this annotation inherits from ( default Annotations do not inherit from any subclasses )
We define a class , Class is used to test our defined annotations :
/** * @Auther: truedei * @Date: 2020 /2020/9/6 18:11 * @Description: Custom annotation test */
public class Test { }
Then create a class , hold class Identifier changed to @interface: This is the custom annotation .
/** * @Auther: truedei * @Date: 2020 /2020/9/6 19:00 * @Description: Custom annotation */
public @interface MyAnnotaion { }
Now you can see it and use it , It just doesn't have any functions :

<>( Two )@Target How to use

Let's give it some function , To identify the role of this annotation :

Add meta notes :@Target, It's used to indicate what works for , What is the goal of the role , Where can I use it .

As you can see, it's a E

You can see it @Target Take a look at the source code :

You can see the following .

What needs to be known in advance is ,value() Is the received parameter , It's not a method .

that Target You need to receive one ElementType[] Array of .
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.
ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
Before adding , Let's make a transformation first :

As you can see, this annotation doesn't add anything , It can be added to a class , It can also be added to the method , It can also be added to variables .

What if we want to limit it ? Only our annotation is allowed to work on classes

Let's take a look first Controller Annotation this should not be strange , I won't explain .

So we can also add this :

You can see the effect , As long as the , The rest were misreported .

So this is meta annotation Target The role of .

to glance at ElementType.java Parameters enumerated in , All of these can be used , Just get to know it .
package java.lang.annotation; public enum ElementType { TYPE, /*
class , Interface ( Include comment types ) Or enumeration declaration */ FIELD, /* Field declaration ( Include enumeration constants ) */ METHOD, /* Method statement */ PARAMETER, /*
Parameter declaration */ CONSTRUCTOR, /* Constructor declaration */ LOCAL_VARIABLE, /* Local variable declaration */ ANNOTATION_TYPE,
/* Annotation type declaration */ PACKAGE /* Package declaration */ /** * Type parameter declaration * * @since 1.8
*/ TYPE_PARAMETER, /** * Use of a type * * @since 1.8 */ TYPE_USE }
If you want your annotations to work for what , It's a good appointment OK:

<>( Three )@Retention How to use

Retention Need to pass RetentionPolicy.

RetentionPolicy.java There are three enumeration parameters , as follows :
package java.lang.annotation; public enum RetentionPolicy { SOURCE, /*
Annotation Information only exists during compiler processing , After the compiler has finished processing, there is no Annotation Information */ CLASS, /*
The compiler will Annotation Stored in the .class In the file . Default behavior */ RUNTIME /*
The compiler will Annotation Stored in class In the file , And can be JVM Read in */ }
It is usually called **RUNTIME**, call RUNTIME We can get the relevant data through reflection , To process, etc .

Because this is not easy to verify , We will not verify them one by one , Just understand .

<>( Four )@Documented How to use

If used @Documented Modify the Annotation, It can appear in the javadoc in .

definition Annotation Time ,@Documented not essential ;

If there is no definition , be Annotation Will not appear in javadoc in .

<>( Five )@Inherited How to use

Subclasses can inherit the annotations of the parent class .
/** * @Auther: truedei * @Date: 2020 /2020/9/6 19:00 * @Description: Custom annotation */
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD}) @Retention(
RetentionPolicy.RUNTIME) @Documented @Inherited public @interface MyAnnotaion {
}
<> Four , User defined parameter details

Parameters of annotation : The type of the parameter + Parameter name ();

If you want to have a default value, you need to add default value .

for example :
// Custom parameters 1 String name(); // Custom parameters 2 With default values String type() default "";
We add a parameter to the predefined annotation class , Just write your name :
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD}) @Retention(
RetentionPolicy.RUNTIME) @Documented @Inherited public @interface MyAnnotaion {
// parameter String name(); }
You can see when we use it , It's going to be a mistake , The reason is that a value must be passed to the defined parameter .

We pass the value :
@MyAnnotaion(name = " Zheng Hui ") public void test(){ }

At this time, there are students to ask : After I define the parameters , Can I pass no parameters , Pass it on when you use it .

The answer is yes , as follows : Just set a default value :
String type() default "";

Yes, of course , You can also pass many types of parameters, such as :
import java.lang.annotation.*; /** * @Auther: truedei * @Date: 2020 /2020/9/6
19:00 * @Description: Custom annotation */ @Target({ElementType.TYPE,ElementType.FIELD,
ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited
public @interface MyAnnotation { //String type String name(); //int type int age()
default 0; //boolean type boolean bool() default false; //char type char cha()
default ' '; // Various array types String[] strs() default {}; // Enumeration type MyEnum myEnum() default
MyEnum.A; }
Enumeration type class definition :
public enum MyEnum { A,B,C,D; }
When we use it , You can use it as you like :
/** * @Auther: truedei * @Date: 2020 /2020/9/6 18:11 * @Description: Custom annotation test */
public class Test { String data; @MyAnnotaion(name = " Zheng Hui ",age = 85,cha = 'A',
strs= {"aasd","xsw"," Hello "},myEnum = MyEnum.C) public void test(){ } }
<> Five , Using reflection to manipulate annotations

MyA.java:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
/** * For us : We are allowed to scan only if this annotation is added */ @Target({ElementType.TYPE}) @Retention(
RetentionPolicy.RUNTIME) public @interface MyA { }
MyAnnotation.java:
import java.lang.annotation.*; /** * @Auther: truedei * @Date: 2020 /2020/9/6
19:00 * @Description: Custom annotation ----> For specific methods */ @Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { //String type
Stringname(); }
Test.java:
/** * @Auther: truedei * @Date: 2020 /2020/9/6 18:11 * @Description: Custom annotation test */
@MyA public class Test { String data; @MyAnnotation(name = " Zheng Hui ") public void
test(String name){ System.out.println(" My name :"+name); } }
Test class :
package cn.annotaion; import java.lang.annotation.Annotation; import java.lang.
annotation.Target; import java.lang.annotation.ElementType; import java.lang.
annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.
lang.annotation.Inherited; import java.lang.reflect.Method; /** * Test class */ public
class AnnotationTest { public static void main(String[] args) throws Exception {
// Get the specified class Test Class cl = Class.forName("cn.annotaion.Test"); // Determine whether it is our specific custom annotation
If so, scan if (cl.isAnnotationPresent(MyA.class)) { Method[] methods = cl.getMethods
(); for (Method method : methods) { // judge somebody() Does the method contain MyAnnotation annotation if(
method.isAnnotationPresent(MyAnnotation.class)){ // Gets the MyAnnotation Annotation examples
MyAnnotation myAnnotation= method.getAnnotation(MyAnnotation.class); // obtain
myAnnotation Value of , And print it out String name = myAnnotation.name(); System.out.println(name)
; // Execute this method method.invoke(new Test(),name); } } } } }
function :

<> Six , Colored eggs

After watching it , Any problems found ?

Careful, you must notice : I used it initially MyAnnotaion.java later MyAnnotation.java

you 're right , It's short t. Fortunately, it doesn't affect the overall situation .

Technology
©2020 ioDraw All rights reserved
python gui Interface examples -Python GUI Programming complete example IO flow —— Classification of flow ,InputStream,OutputStream,Reader,Writer etc. Thread.setDaemon Set up the Daemons STM32 RS485 modbus Communication source program , The test is effective stay MATLAB 2018 Time series prediction with deep learning vue3 Write component library and publish to npm The process of Questions and answers for the interviewer : How did you get a project in the project team ??? In the applet form Multiple cycles input How to get each input Value of 2021 Front end high frequency interview questions ( You have to watch it !!!)js Array method