0

Hi i try to create new annotation with action:

My annotation

 @Retention(RetentionPolicy.RUNTIME)
public @interface myCustomAnnotations{
    String errorMessage();
}

My fucntion

  @myCustomAnnotations(errorMessage = "error message")
    public void sendIssue() {
    }

now i want to log the error message if someone call this method

I search for ever for solution, so if you can help me, it will be awesome!!!

Omri Lugasi
  • 337
  • 3
  • 16

2 Answers2

0

If you don't want to use any existed solutions for logging you can take a look at AspectJ. With AspectJ you will be able to write an aspect which will be called after, before or around methods specified by your annotation. There are a lot of details, so it's better to see here for example http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html

dmitrievanthony
  • 1,501
  • 1
  • 15
  • 41
0

This won't be done simply by declaring custom annotation on methods. Annotations won't trigger automatically. If it were normal annotations then it could be triggered/executed by Annotation Processor at compile time. In your case, as you're using @Retention(RetentionPolicy.RUNTIME) that means annotation needs to be parsed at runtime manually. Have a good read on this answer. Using the reflection, this is the way you can log your message.

Community
  • 1
  • 1
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25