我开始大量使用Java注释.一个例子是使用注释方法并将它们转换为基于'telnet'的命令行命令.我这样做是通过解析注释并挂钩到jopt选项解析器.
但是,我手动做了很多这些.例如,Method参数注释处理..
Method method = ... //;
Class[] parameters = method.getParamterTypes();
Annotation[][] annotations = method.getparamterAnnotations();
for( int i = 0; i < parameters.length; i++ )
{
// iterate through the annotation , see if each param has specific annotation ,etc.
}
Run Code Online (Sandbox Code Playgroud)
这是多余和乏味的.
是否有任何开源项目有助于处理注释?
我们使用它来查找特定的注释:
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(MyAnnotation.class)) {
field.setAccessible(true);
String fieldName = field.getName();
Object fieldValue = field.get(myObj);
field.setAccessible(false);
}
}
Run Code Online (Sandbox Code Playgroud)