我只想检测带有注释的方法@Test
,因此在visitCode()
被调用之前,我想知道它是否带有注释@Test
。从我运行的测试中,我可以说visitAnnotation()
总是首先调用它,但是我不知道是否总是这样,还是有例外。
更新:来自ASM的代码:
这visitAnnotation()
是所谓的:
if (runtimeVisibleAnnotationsOffset != 0) {
attributeLength = this.readUnsignedShort(runtimeVisibleAnnotationsOffset);
for (currentAnnotationOffset = runtimeVisibleAnnotationsOffset + 2; attributeLength-- > 0;
currentAnnotationOffset = this.readElementValues(methodVisitor.visitAnnotation(annotationDescriptor, true), currentAnnotationOffset, true, charBuffer)) {
annotationDescriptor = this.readUTF8(currentAnnotationOffset, charBuffer);
currentAnnotationOffset += 2;
}
}
...
...
然后,它调用visitCode()
:
if (codeOffset != 0) {
methodVisitor.visitCode();
this.readCode(methodVisitor, context, codeOffset);
}
因此,由此很明显,首先调用了visitAnnotation(),但这是在某处指定的还是仅仅是实现细节?
它是..有效指定的,我猜呢?
关键是,按照Java虚拟机规范关于类文件格式的部分,在类文件格式中,注释一定要排在第一位。
ASM可以不遗余力地读取所有这些批注,缓存该信息,继续访问方法定义的代码部分,然后返回并将缓存的信息反馈给您的ASM节点访问者。
ASM的开发团队还可以拿出锤子,用锤子砸自己的脸。
听起来都一样明智。
问题是,因为在类文件排序IS规定,我认为这是那些地方,即使ASM规范没有正式说出来,你可以睡得香甜知道这将永远是真正的一个(或至少,只要类文件按该顺序执行即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句