是否可以从printTitle
内部访问var
的@Anno
实例?
class MyClass {
@Anno(title="Title") Integer var;
void someFunc() {
printTitle(var);
}
public void printTitle(Integer param) {
// How to get to the @Anno annotation?
// Is it possible to get to the 'Field' where param is coming from?
Anno anno = param. ... .getAnnotation(Anno.class);
if(annotation != null) {
...
}
}
}
我不想将var
的名称用作字符串"var"
因为以后很难重构。
无法
从String
获取注解,需要一个表示String var
的Field
对象。
void someFunc() {
printTitle("var");
}
public void printTitle(String field) {
Anno anno = getClass().getField(field).getAnnotation(Anno.class);
if(annotation != null) {
...
}
}