0

This is the first time I am trying to write a custom annotations in java.

I am not sure whether it is possible or not but wanted to give it a try before approaching another solution.

So here is the scenario, I have a lots of method that sends the data out from the application to a device. I have a requirement to log all these data in database.

I would like to create an annotation for this so that I can write the code in the annotation to log the data in database and then annotation all the methods with this annotation.

I can modify the code to log into the database but in that case I have to go in each method and place my code at correct place inorder to log them into database.

This is the reason I am looking for annotation based approach.

Is it possible what I am looking for or am I asking more.

Any pointers will be appreciated or If someone has different approach for my solution that will be really help full.

NullPointerException
  • 3,732
  • 5
  • 28
  • 62

2 Answers2

1

Instead of writing your own Annotations and processing them, have a look at what Spring provides, e.g. Interceptors:

Interceptors vs Aspects in Spring?

Community
  • 1
  • 1
Puce
  • 37,247
  • 13
  • 80
  • 152
1

You can try below approach

package annotation;

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 {
    public enum Priority {LOW, MEDIUM, HIGH}
    String logInfo() default "Logging...";
    Priority priority() default Priority.LOW;
}


package annotation;

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, logInfo = "Inside DAO")
    public void incompleteMethod1() {
        //Some business logic is written
        //But its not complete yet
    }

    @Todo(priority = Todo.Priority.LOW)
    public void incompleteMethod2() {
        //Some business logic is written
        //But its not complete yet
    }
}


package annotation;
import java.lang.reflect.Method;

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

    public static void main(String[] args) {
        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.logInfo());
                System.out.println(" Priority : " + todoAnnotation.priority());
                System.out.println(" --------------------------- ");
            }
        }
    }
}
NPE
  • 429
  • 2
  • 16
  • would you be able to answer on this - https://stackoverflow.com/questions/67573532/writing-custom-annotations-in-java-timecounter-annotation-example – joven May 17 '21 at 16:25