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
2010-04-21 04:33:40
why LinkedList??
01
2010-04-21 06:55:19
No reason. Could (Should) have used a Set.
Chuk Lee
2010-04-21 10:29:22
Does the solution change if doclets are used?
Eclatante
2010-04-22 14:47:20
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
2010-04-22 15:00:30