Aug
10
2008
Reflecting on classes from a Jar that isn’t in your classpath
Posted by: Matt in Software development, tags: java, Software developmentI was recently writing a program to find classes that had a certain annotation on them:
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * This annotation works similarly to the@WebMethod* annotation. It is used to indicate that the annotated method is allowed to be * made visible on a generated facade, and how to do it. * */ @Target(ElementType.METHOD) @Documented @Retention(RetentionPolicy.RUNTIME) public @interface FacadeMethod { /** * This is the operation name that the annotated object will have on the * generated facade. This will be used as the value for *operationNamein the@WebMethod* annotation as well as the name of the method in the facade. * * @return the name that the annotated object will have on the generated * facade */ String operationName(); /** * This is a description of the method which will be used to generate * javadoc on the facade. * * @return a description of the method */ String description() default ""; }
Methods annotated with this method look something like this:
@FacadeMethod(operationName = "effectivelySame",
description = "Return true if the two strings are identical, both null, "
+ "or equivalent after converting to same case and trimming")
public static boolean effectivelySame(String s1, String s2) {
...
What I needed to do was look through a whole mess of jar files that weren’t in the classpath, looking for classes with methods annotated with this annotation, so I could read the operationName and the description properties of the annotation.

Entries (RSS)