本文介绍了Java中如何使用反射获取注解的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Java Reflection
获取注解的值?
How can I get the value of the annotation using Java Reflection
?
这是带注释的方法:
@JsonView( { View.class } )
public Element getElement()
{
return element;
}
推荐答案
这里有一些获取注解的方法,
Here the some way to get annotation,
Class<?> cls = Class.forName(name);
cls.getDeclaredAnnotations(); // get all annotation
(cls.getDeclaredMethods()[0]).getAnnotations(); //get annotation of a method
Annotation ety = cls.getAnnotation(Annotation.class); // get annotation of particular annotation class
这篇关于Java中如何使用反射获取注解的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!