在Java中如何检查当前方法的参数是否有注释并检索该参数值?

发布于 2024-12-02 09:39:23 字数 1382 浏览 2 评论 0原文

考虑这段代码:

public example(String s, int i, @Foo Bar bar) {
  /* ... */
}

我想检查该方法是否有注释 @Foo 并获取参数,如果没有找到 @Foo 注释则抛出异常。

我当前的方法是首先获取当前方法,然后迭代参数注释:

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

class Util {

    private Method getCurrentMethod() {
        try {
            final StackTraceElement[] stes = Thread.currentThread().getStackTrace();
            final StackTraceElement ste = stes[stes.length - 1];
            final String methodName = ste.getMethodName();
            final String className = ste.getClassName();   
            final Class<?> currentClass = Class.forName(className);
            return currentClass.getDeclaredMethod(methodName);
        } catch (Exception cause) {
            throw new UnsupportedOperationException(cause);
        }  
    }

    private Object getArgumentFromMethodWithAnnotation(Method method, Class<?> annotation) {
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();    
            for (Annotation[] annotations : paramAnnotations) {
                for (Annotation an : annotations) {
                    /* ... */
                }
            }
    }

}

这是正确的方法还是有更好的方法? forach 循环内的代码是什么样的?我不确定我是否理解了 getParameterAnnotations 实际返回的内容......

Consider this code:

public example(String s, int i, @Foo Bar bar) {
  /* ... */
}

I want to check if the method has an annotation @Foo and get the argument or throw an exception if no @Foo annotation is found.

My current approach is to first get the current method and then iterate through the parameter annotations:

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

class Util {

    private Method getCurrentMethod() {
        try {
            final StackTraceElement[] stes = Thread.currentThread().getStackTrace();
            final StackTraceElement ste = stes[stes.length - 1];
            final String methodName = ste.getMethodName();
            final String className = ste.getClassName();   
            final Class<?> currentClass = Class.forName(className);
            return currentClass.getDeclaredMethod(methodName);
        } catch (Exception cause) {
            throw new UnsupportedOperationException(cause);
        }  
    }

    private Object getArgumentFromMethodWithAnnotation(Method method, Class<?> annotation) {
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();    
            for (Annotation[] annotations : paramAnnotations) {
                for (Annotation an : annotations) {
                    /* ... */
                }
            }
    }

}

Is this the right approach or is there a better one?
How would the code inside the forach loop look like? I'm not sure if I have understood the what getParameterAnnotations actually returns...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽梦忆笙歌 2024-12-09 09:39:23

外部 for 循环

for (Annotation[] annotations : paramAnnotations) {
   ...
}

应该使用显式计数器,否则您不知道现在正在处理什么参数

final Annotation[][] paramAnnotations = method.getParameterAnnotations();
final Class[] paramTypes = method.getParameterTypes();
for (int i = 0; i < paramAnnotations.length; i++) {
    for (Annotation a: paramAnnotations[i]) {
        if (a instanceof Foo) {
            System.out.println(String.format("parameter %d with type %s is annotated with @Foo", i, paramTypes[i]);
        }
    }
}

还要确保您的注释类型用 @Retention(RetentionPolicy.RUNTIME) 注释

从您的问题来看不完全清楚你想做什么。我们同意形式参数与实际参数的区别:

void foo(int x) { }

{ foo(3); }

其中 x 是参数,3 是参数?

不可能通过反射获取方法的参数。如果可能的话,您必须使用 sun.unsafe 包。不过我不能告诉你太多。

The outer for loop

for (Annotation[] annotations : paramAnnotations) {
   ...
}

should use an explicit counter, otherwise you don't know what parameter you are processing right now

final Annotation[][] paramAnnotations = method.getParameterAnnotations();
final Class[] paramTypes = method.getParameterTypes();
for (int i = 0; i < paramAnnotations.length; i++) {
    for (Annotation a: paramAnnotations[i]) {
        if (a instanceof Foo) {
            System.out.println(String.format("parameter %d with type %s is annotated with @Foo", i, paramTypes[i]);
        }
    }
}

Also make sure your annotation type is annotated with @Retention(RetentionPolicy.RUNTIME)

From your question it is not entirely clear what you are trying to do. We agree on the difference of formal parameters vs. actual arguments:

void foo(int x) { }

{ foo(3); }

where x is a parameter and 3 is an argument?

It is not possible to get the arguments of methods via reflection. If it is possible at all, you would have to use the sun.unsafe package. I can't tell you much about that though.

暮凉 2024-12-09 09:39:23

如果您正在寻找方法上的注释,您可能需要 method.getAnnotations()method.getDeclaredAnnotations()

method.getParameterAnnotations() 调用为您提供方法的形式参数的注释,而不是方法本身的注释。

回头看一下问题标题,我怀疑您正在寻找参数的注释,而我没有在问题的内容中阅读这些注释。如果是这样,您的代码看起来没问题。

请参阅 方法 JavadocAnnotatedElement Javadoc

If you're looking for annotations on the method, you probably want method.getAnnotations() or method.getDeclaredAnnotations().

The method.getParameterAnnotations() call gives you annotations on the method's formal parameters, not on the method itself.

Looking back at the question title, I suspect you are looking for annotations on the parameters, which I didn't read in the content of the question. If that's the case, your code looks fine.

See Method Javadoc and AnnotatedElement Javadoc.

仅此而已 2024-12-09 09:39:23

getParameterAnnotations 返回一个数组,其长度等于方法参数的数量。该数组中的每个元素都包含一个 注释数组< /a> 该参数。
因此,getParameterAnnotations()[2][0] 包含第三个 ([2]) 参数的第一个 ([0]) 注释。

如果您只需要检查至少一个参数是否包含特定类型的注释,则该方法可能如下所示:

private boolean isAnyParameterAnnotated(Method method, Class<?> annotationType) {
    final Annotation[][] paramAnnotations = method.getParameterAnnotations();    
    for (Annotation[] annotations : paramAnnotations) {
        for (Annotation an : annotations) {
            if(an.annotationType().equals(annotationType)) {
                return true;
            }
        }
    }
    return false;
}

getParameterAnnotations returns an array with the length equals to the amount of method parameters. Each element in that array contains an array of annotations on that parameter.
So getParameterAnnotations()[2][0] contains the first ([0]) annotation of the third ([2]) parameter.

If you only need to check if at least one parameter contains an annotation of a specific type, the method could look like this:

private boolean isAnyParameterAnnotated(Method method, Class<?> annotationType) {
    final Annotation[][] paramAnnotations = method.getParameterAnnotations();    
    for (Annotation[] annotations : paramAnnotations) {
        for (Annotation an : annotations) {
            if(an.annotationType().equals(annotationType)) {
                return true;
            }
        }
    }
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文