检索 Java 注释属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1805200/
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
Retrieve Java Annotation Attribute
提问by Diego Dias
How can I retrieve the value of an annotation on the annotated method??
如何在带注释的方法上检索注释的值?
I have:
我有:
@myAnnotation(attribute1 = value1, attibute2 = value2)
public void myMethod()
{
//I want to get value1 here
}
采纳答案by ChssPly76
- Obtain
Method
instance. - Obtain annotation.
- Obtain annotation attribute value.
- 获取
Method
实例。 - 获取注释。
- 获取注释属性值。
Something like:
就像是:
Method m = getClass().getMethod("myMethod");
MyAnnotation a = m.getAnnotation(MyAnnotation.class);
MyValueType value1 = a.attribute1();
You'll need to catch / handle the appropriate exceptions, of course. The above assumes you are indeed retrieving method from the current class (replace getClass()
with Class.forName()
otherwise) and the method in question is public (use getDeclaredMethods()
if that's not the case)
当然,您需要捕获/处理适当的异常。以上假设您确实是从当前类中检索方法(getClass()
用Class.forName()
其他方法替换)并且所讨论的方法是公共的(getDeclaredMethods()
如果不是这种情况,则使用)
回答by mhaller
Two important things:
两个重要的事情:
- There is no way to get the current method, e.g. there is no getMethod() such as getClass(). Therefore, the method accessing its own annotation would need to know its own name.
- The retention policyof the annotation must be set to
RUNTIME
, so you can access the annotation at runtime. The default is compile-time, which means annotations are available in the class file, but cannot be accessed at runtime using reflection.
- 有没有办法让目前的方法,如没有getMethod(),如的getClass()。因此,访问自己注解的方法需要知道自己的名字。
- 注释的保留策略必须设置为
RUNTIME
,以便您可以在运行时访问注释。默认是编译时,这意味着注解在类文件中可用,但不能在运行时使用反射访问。
Full example:
完整示例:
@Retention(RetentionPolicy.RUNTIME)
public static @interface MyAnnotation {
String value1();
int value2();
}
@Test
@MyAnnotation(value1 = "Foo", value2 = 1337)
public void testAnnotation() throws Exception {
Method[] methods = getClass().getMethods();
Method method = methods[0];
assertEquals("testAnnotation", method.getName());
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
assertEquals("Foo", annotation.value1());
assertEquals(1337, annotation.value2());
}
回答by alphazero
@mhaller: a bit too long for a comment on your post. Obviously would need further refinement to deal with overloaded methods, but it is not impossible.:
@mhaller:对您的帖子发表评论有点太长了。显然需要进一步细化来处理重载的方法,但这并非不可能。:
import java.lang.reflect.Method;
public class Hack {
public static void main (String[] args) {
(new Hack()).foobar();
}
public void foobar () {
Method here = getCurrentMethod(this);
System.out.format("And here we are: %s\n", here);
}
public static final Method getCurrentMethod(Object o) {
String s = Thread.currentThread().getStackTrace()[2].getMethodName();
Method cm = null;
for(Method m : o.getClass().getMethods()){
if(m.getName().equals(s)){
cm = m; break;
}
}
return cm;
}
}
[edit: credit/thanks to Alexandr Priymak for spotting the error in main()]
[编辑:感谢 Alexandr Priymak 发现 main() 中的错误]
回答by s s
To get the current method, try using this code:
要获取当前方法,请尝试使用以下代码:
Thread.currentThread().getStackTrace()[1].getClassName().toString()+\".\"+Thread.currentThread().getStackTrace()[1].getMethodName().toString()