Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将注释类型作为方法参数传递?_Java_Annotations_Spring Aop_Aspect - Fatal编程技术网

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

Java 如何将注释类型作为方法参数传递?,java,annotations,spring-aop,aspect,Java,Annotations,Spring Aop,Aspect,有一种方法可以处理注释,比如 public void getAnnotationValue(ProceedingJoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getStaticPart().getSignature(); Method method = methodSignature.getMethod(); Annotation annota

有一种方法可以处理注释,比如

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());
}
getAnnotationValue(joinPoint, RequestHeader);
我想将其转换为一个通用方法,该方法接受joinPoint和注释类型,如

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());
}
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());
    }
public void getAnnotationValue(ProceedingJoinPoint,类您可以做的“最佳”事情:

public void foo(Class<? extends java.lang.annotation.Annotation> annotationClass) { ...
public void foo(Class你能做的“最好”的事情:

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

public void foo(Class你想做的事情不是那样的。问题不在于方法签名,而是你对如何在Java中使用类型的错误理解。在这一行中

annotationType请求参数=(annotationType)注释;
…您有两个错误:

  • 不能使用
    annotationType requestParam
    声明变量,因为
    annotationType
    不是类名文字,而是变量名。这是一个语法错误,编译器将其标记为语法错误
  • 您不能使用
    (annotationType)annotation
    来强制转换,原因与第一种情况相同。Java不是这样工作的,代码只是无效
话虽如此,稍后的代码假设捕获的注释类有一个方法
value()
,这对于某些注释类可能是正确的,但在一般情况下不起作用。但是,假设在调用帮助器方法的所有情况下都存在该方法,则可以将其改为如下所示:

public void getAnnotationValue(连接点连接点){
MethodSignature MethodSignature=(MethodSignature)joinPoint.getStaticPart().getSignature();
Method=methodSignature.getMethod();
Annotation=method.getParameterAnnotations()[0][0];

类您想要做的事情不是这样的。问题不是方法签名,而是您对如何在Java中使用类型的错误理解。在这一行中

annotationType请求参数=(annotationType)注释;
…您有两个错误:

  • 不能使用
    annotationType requestParam
    声明变量,因为
    annotationType
    不是类名文字,而是变量名。这是一个语法错误,编译器将其标记为语法错误
  • 您不能使用
    (annotationType)annotation
    来强制转换,原因与第一种情况相同。Java不是这样工作的,代码只是无效
话虽如此,稍后的代码假设捕获的注释类有一个方法
value()
,这对于某些注释类可能是正确的,但在一般情况下不起作用。但是,假设在调用帮助器方法的所有情况下都存在该方法,则可以将其改为如下所示:

public void getAnnotationValue(连接点连接点){
MethodSignature MethodSignature=(MethodSignature)joinPoint.getStaticPart().getSignature();
Method=methodSignature.getMethod();
Annotation=method.getParameterAnnotations()[0][0];

CLASSTYPE在java中不是变量,您可以使用
Class
Classthough@Andronicus我尝试过使用ClassIn。在这种情况下,请更新您的问题以给出一个答案。另外:当您希望确保其他用户收到您对他们的评论时,请使用@user语法。不幸的是,type在java中不是一个变量,您可以使用
Class
Classthough@Andronicus我尝试过使用ClassIn。在这种情况下,请更新您的问题以给出一个答案。另外:如果您想确保其他用户收到您对他们的评论的通知,请使用@user语法。我已经尝试过了,但是出现了键入未解决错误的提示!!@KNDheeraj我想是这样的。因此,我建议您请给出一个显示编译器错误的最小示例。否则我们无法帮助您。我已经尝试过了,但提示输入未解决的错误!!@KNDheeraj我猜到了。因此,我建议您阅读并相应地增强您的问题。给我们一个显示编译器错误的最小示例对不起,否则我们帮不了你。