views:

75

answers:

1

I have a code base where developers use @author annotations on their class definitions. Is there a way for me to be able to programmatically count how many classes are authored by each developer using those annotations?

+4  A: 

Assuming this is how you use the annotation

@Author("fred")
public class MyClass {...

Then here is a method that will do it

public List<Class> getClassesWrittenBy(String name, List<Class> classList) {
   List<Class> list = new LinkedList<Class>();
   for (Class clazz: classList)
      if (clazz.isAnnotationPresent(Author.class)) {
          Author author = clazz.getAnnotation(Author.class);
          if (author.value().equals(name))
             list.add(clazz);
      }
   return (list);
}
Chuk Lee
why LinkedList??
01
No reason. Could (Should) have used a Set.
Chuk Lee
Does the solution change if doclets are used?
Eclatante
doclets are not annotations (in the programming sense). You will probably have to write your doclet plugins to process that or good old fashion grep
Chuk Lee