'Doubts about the ClassReader and the accept method

I would like to know how the "optimization" described in the ASM user manual works.

I took a small snippet from the manual:

If a ClassReader component detects that a MethodVisitor returned by the ClassVisitor passed as argument to its accept method comes from a ClassWriter, this means that the content of this method will not be transformed, and will in fact not even be seen by the application. In this case the ClassReader component does not parse the content of this method, does not generate the corresponding events, and just copies the byte array representation of this method in the ClassWriter.

More precisely, I would like to know:

  1. What events does the ClassReader not generate when the MethodVisitor is retrieving from a ClassWriter and this is detected by the accept method?

  2. Theoretically the ClassReader is just an event producer. Can the accept method really "copy" the content of a method to the ClassWriter of a ClassVisitor that was passed as a parameter or make any other type of modification?

  3. Is the MethodVisitor returned by the visitMethod method really useful for anything? If yes, for what? I have this doubt because even returning null in this method, my modifications occur normally without any kind of error.



Solution 1:[1]

If you pass the ClassReader, the ClassWriter can detect that a MethodVisitor is directly issued from the corresponding ClassReader. In this case, ASM does not deserialize and serialize a method but simply copied the entire methods representation from the class file.

Once you wrap a visitor, it goes back to deserializing and serializing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafael Winterhalter