对类阅读器和接受方法的疑问
我想知道ASM用户手册中描述的“优化”是如何工作的。
我从手册中拿了一个小片段:
如果类Rreader组件检测到方法visitor返回 classvisitor作为参数传递给其接受方法的传递来自 班级作者,这意味着此方法的内容不会 转变,实际上甚至不会被应用程序看到。 在这种情况下,类Rreader组件不会解析 此方法不会生成相应的事件,而只是复制了classwriter中此方法的字节数组表示。
更确切地说,我想知道:
当
methodVisitor
从class> classwriter
检索时,classReader
未生成什么事件通过接受方法检测到?理论上,
classReader
只是事件生产者。接受方法可以真正将方法的内容“复制”到classWriter
classVisitor
被作为参数传递或进行任何其他类型的修改?MethodVisitor
由vistmethod
方法返回,对任何事物真的有用吗?如果是,什么?我有疑问,因为即使在此方法中返回null
,我的修改正常发生而没有任何错误。
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:
What events does the
ClassReader
not generate when theMethodVisitor
is retrieving from aClassWriter
and this is detected by the accept method?Theoretically the
ClassReader
is just an event producer. Can the accept method really "copy" the content of a method to theClassWriter
of aClassVisitor
that was passed as a parameter or make any other type of modification?Is the
MethodVisitor
returned by thevisitMethod
method really useful for anything? If yes, for what? I have this doubt because even returningnull
in this method, my modifications occur normally without any kind of error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们正在谈论所有访问……代码> 班级阅读器通常会调用哪个。
为了启用此优化,目标类的恒定池必须与源类兼容。为了确保这一点,您必须将
classReader
传递给classwriter
的构造函数。如果您知道所产生的课程将有足够的共同点以使其有价值,那么这就是您应该做的。相比之下,当您进行了许多重大更改时,这可能会在结果的类文件中留下很多未使用的文物。如果您将
MethodVisitor
从classWriter
直接传递给读者,则适用方法的优化将适用,因此您不会对此特定方法进行任何修改。这对于所有场景都很有用,您只能修改某些方法或不修改任何方法,例如,仅添加新方法。您如何应用修改?生产具有属性的可执行代码或方法的唯一方法是,使用
MethodVisitor
由classWriter
的 vistmethod 直接或间接地使用。当您不返回任何MethodVisitor
而是null
将其返回给读者时,它不会在其上调用任何方法,因此不会重现任何原始工件。如果您要删除或替换整个方法,这可能很有用,但是如果要保留属性和代码,则仅注入一些新属性或代码。而且,由于整个讨论的全部内容,当您将作者的MethodVisitor
直接传递给读者时,您将在结果类文件中重现原始方法,无论是否进行了优化。We are talking about all visit… methods of the
MethodVisitor
which the class reader normally invokes.To enable this optimization, the constant pool of the target class must be compatible with the source class. To ensure this, you have to pass the
ClassReader
to theClassWriter
’s constructor. This is what you should do if you know that the resulting class will have enough in common to make this worthwhile. In contrast, when you have a lot of significant changes, this could leave a lot of unused artifacts in the resulting class file.The optimization for a method applies if you pass the
MethodVisitor
from theClassWriter
directly to the reader, so you will not make any modification to this particular method. This is useful for all scenarios, where you only modify some of the methods or don’t modify any method and, e.g. only add new methods.How do you apply your modifications? The only way to produce executable code or methods with attributes, is by using the
MethodVisitor
returned by theClassWriter
’svisitMethod
, directly or indirectly. When you don’t return anyMethodVisitor
butnull
to the reader, it will not invoke any methods on it, hence, not reproduce any of the original artifacts. This might be useful if you remove or replace the entire method, but not if you want to keep the attributes and the code, to only inject some new attributes or code. And, as the entire discussion is all about, when you pass the writer’sMethodVisitor
directly to the reader, you will reproduce the original method in the resulting class file, whether this is done optimized or not.如果您通过类Reader,则类作者可以检测到MethodVisitor是直接从相应的classReader发出的。在这种情况下,ASM不可避免和序列化方法,而只是复制了类文件中的整个方法表示。
一旦您包裹了访客,它就可以恢复进行序列化和序列化。
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.