In the last couple of days, I've been trying to read up on ASM, ClassWriter, ClassReader, Visitors (and Visitor pattern), etc. And although I'm understanding more about how these things work, I'm still a little bit at a loss how to write a simple ClassReader and rewrite it with a proper stackmap. I've seen that the ClassReader has a parameter to ignore stackmaps, and ClassWriter has one to write the stackmap, but now I just have to figure out the missing glue logic - there doesn't seem to be an obvious way to read and entire class and rewrite it. I seem to have to identify each part of the class individually (ie: methods, fields, constants, etc). So I'm sure I'm missing something. I hoped it would be a lot easier to simply say something like : ClassWriter.write ( ClassReader.readClass( MyClass.class) ), but it doesn't seem to be so straightforward.
As for the library in question, it's an ancient persistence library called Kodo. It uses (is backed?) by OpenJPA 0.9.5-incubating - just to give an idea how old this product is. For those who haven't heard of Kodo before (I had never heard of it before starting on this project), it was a commercial implementation of JDO before JPA was even released. Produced by a company called Solarmetrics, which was then bought by BEA, and then acquired by Oracle. The product has been dead for 10 years already.
As part of the JDO build process, there is a class enhancer which updates all the persistent classes, adding FK fields, mappings between classes, etc. This build process is in the form of an ant task. I have since managed to wrap the ant process in an antrun plugin to make it "maven-ish", but haven't been able to properly debug the process to fully understand what it is doing. I have dug out what I think is the class though : org.apache.openjpa.enhance.PCEnhancer. This class uses Serp (also produced by BEA, I think) as the bytecode manipualtion library. Current version I'm using is Serp 1.13. I can't tell if newer Serp generates the stackmap or not, but I did determine that newer versions of OpenJPA (ie: v2+) introduced ASM to update the stackmap after Serp (kind of like what I'm looking to do). I'm trying to copy that magic now, but it's more involved that I would have liked (includes reverse engineering the ant plugin to rebuild it as a maven plugin, etc).
If anyone can assist me, I'd be happy to learn more about bytecode manipulation, etc. I've been a longtime user of AJ, but I've never really looked at how it does its magic.
Thanks,
Eric