Passing custom annotation value in java

I am using custom annotation with aspectj.

@TestLoggingAnnotation(setMessage = "I want to set value here") public void get() { String retString = null; String message = "DEFAULT"; if (message == "DEFAULT") { retString = "Default Logging"; } else { retString = "Custom Logging"; } } 

The above is simple and rough. My requirement is that I want to pass the parameter value after the method.

In my case, I want to set retString to setMessage in a user parameter.

+6
source share
2 answers

Currently, only compilation constants can accept annotations and cannot be assigned values ​​at runtime, although their value can be used at runtime using @Retention.

discussion follows here

+1
source
 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface TestLoggingAnnotation{    String setMessage (); } 

now use reflection to extract and set the param method. I doubt that we can do this with local variables.

0
source

Source: https://habr.com/ru/post/955664/


All Articles