Skip to content

Latest commit

 

History

History
104 lines (78 loc) · 2.57 KB

JavaAgent-premain方法2-实现修改代码逻辑.md

File metadata and controls

104 lines (78 loc) · 2.57 KB

JavaAgent-premain方法 实现修改代码逻辑

效果:

实现 修改 程序源代码 hello -> hello agented

Agent2.java

public class Agent2 {
    /**
     * 可以运行在main方法启动前
     * @param agent             输入的参数
     * @param instrumentation             输入的参数
     */
    public static void premain(String agent, Instrumentation instrumentation){
        System.out.println("Agent2 premain 2param :" + agent);
        instrumentation.addTransformer(new ConsoleTransformer(),true);
    }

}

ConsoleTransformer.java

public class ConsoleTransformer implements ClassFileTransformer {
    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        if (className.equals("cn/bigfire/Console")){
            String root = StrUtil.subBefore(System.getProperty("user.dir"), "JavaAgentDemo", true);
            String classFile = root + "JavaAgentDemo/agent/src/main/resources/Console.class";
            return FileUtil.readBytes(classFile);
        }
        return classfileBuffer;
    }
}

Demo2.java

public class Demo2 {

    /**
     * VM参数
     * -javaagent:D:\desktop\text\code\mycode\JavaAgentDemo\agent\target/agent.jar=input
     * */
    public static void main(String[] args) throws Exception {
        new Thread(()->{
            while (true){
                Console.hello();//    public static void hello(){System.out.println("hello"); }
                ThreadUtil.sleep(2000);
            }
        }).start();
    }
}

resources/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dahuoyzs
Created-By: Apache Maven 3.6.0
Build-Jdk: 1.8.0_171
Premain-Class: cn.bigfire.Agent2
Can-Retransform-Classes: true

运行效果

Agent2 premain 2param :input
满足条件
hello  agented
hello  agented
hello  agented
hello  agented

测试Agent2

复制MANIFEST.MF_Agent2META-INF目录下。去掉后缀_Agent2覆盖MANIFEST.MF文件

cd 【项目目录】JavaAgentDemo\agent 
mvn clean package

overwrite

mavenpackage

启动demo2 设置JVM参数

-javaagent:【项目目录】JavaAgentDemo\agent\target/agent.jar=input

image-20200731201651853