Shahid Ghafoor
Shahid Ghafoor

Reputation: 3103

passing value to custom annotation 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 just simple and sample code. My requirement is that I want to pass the parameter value after resulting from method.

In my case I want set retString value to setMessage in custom parameter.

Upvotes: 6

Views: 7618

Answers (2)

Pankaj Sharma
Pankaj Sharma

Reputation: 1853

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

now use reflection to extract and set method param i doubt we can do this with local varibles.

Upvotes: 0

codingenious
codingenious

Reputation: 8663

As of now, annotations can only take compile constants and cant be assigned values at runtime, though their value can be used at runtime using @Retention.

discussion follows here

Upvotes: 2

Related Questions