The Programming Apprentice The Foosball Wizard
Aug 12

Giving away attributes without all of your source

Tech Add comments

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! :)

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What are the first four letters in the word British?