Issue
I'd like to instrument only methods annotated with @Test
, so before visitCode()
is called, I'd like to know if it's annotated with @Test
or not. From the tests I ran, I can say that visitAnnotation()
was always called first, but I don't know if this is always the case, or there are exceptions.
Update: code from ASM:
This is where visitAnnotation()
is called:
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;
}
}
...
...
And after this, it calls visitCode()
:
if (codeOffset != 0) {
methodVisitor.visitCode();
this.readCode(methodVisitor, context, codeOffset);
}
So from this it is pretty clear, that visitAnnotation() is called first, but is this specified somewhere or is it just an implementation detail?
Solution
The ASM javadoc specifes the order of all method calls and yes, by contract, this order is guaranteed, visitAnnotation is called before visitCode. If you use official ASM emitters such as ClassReader, the order will always follow the specification.
There is however no order guarantee built into the type system. Anybody could implement a different order and some consumers such as ClassWriter tolerate such deviation, even though it's not always explicitly guaranteed.
(Update by kriegaex) If you want to know for sure, just check out the ASM manual, chapter 3.2.1 where this is documented explicitly:
Answered By - Rafael Winterhalter