mirror of
https://gitee.com/huoyo/ko-time.git
synced 2025-12-06 08:48:30 +08:00
update docs
This commit is contained in:
parent
ad1f32ddaf
commit
bda47960c7
@ -1,37 +0,0 @@
|
||||
package cn.langpy.kotime.staaop;
|
||||
|
||||
|
||||
import cn.langpy.kotime.util.Context;
|
||||
import javassist.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.security.ProtectionDomain;
|
||||
|
||||
public class AgentHandler implements ClassFileTransformer {
|
||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
|
||||
className = className.replace("/",".");
|
||||
/**/
|
||||
|
||||
try {
|
||||
CtClass ctClass= ClassPool.getDefault().get(className);
|
||||
CtMethod[] declaredMethods = ctClass.getDeclaredMethods();
|
||||
for (CtMethod method : declaredMethods) {
|
||||
if (Modifier.isStatic(method.getModifiers())) {
|
||||
method.insertBefore("System.out.println(\"前置--\");");
|
||||
method.insertAfter("System.out.println(\"后置--\");");
|
||||
}
|
||||
}
|
||||
ctClass.detach();
|
||||
return ctClass.toBytecode();
|
||||
} catch (NotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (CannotCompileException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return classfileBuffer;
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
package cn.langpy.kotime.staaop;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.lang.instrument.UnmodifiableClassException;
|
||||
|
||||
public class StaticAgent {
|
||||
public static void agentmain(String agentArgs, Instrumentation inst){
|
||||
System.out.println("修改静态方法");
|
||||
/*true 必须 */
|
||||
inst.addTransformer(new AgentHandler(),true);
|
||||
Class[] classes = inst.getAllLoadedClasses();
|
||||
for (Class clazz : classes) {
|
||||
if (clazz.getName().equals("com.example.demo.controller.Index2Controller")) {
|
||||
try {
|
||||
inst.retransformClasses(clazz);
|
||||
} catch (UnmodifiableClassException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user