• byte buddy字节码增强——输出方法执行时间


    目标: 输出各函数执行时间

    1. 引包
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>bytebuddyTest</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
        <properties>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>net.bytebuddy</groupId>
                <artifactId>byte-buddy</artifactId>
                <version>1.8.20</version>
            </dependency>
            <dependency>
                <groupId>net.bytebuddy</groupId>
                <artifactId>byte-buddy-agent</artifactId>
                <version>1.8.20</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>8</source>
                        <target>8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            // maven中指定Premain-Class
                            <manifestEntries>
                                <Premain-Class>buddy.agent.MethodAgent</Premain-Class>
                                <Can-Redefine-Classes>true</Can-Redefine-Classes>
                                <Can-Retransform-Classes>true</Can-Retransform-Classes>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    • 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
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    1. Pre-Main 操作类
    package buddy.agent;
    
    import net.bytebuddy.agent.builder.AgentBuilder;
    import net.bytebuddy.description.type.TypeDescription;
    import net.bytebuddy.dynamic.DynamicType;
    import net.bytebuddy.implementation.MethodDelegation;
    import net.bytebuddy.matcher.ElementMatchers;
    import net.bytebuddy.utility.JavaModule;
    
    import java.lang.instrument.Instrumentation;
    
    public class MethodAgent {
    
        public static void premain(String args, Instrumentation instrumentation) {
    
            System.err.println("MethodAgent " + args);
    
            AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule) -> {
                return builder.method(ElementMatchers.any()).intercept(MethodDelegation.to(MethodTimeCheck.class));
            };
            AgentBuilder.Listener listener = new AgentBuilder.Listener() {
                @Override
                public void onDiscovery(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) {
                    System.out.println(s + " onDiscovery");
                }
    
                @Override
                public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b, DynamicType dynamicType) {
                    System.out.println(typeDescription + " onTransformation");
                }
    
                @Override
                public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b) {
                    System.out.println(typeDescription + " onIgnored");
                }
    
                @Override
                public void onError(String s, ClassLoader classLoader, JavaModule javaModule, boolean b, Throwable throwable) {
                    throwable.printStackTrace();
                }
    
                @Override
                public void onComplete(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) {
                    System.out.println(s + " onComplete");
                }
            };
    
            new AgentBuilder
                    .Default()
                    .type(ElementMatchers.nameStartsWith("buddy.say"))
                    .transform(transformer)
                    .with(listener)
                    .installOn(instrumentation);
    
    
        }
    }
    
    
    • 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
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    拦截器处理类

    package buddy.agent;
    
    import net.bytebuddy.implementation.bind.annotation.Origin;
    import net.bytebuddy.implementation.bind.annotation.RuntimeType;
    import net.bytebuddy.implementation.bind.annotation.SuperCall;
    
    import java.lang.reflect.Method;
    import java.util.concurrent.Callable;
    
    public class MethodTimeCheck {
    
        @RuntimeType
        public static void interceptor(@Origin Method method, @SuperCall Callable<?> callable) {
            long startTime =System.currentTimeMillis();
            try {
                callable.call();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println(method + " cost " + (System.currentTimeMillis() - startTime) + " ms");
            }
        }
    
    }
    
    
    • 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
    1. maven 打包,然后在VM option 中添加 -javaagent:XXX(your path).jar=args
    2. 测试类
    package buddy.say;
    
    public class HelloMethod {
    
        public void say() {
            try {
                Thread.sleep(1000);
                System.out.println("execute OK~");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    package buddy;
    
    import buddy.say.HelloMethod;
    
    public class StartExecute {
    
        public static void main(String[] args) {
            HelloMethod helloMethod = new HelloMethod();
            helloMethod.say();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    结束 ~ ~ (^ v ^)~ ~

  • 相关阅读:
    JavaScript 52 JavaScript this 关键词
    【从零开始学习 SystemVerilog】6.5、SystemVerilog 接口—— Clocking Blocks(上)
    Shader中需要数学知识
    2.2.2 部署Master节点、添加Node节点
    【opencv3】PnP测距(完整流程附C++代码)
    大数据开发(Hadoop面试真题-卷九)
    泰语快速学习方法!速成方法学习!
    【吴恩达机器学习笔记】七、神经网络
    空间复杂度(数据结构)
    学生家乡网页设计作品静态HTML网页模板源码 广西旅游景点网页设计 大学生家乡主题网站制作 简单家乡介绍网页设计成品
  • 原文地址:https://blog.csdn.net/qq_38746380/article/details/132913323