import net.bytebuddy.ByteBuddy; import net.bytebuddy.asm.Advice; import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.implementation.MethodDelegation; import net.bytebuddy.matcher.ElementMatchers; public class PerformanceOptimizer { public static <T> T optimize(Class<T> targetClass) throws IllegalAccessException, InstantiationException { DynamicType.Unloaded<T> dynamicType = new ByteBuddy() .subclass(targetClass) .method(ElementMatchers.any()) .intercept(Advice.to(PerformanceInterceptor.class)) .make(); Class<? extends T> dynamicClass = dynamicType.load(targetClass.getClassLoader()) .getLoaded(); return dynamicClass.newInstance(); } public static class PerformanceInterceptor { @Advice.OnMethodEnter public static void enter(@Advice.Origin String method) { System.out.println("Entering method: " + method); } @Advice.OnMethodExit public static void exit(@Advice.Origin String method) { System.out.println("Exiting method: " + method); } } }


上一篇:
下一篇:
切换中文