使用bytebuddy构建agent

使用固定agent

引入pom

 
            net.bytebuddy
            byte-buddy
            1.4.16
        
        
            net.bytebuddy
            byte-buddy-agent
            1.4.16
        

实例

public static void installAgent(){
        ByteBuddyAgent.install();
        AgentBuilder agentBuilder = new AgentBuilder.Default()
                .type(nameStartsWith("com.codecraft.demo")
                        .and(not(isInterface()))
                        .and(not(isStatic()))
                .transform((builder,typeDescription,classLoader) -> builder
                        .method(ElementMatchers.any())
                        .intercept(MethodDelegation.to(TraceInterceptor.class)
                );
agentBuilder.installOnByteBuddyAgent();

使用自己构建的agent

    public static void premain(String argument, Instrumentation inst) {
        System.out.println("start premain)");

        new AgentBuilder.Default()
                .type(nameStartsWith("com.codecraft.demo").and(not(isInterface())).and(not(isStatic())))
                .transform((builder,typeDescription,classLoader) -> builder
                        .method(ElementMatchers.any())
                        .intercept(MethodDelegation.to(TraceInterceptor.class)
                )
                ).with(new AgentBuilder.Listener(){

            @Override
            public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, DynamicType dynamicType) {

            }

            @Override
            public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {

            }

            @Override
            public void onError(String s, ClassLoader classLoader, JavaModule javaModule, Throwable throwable) {
                throwable.printStackTrace();
            }

            @Override
            public void onComplete(String s, ClassLoader classLoader, JavaModule javaModule) {

            }
        })
                .installOn(inst);
    }

pom


                org.apache.maven.plugins
                maven-compiler-plugin
                
                
                    ${java.version}
                    ${java.version}
                    ${project.build.sourceEncoding}
                
            

                org.apache.maven.plugins
                maven-assembly-plugin
                2.6
                
                    
                        jar-with-dependencies
                    
                    ${project.artifactId}-${project.version}
                    false
                    
                        
                            ${premain}
                            ${premain}
                        
                    
                
                
                    
                        assemble-all
                        package
                        
                            single
                        
                    
                
            

docs

  • 通过使用Byte Buddy,便捷地创建Java Agent
  • bytebuddy-tutorial

你可能感兴趣的:(jvm,java)