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. 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);


    }
}

拦截器处理类

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. 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();
        }
    }
}

package buddy;

import buddy.say.HelloMethod;

public class StartExecute {

    public static void main(String[] args) {
        HelloMethod helloMethod = new HelloMethod();
        helloMethod.say();
    }
}

byte buddy字节码增强——输出方法执行时间_第1张图片

结束 ~ ~ (^ v ^)~ ~

你可能感兴趣的:(Java,字节码增强,java,byte,buddy,字节码增强,打印函数的执行时间,动态代理)