I want an easy to use API from cglib or some wrapper class to achieve the following class transformation, so that while I use the class without any proxy involved.

@Entity
public class SomeProcess extends SomeProcessBase implements Serializable {

@ToBeTransformed
public void start() {
    //do some business logics
}

}

After class has been transformed, I expect it would be like this:

@Entity
public class SomeProcess extends SomeProcessBase implements Serializable {

public void start() {
    Executor.execute(new Executable() {
        public void execute() {

          //do some business logics

        }
    });

}

}

So while I want to call someProcess.start, i can directly use the following code:

SomeProcess process = new SomeProcess();
process.start();

Other than

SomeProcess process = new SomeProcess();
SomeProcess processProxy = Proxy.wrapper(process);
processProxy.start();
有帮助吗?

解决方案

You can use javassist http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/tutorial/tutorial.html to change a class on the fly. Something like this:

    ClassPool pool = ClassPool.getDefault();
    CtClass sp = pool.get("SomeProcess");
    for (CtMethod m : sp.getDeclaredMethods()) {
        if (m.hasAnnotation(ToBeTransformed.class)) {
            String body = // create new body 
            m.setBody(body);
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top