在Java中如何检查当前方法的参数是否有注释并检索该参数值?
考虑这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
外部 for 循环
应该使用显式计数器,否则您不知道现在正在处理什么参数
还要确保您的注释类型用
@Retention(RetentionPolicy.RUNTIME)
注释从您的问题来看不完全清楚你想做什么。我们同意形式参数与实际参数的区别:
其中
x
是参数,3
是参数?不可能通过反射获取方法的参数。如果可能的话,您必须使用
sun.unsafe
包。不过我不能告诉你太多。The outer for loop
should use an explicit counter, otherwise you don't know what parameter you are processing right now
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:
where
x
is a parameter and3
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.如果您正在寻找方法上的注释,您可能需要
method.getAnnotations()
或method.getDeclaredAnnotations()
。method.getParameterAnnotations()
调用为您提供方法的形式参数的注释,而不是方法本身的注释。回头看一下问题标题,我怀疑您正在寻找参数的注释,而我没有在问题的内容中阅读这些注释。如果是这样,您的代码看起来没问题。
请参阅 方法 Javadoc 和 AnnotatedElement Javadoc。
If you're looking for annotations on the method, you probably want
method.getAnnotations()
ormethod.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.
getParameterAnnotations
返回一个数组,其长度等于方法参数的数量。该数组中的每个元素都包含一个 注释数组< /a> 该参数。因此,
getParameterAnnotations()[2][0]
包含第三个 ([2]
) 参数的第一个 ([0]
) 注释。如果您只需要检查至少一个参数是否包含特定类型的注释,则该方法可能如下所示:
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: