gpt4 book ai didi

annotations - 如何通过注释获取参数的值?

转载 作者:行者123 更新时间:2023-12-05 01:00:28 25 4
gpt4 key购买 nike

我使用spring aop来拦截方法的调用。然后我定义了一个注解TestParam

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

我尝试将此注释添加到方法中的参数中。

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

我尝试拦截调用

@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
}
}
}

那如何获取被@TestParam注解的参数的值呢?
我想获取参数的值,而不是注解的值。

最佳答案

这里是 MCVE包含包名称、导入等。只需复制和粘贴。

标记注释:

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 {}

驱动程序应用:

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");
}
}

方面:

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();
}
}

控制台日志:

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

关于annotations - 如何通过注释获取参数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49585051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com