6

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.

Srinivas
  • 1,780
  • 1
  • 14
  • 27
Shahid Ghafoor
  • 2,991
  • 17
  • 68
  • 123
  • 4
    afaik, custom annotation methods cannot have parameters. But I'd love to be wrong. – CPerkins Oct 10 '13 at 12:21
  • @CPerkins `@TestLoggingAnnotation(setMessage = "I want to set value here")` you can call an thing to `setMessage = "I want to set value here".` I want to set value of setMessage. now it is ok ? – Shahid Ghafoor Oct 10 '13 at 12:25
  • there is another reply on stackoverflow for this issue http://stackoverflow.com/questions/14268981/modify-a-class-definitions-annotation-string-parameter-at-runtime – RamonBoza Oct 10 '13 at 12:34
  • I'm writing it as a comment in case I'm wrong - but annotation are not exactly a part of your run-time code. As their name states, they only annotate. They come to mark something that was set in compile time on a class or field. You can't use variables in annotations because the data is decided on in compile time. – Avi Oct 10 '13 at 12:34

2 Answers2

2

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

Community
  • 1
  • 1
codingenious
  • 8,385
  • 12
  • 60
  • 90
0
@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.

Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22