java 有没有更有效的方法来获得带注释的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2237289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a more efficient way to get an annotated method?
提问by ?ukasz Bownik
I started a "for fun, nobody knows, nobody cares" open source project (LinkSet).
我开始了一个“为了好玩,没人知道,没人关心”开源项目(LinkSet)。
In one place I need to get an annotated method of a class.
在一个地方,我需要获得一个类的带注释的方法。
Is there a more efficient way to do it than this? I mean without the need of iterating through every method?
还有比这更有效的方法吗?我的意思是不需要遍历每种方法?
for (final Method method : cls.getDeclaredMethods()) {
final HandlerMethod handler = method.getAnnotation(HandlerMethod.class);
if (handler != null) {
return method;
}
}
回答by BalusC
Take a look for Reflections(dependencies: Guavaand Javassist). It's a library which has already optimized the most of it all. There's a Reflections#getMethodsAnnotatedWith()which suits your functional requirement.
查看Reflections(依赖项:Guava和Javassist)。这是一个已经对其进行了最优化的库。有一个Reflections#getMethodsAnnotatedWith()适合您的功能要求。
Here's an SSCCE, just copy'n'paste'n'run it.
这是一个SSCCE,只需复制'n'paste'n'run它。
package com.stackoverflow;
import java.lang.reflect.Method;
import java.util.Set;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
public class Test {
@Deprecated
public static void main(String[] args) {
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forPackage("com.stackoverflow"))
.setScanners(new MethodAnnotationsScanner()));
Set<Method> methods = reflections.getMethodsAnnotatedWith(Deprecated.class);
System.out.println(methods);
}
}
回答by Bozho
No. But this is not inefficient at all.
不。但这一点也不低效。
For example spring is using the following code:
例如 spring 使用以下代码:
public static <A extends Annotation> A getAnnotation(
Method method, Class<A> annotationType) {
return BridgeMethodResolver.
findBridgedMethod(method).getAnnotation(annotationType);
}
(where the BridgedMethodResolveris another topic, but it just returns a Methodobject)
(其中BridgedMethodResolver是另一个主题,但它只返回一个Method对象)
Also, instead of comparing to null, you can check whether an annotation is present with isAnnotationPresent(YourAnnotation.class)(as suggested in the comments below the question)
此外,null您可以检查注释是否存在isAnnotationPresent(YourAnnotation.class)(如问题下方的评论中所建议),而不是与 进行比较
回答by wheaties
If you're going to make several calls to this for every class you can create a descriptor like class which does nothing more than cache this type of information. Then when you want to retrieve the information you just look at it's descriptor.
如果您要为每个类多次调用 this,您可以创建一个类似 class 的描述符,它只会缓存此类信息。然后,当您想检索信息时,只需查看它的描述符即可。
To answer your question:
回答你的问题:
Class<?> _class = Whatever.class;
Annotation[] annos = _class.getAnnotations();
will return all annotations of a class. What you've done will only return the very first annotation of a method. Like wise:
将返回一个类的所有注释。您所做的只会返回方法的第一个注释。同样地:
Annotion[] annos = myMethod.getAnnotations();
returns all the annotations of a given method.
返回给定方法的所有注释。

