I am using ASM library to instrument some classes. I create my own instance of MethodVisitor
and override some of its methods e.g. visitCode()
which is called when the MethodVisitor
is at the start of the method. Here, I insert my own code if the method is not a getter/setter. But how can I know if the method is not a getter/setter if the visitor is only at the start of the method and the rest of the method hasn't been visited yet?
I could look at the name of the method, but that's not conclusive. To test if a method is a getter or setter I'd like check if the only thing which the method does, is that it uses a GETFIELD or SETFIELD opcode on the current instance and nothing else.
UPDATE: One way I found to work is to override the visitField()
method as well on my ClassVisitor
, not just the visitMethod()
, so I store all the field names of the class and check if a method has the name getX()
or setX()
where X is a field name. This approach would work but it is still limited because what if the user has such a method named like that, but it is not a setter/getter.