Search code examples
javainstrumentationjavassist

Javassist capture local variable values for specific statements


I'm trying to capture the values of variables involved in a conditional expression of branch and loop statements eg:

if (a + b < c - 5) { // here, capture value of a, b and c
    // if body
}

for (int i = 0; i / x < 4 ; i++){ // here, capture value of i and x on exit of loop
    // for body
}

Does Javassist provide ExpressionEditor for if, switch, for and while statements like it does for FieldAccess, MethodCall, Handler etc.?

Is there any other approach to this problem? (I'm interested in doing this at runtime)


Solution

  • I don't really think that this is possible, with Javassist you cannot go and visit this deep in the ByteCode.

    Your needs are too fine grained and the library is not supporting it as you can see from this document you can access to all the variables that are parameters of a method or return of a method but you cannot find anything about all the other variables.

    If I may suggest something you should take a look at ASM. I am not an expert about it but, as I know and according to this documentation should provide more insight view of the bytecode.