实现 修改 程序源代码 hello -> hello agented
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;
}
}
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();
}
}
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
复制MANIFEST.MF_Agent2
到META-INF
目录下。去掉后缀_Agent2
覆盖MANIFEST.MF文件
cd 【项目目录】JavaAgentDemo\agent
mvn clean package
启动demo2 设置JVM参数
-javaagent:【项目目录】JavaAgentDemo\agent\target/agent.jar=input