If you have downloaded JDK source from jdk6.dev.java.net, the sources for jhat (Java Heap Analysis Tool) is under $JDK/j2se/src/share/classes/com/sun/tools/hat directory. There is an ant build file (build.xml) to build just the jhat (jhat.jar is the build output). I mentioned about Object Query Language (SQL-like language to query Java Heap) support for jhat - you may want to refer to the following:
OQL is implemented using the JavaScript JSR-223 engine in JDK 6. select...from...where is parsed by a simple tokenizer. select and where expressions are JavaScript expressions. Also, if anything that does not start with "select" is directly "eval"-ed as JavaScript. You can use OQL form to write JavaScript functions - which you can call in subsequent OQL queries.