PurpleCraw
PurpleCraw

Reputation: 303

How get the value of the parameter with annotation?

I use spring aop to intercept the invocation of method. Then I defined an annotation TestParam

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestParam{}

And I try to add this annotation to a parameter in method.

public class Test {
    public void test(String abc, @TestParam String def) {
    }
}

I try to intercept the invocation

@Around
public Object intercept(ProceedingJoinPoint proceedingJoinPoint) {
    Signature signature = proceedingJoinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature)signature;
    Method method = methodSignature.getMethod();
    Parameter[] parameters = method.getParameters();
    for (Parameter parameter : parameters) {
        Annotation annotation = parameter.getAnnotation(TestParam.class);
        if (annotation != null) {
            // how can I can the value of this parameter
        }
    }
}

Then how can I get the value of the parameter who is annotationned with @TestParam?
I want to get the parameter's value, not the value of annotation.

Upvotes: 4

Views: 3359

Answers (1)

kriegaex
kriegaex

Reputation: 67317

Here is an MCVE with package names, imports etc. Just copy & paste.

Marker annotation:

package de.scrum_master.app;

import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Target(PARAMETER)
@Retention(RUNTIME)
public @interface TestParam {}

Driver application:

package de.scrum_master.app;

public class Test {
  public void test(String abc, @TestParam String def) {}
  public void toast(@TestParam String def) {}
  public void doSomething(String abc, String def) {}

  public int doSomethingElse(@TestParam int number, String abc, @TestParam String def) {
    return number * 2;
  }

  public static void main(String[] args) {
    Test test = new Test();
    test.test("foo", "bar");
    test.toast("cheers");
    test.doSomething("foo", "bar");
    test.doSomethingElse(11, "bar", "zot");
  }
}

Aspect:

package de.scrum_master.aspect;

import java.lang.annotation.Annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

import de.scrum_master.app.TestParam;

@Aspect
public class MyAspect {
  @Around("execution(public * *(.., @de.scrum_master.app.TestParam (*), ..))")
  public Object doAwesomeStuff(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    System.out.println(thisJoinPoint);
    Object[] methodArgs = thisJoinPoint.getArgs();
    int numArgs = methodArgs.length;
    MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
    Annotation[][] annotationMatrix = methodSignature.getMethod().getParameterAnnotations();
    for (int i = 0; i < numArgs; i++) {
      Annotation[] annotations = annotationMatrix[i];
      for (Annotation annotation : annotations) {
        if (annotation.annotationType() == TestParam.class) {
          //System.out.println("  annotation = " + annotation);
          System.out.println("  annotated parameter value = " + methodArgs[i]);
        }
      }
    }
    return thisJoinPoint.proceed();
  }
}

Console log:

execution(void de.scrum_master.app.Test.test(String, String))
  annotated parameter value = bar
execution(void de.scrum_master.app.Test.toast(String))
  annotated parameter value = cheers
execution(int de.scrum_master.app.Test.doSomethingElse(int, String, String))
  annotated parameter value = 11
  annotated parameter value = zot

Upvotes: 11

Related Questions