I think I may have started on the wrong end with my LAPT tool. Both Corky and James told me today that javac is written in Java, is relatively easy to build, and written in a pretty clean way, so maybe I should have created a modified version of javac instead.
However, I first have to check the licensing terms. I don’t know if I could freely distribute the tool then. So I may need to keep my tool around, just clean it up a little. James says I can distribute it in binary form for research purposes only, and in source form as long as I don’t violate Sun’s intellectual property. That probably means that whoever uses my source code (which will probably contain the source of javac) will have to agree to the Sun Community Source License (SCSL) first. But that’s ok with me.
One of the reasons I started from the wrong end was probably that I thought I could do it almost purely by working with class files. When I think of Java, I mostly think if class files now, because that’s how I mostly deal with it now, I don’t actually think of text. I figured I could just pull the line numbers out of the class file, pull the annotations out of the source, and add them to the class file. After about half a day, though, I realized that simply copying some text out of the source wasn’t enough, and I actually needed to parse the Java file. That’s where ANTLR came in, which fortunately was pretty easy to use.
Then I realized that just looking at the AST that ANTLR gives me isn’t enough to create the attribute that I wanted to add. Class names in the AST aren’t fully qualified, but class names in class files always are. I didn’t want to write my own implementation of the algorithm to determine the fully qualified class name, because I’m sure I would have made some mistake, and since I’d realized that annotations on parameters do make it in the class file and are accessible at runtime, I came up with the idea of adding dummy methods to the Java file and turn the annotated local variables into annotated parameters of that dummy method. Then I’d compile the Java file with the additional dummy methods, open the class file, and transfer the annotations to the class file of the original Java file.
Yeah… This is probably the ugliest hack I’ve ever written, and because of these unforeseen problems of parsing and needing fully qualified class names, it took way longer than I expected.
I still think it should be so much easier to do inside javac itself, so I’ll definitely look at it, now that I’ve been told it’s not that hard to work with.