How to merge multiple ClassReader instances into a single ClassVisitor?
In the ASM user guide, section 2.2.7 "Transformation chains" states that "several class adapters can delegate to the same ClassVisitor" and figure 2.8 "A complex transformation chain" shows three ClassReaders proceeding through a transformation chain to a single ClassWriter. However, while a code example is given of splitting a ClassVisitor out to several others, I can't for the life of me figure out how one would merge several chains into a single one using the core API without either (a) storing a potentially unbounded amount of data for an indeterminate amount of time or (b) parsing the same class file multiple times.
The problem I'm running into is that ClassReader#accept isn't re-entrant; once you start an accept() call and pass it the first ClassVisitor in the chain, it's going to read through the entire file and call every method, in order. What I'd like to do is:
- Start a parse on several ClassReaders
- Get the header data from all of them and produce the appropriate visit(), visitSource(), etc calls on the output ClassVisitor
- Visit the annotations/attributes from each, passing some or all or more to the output ClassVisitor
- Visit the members from each, passing some or all or more to the output ClassVisitor
- Receive the visitEnd() from each reader, then call visitEnd() on the output
It seems like this should be possible from the docs, but short of sticking each ClassReader in its own Thread and passing calls back and forth over thread boundaries (which would probably be even less performant than just calling accept() over and over again), I can't figure out how to make it work. Can someone please provide an example how merging multiple ClassVisitors is supposed to work?