0

I hava A Class in another jar and i use it in B bean. Now i want to add log for method in A Class. How can i do this in my project without fix the jar.

My mind:

  1. use ApplicationListener to redefine class before bean init.

  2. do something in onApplicationEvent() to redefine the A class. // this is my question.

I know that can use asm or other tool to fix bytecode. I hava see instrument and do not find solution . https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html

How to obtain instance of Instrumentation in Java

A class.

public class A {

public void find(){
    System.out.println("aaa");
    //i want to add log here.
}

B bean

@Service public class B { 
public A get(){
    return new A();
}

ApplicationListener

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    if(event.getApplicationContext().getParent() != null){
        return;
    }
   // redefine class
}

then when i use b.get().find() it will print the log i add.

1 Answers1

0

find my solution. using javassist modify the class. and load it (using CtClass.toClass())in advance. https://www.javassist.org/tutorial/tutorial.html