如何使用Java中的Reflection获取注释的值

Myt*_*hul 4 java reflection annotations

如何使用Java获取注释的值Reflection

这是带注释的方法:

@JsonView( { View.class } )
public Element getElement()
{
    return element;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*exR 7

假设您的方法是在类中声明的MyClass,请执行以下操作:

MyClass.class.getMethod("getElement").getAnnotation(JsonView.class).value()
Run Code Online (Sandbox Code Playgroud)


Sel*_*raj 4

这是获取注释的一些方法,

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
Run Code Online (Sandbox Code Playgroud)