1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
public class HisenInstrumentation { public void init(Instrumentation instrumentation) { new AgentBuilder.Default() .disableClassFormatChanges() .with(RETRANSFORMATION) .with(AgentBuilder.RedefinitionStrategy.Listener.StreamWriting.toSystemError()) .with(AgentBuilder.Listener.StreamWriting.toSystemError().withTransformationsOnly()) .with(AgentBuilder.InstallationListener.StreamWriting.toSystemError()) .ignore(none()) .ignore( nameStartsWith("net.bytebuddy.") .or(nameStartsWith("jdk.internal.reflect.")) .or(nameStartsWith("java.lang.invoke.")) .or(nameStartsWith("com.sun.proxy.")) ) .disableClassFormatChanges() .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE) .with(AgentBuilder.TypeStrategy.Default.REDEFINE) .type(nameEndsWith("Hisen")) .transform( (builder, type, classLoader, transformer, module) -> builder.visit(Advice.to(HisenAdvice.class).on(isMethod().and(named("hello"))))) .installOn(instrumentation); }
public static class HisenAdvice { @Advice.OnMethodEnter static long enter(@Advice.Argument(value = 0, typing = DYNAMIC, readOnly = false) String name) { System.out.println("name before:" + name); name += System.currentTimeMillis(); return System.currentTimeMillis(); }
@Advice.OnMethodExit(onThrowable = RuntimeException.class) static void exit( @Advice.Enter long startTime) { System.out.println("HisenAdvice exit. time use:" + (System.currentTimeMillis() - startTime)); } } }
|