How to use Javassist insert codes after super.xxx ()

class A extends B{
public void a(){
    super.a();
    System.out.println("hello");
}
}

I want to use javassist to insert some codes after super.xxx, for example:

class A extends B{
public void a(){
    super.a();
    System.out.println("inject"); // javassist 
    System.out.println("hello");
}
}

if origin codes do not contain super.xxx, then just enter the method for using the .inertBefore code.

My question is how to determine if the method contains "super.xxx" and how can I embed codes as described above.

+4
source share
2 answers

According to your code, it looks like the end of a method into which you want to insert code so that you can use the method ClassPool insertAfter()to add lines at the end of a particular method.

insertAt(), .

0

Javassist CtBehavior::insertAt. , -g, ​​ . , , , , .

super? , B, , CtBehavior::insertAfter:

if (this instanceof A) {
  // your code goes here.
}

, .

0

Source: https://habr.com/ru/post/1656490/


All Articles