2

I know (at least using either BCEL, or ASM, for instance), it is possible to somehow access local variables of a method... but, I need something more, what I would like is:

  1. to get the type of such a variable (or a way to convert from the signature)
  2. to know (distinguish) when this variable is used (either sees it value affected, or is passed as parameter)
  3. when this variable is used as parameter, to know which method call it was passed to
  4. to break "method-chains" in their respective method calls and get their return value so I can manipulate them

The basic idea is that I would like to "instrument" methods a bit in the same way a debugger does (though limited to the first frame depth...).

Any pointer appreciated. If more information need, feel free to ask.

Jester
  • 56,577
  • 4
  • 81
  • 125
strexxx
  • 11
  • 1

1 Answers1

0

This is only possible using a byte code-level API. cglib does not expose such an API such that you have to choose between ASM, BCEL and Javassist where I would recommend you ASM which has the best documentation.

What you would need to do:

  1. Parse the signature of the method, ASM offers utilities for that. You would get any type by its internal name. You would need to map these names to their index.
  2. Find any use of the variable that is used from that index.

This is however a quite difficult task. In order to predict your code, you would have to emulate the method invocation. The JVM is a stack machine, arguments can be placed on the operand stack as a result of an arbitrary chain of commands. Therefore, you would effectively have to interpret any byte code instruction that you find. You will, more or less, need to write your own simplistic interpreter what is quite a task.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • It will be easier, if there are debug information attached to the method. I guess, if the OP wants process the methods “in the same way a debugger does” that will be the way to go (which implies not supporting methods not having these attributes). – Holger Feb 17 '15 at 10:49