How can I get the method name which has annotation?

eclipse-jdt, eclipse-plugin, java

Solution

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;
}

Problem

A class for example `Exam` has some methods which has annotation. ``` @Override public void add() { int c=12; } ``` How can I get the method name (add) which has `@Override` annotation using `org.eclipse.jdt.core.IAnnotation`?

Original source