我有这样的界面:
public interface IFoo{
@AnnotationTest(param="test")
String invoke();
}
Run Code Online (Sandbox Code Playgroud)
我这样实现:
public class Foo implements IFoo{
@Override
public String invoke(){
Method method = new Object() {
}.getClass().getEnclosingMethod();
AnnotationTest ann = method.getAnnotation(AnnotationTest.class);
if(ann == null){
System.out.printl("Parent method's annotation is unreachable...")
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果可以达到父母的注释,我想学习它的方法.
任何帮助或想法将不胜感激.
sup*_*bob 10
您可以使用Spring AnnotationUtils.findAnnotation从接口读取注释.
示例:
接口 I.java
public interface I {
@SomeAnnotation
void theMethod();
}
Run Code Online (Sandbox Code Playgroud)
实施课程 A.java
public class A implements I {
public void theMethod() {
Method method = new Object() {}.getClass().getEnclosingMethod();
SomeAnnotation ann = AnnotationUtils.findAnnotation(method, AnnotationTest.class);
}
}
Run Code Online (Sandbox Code Playgroud)
它显然需要包含在您的项目(和导入)Spring框架类中.