views:

184

answers:

2

How do I retrieve a list of deprecated methods from a class.

I need to list the methods that have been marked as deprecated for a class to pass on to documentation.

I don't really want to copy and paste each method and its javadoc into a seperate file, is it possible to do this through the javadoc tool or through eclipse?

+3  A: 

Actually javadoc automatically generates a deprecated-list.html page. Just run javadoc, and see if that is what you need.

siddhadev
+3  A: 

This gets all the methods of the specified class:

public class DumpMethods {
  public static void main(String args[])
  {
    try {
      Class c = Class.forName(args[0]);
      Method m[] = c.getDeclaredMethods();
      for (int i = 0; i < m.length; i++)
      System.out.println(m[i].toString());
    }
    catch (Throwable e) {
      System.err.println(e);
    }
  }
}

To get the deprecated methods, for each method, do something like this:

Method method = ... //obtain method object
Annotation[] annotations = method.getDeclaredAnnotations();

for(Annotation annotation : annotations){
    if(annotation instanceof DeprecatedAnnotation){
        // It's deprecated.
    }
}
John Feminella
This assumes the deprecated methods have been annotated, which may be OK. It'll fall down if older code uses the /**@deprecated*/ javadoc notation.
McDowell
That's true. However, there is no programmatic solution other than this one that I'm aware of, and he was looking for a programmatic solution.
John Feminella
Or, at least, he seemed to be! :)
John Feminella