A为例类Exam
有一些方法,其中有注释。
@Override
public void add() {
int c=12;
}
我怎样才能获得方法名称(添加),其中有@Override
使用注释org.eclipse.jdt.core.IAnnotation
?
A为例类Exam
有一些方法,其中有注释。
@Override
public void add() {
int c=12;
}
我怎样才能获得方法名称(添加),其中有@Override
使用注释org.eclipse.jdt.core.IAnnotation
?
The IAnnotation is strongly misleading, please see the documentation.
To retrieve the Methods from Class that have some annotation. To do that you have to iterate through all methods and yield only those that have such annotation.
public static Collection<Method> methodWithAnnotation(Class<?> classType, Class<? extends Annotation> annotationClass) {
if(classType == null) throw new NullPointerException("classType must not be null");
if(annotationClass== null) throw new NullPointerException("annotationClass must not be null");
Collection<Method> result = new ArrayList<Method>();
for(Method method : classType.getMethods()) {
if(method.isAnnotationPresent(annotationClass)) {
result.add(method);
}
}
return result;
}
您可以使用反射在运行时这样做。
public class FindOverrides {
public static void main(String[] args) throws Exception {
for (Method m : Exam.class.getMethods()) {
if (m.isAnnotationPresent(Override.class)) {
System.out.println(m.toString());
}
}
}
}
编辑:为了在开发时间/设计时间这样做,你可以使用所描述的方法在这里 。
另一种简单的JDT溶液采用AST DOM可以是如下:
public boolean visit(SingleMemberAnnotation annotation) {
if (annotation.getParent() instanceof MethodDeclaration) {
// This is an annotation on a method
// Add this method declaration to some list
}
}
您还需要访问NormalAnnotation
和MarkerAnnotation
节点。