Java Annotations

Java Annotations

"Annotations are only metadata and do not contain any business logic"

"Annotations only provide information about the attribute (class/method/package/field) on which it is defined. The consumer is a piece of code that reads this information and then performs the necessary logic."

  1. @Override annotation in above code. Even if I don’t put @Override, code works properly without any issue.

  2. @Override tells the compiler that this method is an overridden method (metadata about the method), and if any such method does not exist in a parent class, then throw a compiler error (method does not override a method from its super class).

  3. if I would have made a typography mistake and used the method name as toStrring() {double r}, and if I wouldn’t have used @Override, my code would have compiled and executed successfully

  4. 注解需要三要素:定义、使用、读取并执行

    • Target

    • Retention - RetentionPolicy.RUNTIME

package AnnotationsSample;

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, first = "good william")
    public void incompleteMethod2() {
        //Some business logic is written
        //But its not complete yet
    }
    
    public static void main(String[] args) {
        
        System.out.println(BusinessLogic.class.getAnnotation(Todo.class).first());
    }
}
package AnnotationsSample;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Todo {
    String first() default "tianya";
    public enum Priority {LOW, MEDIUM, HIGH}package AnnotationsSample;
    public enum Status {STARTED, NOT_STARTED}    
    String author() default "Yash";
    Priority priority() default Priority.LOW;
    Status status() default Status.NOT_STARTED;
}
import java.lang.reflect.Method;

public class TodoReport {
    public TodoReport() {
        super();
    }

    public static void main(String[] args) {
        getTodoReportForBusinessLogic();
    }

    /**
     * This method iterates through all messages of BusinessLogic class and fetches annotations defined on each of them.
     * After that it displays the information from annotation accordingly.
     */
    private static void getTodoReportForBusinessLogic() {
        
        Class businessLogicClass = BusinessLogic.class;
        for(Method method : businessLogicClass.getMethods()) {
            Todo todoAnnotation = 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(" Status : " + todoAnnotation.first());
                System.out.println(" --------------------------- ");
            }
        }

    }

Reference

  1. How Do Annotations Work in Java?

  2. Java annotation

  3. 注解(上)

  4. 注解(下)

标签: none

添加新评论