This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Giving away attributes without all of your source
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Joe Walnes has talked about an interesting tactic to allowing people to grab attributes (via QDox, or whatever you want), without having to give away the entire source code.
This has been a problem, as (until Java 5), the attribute data is not available in the bytecode. Instead of doing something wacky like parsing the attributes into some other form which you can distribute, how about just taking out the body of the methods and turn:
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();
}
}
}
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.
Simple! Lovely! :)