Java Reference
In-Depth Information
*/
public
public static
static List < Class <?>> findClassesWithAnnotatedMethods ( String packageName ,
Class <? extends
extends Annotation > methodAnnotationClass ) throws
throws Exception {
List < Class <?>> ret = new
new ArrayList <>();
String [] classes = ClassesInPackage . getPackageContent ( packageName );
for
for ( String clazz : classes ) {
Class <?> c = Class . forName ( clazz );
for
for ( Method m : c . getMethods ()) {
iif ( m . isAnnotationPresent ( methodAnnotationClass )) {
ret . add ( c );
}
}
}
return
return ret ;
}
See Also
Using and Defining Annotations , and the rest of this chapter.
Program: CrossRef
You've probably seen those other Java books that consist entirely of listings of the Java API
for version thus-and-such of the JDK. I don't suppose you thought the authors of these works
sat down and typed the entire contents from scratch. As a programmer, you would have real-
ized, I hope, that there must be a way to obtain that information from Java. But you might
not have realized how easy it is! If you've read this chapter faithfully, you now know that
there is one true way: make the computer do the walking. Example 23-21 is a program that
puts most of the techniques together. This version generates a cross-reference listing, but by
overriding the last few methods, you could easily convert it to print the information in any
format you like, including an API Reference book. You'd need to deal with the details of this
or that publishing software—FrameMaker, troff, T E X, or whatever—but that's the easy part.
This program makes fuller use of the Reflection API than did MyJavaP in Printing Class In-
formation . It also uses the java.util.zip classes (see Reading and Writing JAR or ZIP
Archives ) to crack the JAR archive containing the class files of the API. Each class file
found in the archive is loaded and listed; the listing part is similar to MyJavaP .
Example 23-21. CrossRef.java
Search WWH ::




Custom Search