0

everybody, I want to get the synchronized block parameter, such as

    String obj;  
    synchronized(obj){
        ... 
    }

How can I get the parameter 'obj' at byte code level using Javassist? Any suggestions are welcome.

scala
  • 11
  • 3

1 Answers1

0

You will have to use the low-level API of Javassist or ASM to analyze the bytecode instructions in order to do what you want.

 Object obj;
 synchronized(obj){
   //...
 }

Translates into

 0:   aload_0
 1:   getfield        #2; //Field obj:Ljava/lang/Object;
 4:   dup
 5:   astore_1
 6:   monitorenter
 ...

The monitorenter instruction is the start of synchronized block and astore_1 instruction just before that places the obj field value on top of the stack - that is the value you're looking for.

Anton Arhipov
  • 6,479
  • 1
  • 35
  • 43