Let's start with an example
class Father { public void f() {} } public class Son extends Father{ @Override public void f() {} }
- When the subclass overrides the method of the parent class, an @ Override will appear above, which is an annotation
@Controller public class StudentController {
- The @ Controller in the framework is also an annotation
What is annotation
public interface Annotation { boolean equals(Object obj); int hashCode(); String toString(); Class<? extends Annotation> annotationType(); }
- Annotation is an interface
- In a way, annotations act like modifiers (public, final, static)
- The program can obtain the annotation of the specified program element through reflection, and then obtain the metadata in the annotation through annotation.
Types of annotations
- JDK built-in system annotation
- Meta annotation for 'embellishment' annotation
- Custom annotation
JDK built-in system annotation
@Override
- As shown in the above example, this annotation is used to modify methods that override the parent class
@Deprecated
- This annotation is used to modify outdated methods
- You can see that when the method is decorated with @ Deprecated and then used, the compiler will remind that the method is outdated
@SuppressWarnnings
- Used to ignore compiler warnings and tell the compiler to stop warnings for this method.
Meta annotation
@Target
- Function: where can the described annotation be used
- Parameter value
ElementType | meaning |
---|---|
ElementType | meaning |
ANNOTATION_TYPE | Annotation type declaration |
CONSTRUCTOR | Construction method declaration |
FIELD | Field declaration (including enumeration constants) |
LOCAL_VARIABLE | Local variable declaration |
METHOD | Method declaration |
PACKAGE | Package declaration |
PARAMETER | Parameter declaration |
TYPE | Class, interface (including annotation type) or enumeration declaration |
import java.lang.annotation.Target; import java.lang.annotation.ElementType; @Target(ElementType.FIELD) public @interface MyAnnotation { }
- The above is a custom annotation, decorated with a @ Target, indicating that the custom annotation can be used to modify the field
@Retention
- Purpose: indicates how long notes of note type will be retained
- Parameter value
RetentionPoicy | significance |
---|---|
SOURCE | Reserved in the source file, such as @ Override, for interaction with the compiler |
CLASS | source, the Class file is reserved for generating additional files during compilation |
RUNTIME | sorce, class file, reserved at runtime |
@Documented
- Function: indicates that a certain type of annotation will be documented through javadoc and similar default tools
- Annotations modified by @ Documented will be generated into javadoc
import java.lang.annotation.Documented; @Documented public @interface Demo { }
- Open cmd and execute javadoc demo Java, and then you can see that the annotation is generated into javadoc
@Inherited
- Action: indicates that the annotation type is automatically Inherited. You can let the subclass object use getAnnotations() to get the annotation of the parent class @ Inherited modifier.
@Inherited @Retention( RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { String msg() default "jiajun"; } @MyAnnotation class Father { } class son extends Father{ } public class Demo6 { public static void main(String[] args) { Father f=new son(); System.out.println(Arrays.toString(f.getClass().getAnnotations())); } } //Output: [@ MyAnnotation(msg=jiajun)]
Custom annotation
format
- public @interface annotation name {definition body}
@Retention source code
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { /** * Returns the retention policy. * @return the retention policy */ RetentionPolicy value(); }
- You can see that the return value of the annotation method is the RetentionPolicy enumeration type
use
- Use @ interface (different from interface) to declare an annotation
- When using @ interface to define annotations, the Annotation interface is automatically inherited
- Modifying custom annotations with meta annotations
- Some parameter members can be defined in the definition body. Default sets the default parameter value
- When annotation is used, the value is transferred in the form of (msg = ""). When the parameter name is value, the value can be directly transferred in the form of ("666").
- When no value is passed, @ MyAnnotation(), the parameter value obtained is the default value
experiment
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { String msg() default "jiajun"; } @MyAnnotation(msg="666") class Test1 {} public class Test2 { public static void main(String[] args) { Test1 t=new Test1(); Class c=t.getClass(); MyAnnotation ma = (MyAnnotation) c.getAnnotation(MyAnnotation.class); System.out.println(ma.msg()); } }
- A MyAnnotation annotation is customized, the annotation life cycle is declared with the Retention annotation, and the modification scope of the annotation is modified with the Target
- Test1 Class is decorated with user-defined annotations, and relevant information is obtained through Class. When Rention does not modify RUNTIME, relevant information cannot be obtained
Access annotation
Access class annotation
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { String name() default "jiajun"; } @MyAnnotation(name="jiajun") public class Test { } public static void main(String[] args) { Class clazz = Test.class; Annotation[] annotations = clazz.getAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); } } }
Access method annotation
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String name() default "jiajun"; } public class Test { @MyAnnotation(name="jiajun") public void doSomething(){} } public class Demo { public static void main(String[] args) { Class clazz=Test.class; Method[] methods=clazz.getMethods(); for(Method method :methods) { if(method.getName()=="doSomething") { Annotation annotation = method.getAnnotation(MyAnnotation.class); if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); } } } } }
Access parameter annotation
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface MyAnnotation { String name() default "jiajun"; } public class Test { public static void doSomething( @MyAnnotation(name="jiajun") String parameter){ } } public class Demo { public static void main(String[] args) { Class clazz=Test.class; Method[] methods=clazz.getMethods(); for(Method method :methods) { if(method.getName()=="doSomething") { Annotation[][] parameterAnnotations = method.getParameterAnnotations(); Class[] parameterTypes = method.getParameterTypes(); int i=0; for(Annotation[] annotations : parameterAnnotations){ Class parameterType = parameterTypes[i++]; for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("param: " + parameterType.getName()); System.out.println("name : " + myAnnotation.name()); } } } } } } }
- Each method has n parameters, and each parameter contains an annotation array, so getParameterAnnotations() returns a two-dimensional array
I'm a lazy guy!
I'm a lazy guy
I'm a lazy guy
I'm a lazy guy
I'm a lazy guy
Author: jiajun source: http://www.cnblogs.com/-new/
The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original connection must be given in an obvious position on the article page, otherwise the right to investigate legal responsibility is reserved. If you think it's still helpful, you can click [recommend] in the lower right corner, hoping to continuously bring you good technical articles! Want to make progress with me? Then pay attention to me.