gpt4 book ai didi

java - 如何将注释类型作为方法参数传递?

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:42 28 4
gpt4 key购买 nike

有一种处理注释的方法,例如

public void getAnnotationValue(ProceedingJoinPoint joinPoint)
{
MethodSignature methodSignature = (MethodSignature) joinPoint.getStaticPart().getSignature();
Method method = methodSignature.getMethod();
Annotation annotation = method.getParameterAnnotations()[0][0];
RequestHeader requestParam = (RequestHeader) annotation;
System.out.println(requestParam.value());
}

我想将其转换为接受 joinPoint 和注释类型的通用方法,例如

getAnnotationValue(joinPoint, RequestHeader);

我尝试使用:

public void getAnnotationValue(ProceedingJoinPoint joinPoint, Class<? extends Annotation> annotationType)
{
MethodSignature methodSignature = (MethodSignature) joinPoint.getStaticPart().getSignature();
Method method = methodSignature.getMethod();
Annotation annotation = method.getParameterAnnotations()[0][0];
annotationType requestParam = (annotationType) annotation;
System.out.println(requestParam.value());
}

但它提示错误,指出type unresolved error?如何处理它并将注释值传递到该函数!!

最佳答案

你能做的“最好”的事情:

public void foo(Class<? extends java.lang.annotation.Annotation> annotationClass) { ...

注释没有特定的“类型”类,但您可以使用普通的 Class 对象,并简单地表达您期望注释基类的子类。

关于java - 如何将注释类型作为方法参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55449996/

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