Java annotations--notes

The role of the @Override tag
@Override is pseudo-code, so it can be written or not. It means method overriding, and writing it will bring us benefits.
1. It can be used as a comment for easy reading.
2. Tell people reading your code that this is method overriding.
3. The compiler can verify for you whether the method name under @Override is all in your parent class, and report an error if not.

Java annotations, also known as annotations, are special syntax metadata that Java supports adding to source code since 1.5;

Classes, methods, variables, parameters, and packages in Java can all be annotated.

Annotations are only metadata and have nothing to do with business logic, so when you look at the annotation class, you find that there is no logic processing in it;

If the annotation does not contain business logic processing, someone must implement the logic. The logical implementation of annotations is handled by the user of the metadata .

An annotation only provides information about the properties (class/method/variable/parameter/package) it defines, and users of the annotation can read this information and implement the necessary logic .

When using annotations in java (like @Override, @Deprecated, @SuppressWarnings) the JVM is the user and it works at the bytecode level.

The function of the annotation
format check: tell the compiler information, such as if the method marked by @Override is not a method of the parent class, the IDE will report an error;

Reduce configuration: dynamically process at runtime, get annotation information, and realize the function of replacing configuration files;

Reduce repetitive work: For example, the third-party framework xUtils reduces the call to findViewById by annotating @ViewInject, and similar (JUnit, ActiveAndroid, etc.);

How to customize annotations

When using @interface to customize the annotation, the java.lang.annotation.Annotation interface is automatically inherited, and other details are automatically completed by the compiler.

When defining an annotation, other annotations or interfaces cannot be inherited. @interface is used to declare an annotation where each method actually declares a configuration parameter.

The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be basic types, Class, String, enum).

The default value of a parameter can be declared by default.

  • Custom annotation format:

      元注解
      public @interface 注解名{
          定义体;
      }
    
  • Supported data types for annotation parameters:

1. All basic data types (int, float, boolean, byte, double, char, long, short);

2. String type;

3. Class type;

4. enum type;

5. Annotation type;

6. Arrays of all the above types.

  • Example demonstration:

ToDo.java: Annotation class

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Todo {
    public enum Priority {LOW, MEDIUM, HIGH} public enum Status {STARTED, NOT_STARTED} String author() default "Yash"; Priority priority() default Priority.LOW; Status status() default Status.NOT_STARTED; } 

BusinessLogic: Using Annotated Classes

public class BusinessLogic {
    public BusinessLogic() { super(); } public void compltedMethod() { System.out.println("This method is complete"); } @Todo(priority = Todo.Priority.HIGH) public void notYetStartedMethod() { // No Code Written yet } @Todo(priority = Todo.Priority.MEDIUM, author = "Uday", status = Todo.Status.STARTED) public void incompleteMethod1() { //Some business logic is written //But its not complete yet } @Todo(priority = Todo.Priority.LOW, status = Todo.Status.STARTED ) public void incompleteMethod2() { //Some business logic is written //But its not complete yet } } 

TodoReport.java: Parse annotation information

public class TodoReport {
    public TodoReport() { super(); } public static void main(String[] args) { getTodoReportForBusinessLogic(); } /** * 解析使用注解的类,获取通过注解设置的属性 */ private static void getTodoReportForBusinessLogic() { Class businessLogicClass = BusinessLogic.class; for(Method method : businessLogicClass.getMethods()) { Todo todoAnnotation = (Todo)method.getAnnotation(Todo.class); if(todoAnnotation != null) { System.out.println(" Method Name : " + method.getName()); System.out.println(" Author : " + todoAnnotation.author()); System.out.println(" Priority : " + todoAnnotation.priority()); System.out.println(" Status : " + todoAnnotation.status()); System.out.println(" --------------------------- "); } } } } 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730921&siteId=291194637