如何在Java中使用反射获取注释的值
如何使用Java如何在Java中使用反射获取注释的值,java,reflection,annotations,Java,Reflection,Annotations,如何使用Java反射获取注释的值 这是带注释的方法: @JsonView( { View.class } ) public Element getElement() { return element; } 这里有一些获取注释的方法 Class<?> cls = Class.forName(name); cls.getDeclaredAnnotations(); // get all annotation (cls.getDeclaredMethods()[
反射
获取注释的值
这是带注释的方法:
@JsonView( { View.class } )
public Element getElement()
{
return element;
}
这里有一些获取注释的方法
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
Class cls=Class.forName(name);
cls.getDeclaredAnnotations();//获取所有注释
(cls.getDeclaredMethods()[0]).getAnnotations()//获取方法的注释
注释ety=cls.getAnnotation(Annotation.class);//获取特定注释类的注释
假设您的方法是在类MyClass
中声明的,请执行以下操作:
MyClass.class.getMethod("getElement").getAnnotation(JsonView.class).value()
你有没有花时间查这个?在Java中,这是非常简单的事情,web上到处都有这样的例子。他们不可能让它更简单。在
注释上没有value()
方法
@AlexBurdusel是一种通用方法,@JsonView
必须声明一个value()
方法才能按照问题正文中显示的方式使用它。