This post originated from an RSS feed registered with Agile Buzz
by Joe Walnes.
Original Post: A simple way to access JavaDoc attributes at runtime
Feed Title: Joe's New Jelly
Feed URL: http://joe.truemesh.com/blog/index.rdf
Feed Description: The musings of a ThoughtWorker obsessed with Agile, XP, maintainability, Java, .NET, Ruby and OpenSource. Mmm'kay?
It's easy to gain access to JavaDoc attributes using QDox, however this requires you to have the source available. At runtime this isn't always possible, unless you're happy giving your source away.
A workaround to this is to use QDox to parse the source code at development time and serialize the resulting structures to make them available at runtime. This will work, but it's a bit nasty as big binary files aren't the most stable of things.
A more open approach is to bundle the source of your classes with the application, making them available at runtime, however without the method body implementations.
Class with method body implementations:
package stuff;
public class CheeseSlicer {
/** @transaction mode=isolated */
public Slice feedMe() {
// some implementation specific stuff here.. don't waste your time reading it.
if (System.currentTimeMillisMillis() % 2 == 0) {
return new CheddarSlice();
} else {
return new BrieSlice();
}
}
}
Class WITHOUT method body implementations:
package stuff;
public class CheeseSlicer {
/** @transaction mode=isolated */
public Slice feedMe();
}
To do this is simple. First, QDox can be used to parse the source of a class into a JavaClass. Calling JavaClass.toString() will print out the class without the body implementations (as above). This is something done at development time and the result should be written to a file that is available at runtime.
At runtime, QDox can read this file just as if it were a full-blown Java source file, making the attributes available.
JDK 1.5, of course, makes this redundant, but in the mean time...