1

java agent的作用

在字节码这个层面对类和方法进行修改的技术,能够在不影响编译的情况下,修改字节码。可以理解spring的aop技术

如何实现

  1. 实现javaagent需要实现premain方法
  2. 必须在MANIFEST.MF文件中有Premain-Class

demo实现

agent

  package com.xueyou.demo.agent;
     
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.CtNewMethod;
     
    import java.lang.instrument.ClassFileTransformer;
    import java.lang.instrument.IllegalClassFormatException;
    import java.security.ProtectionDomain;
     
    public class FirstAgent implements ClassFileTransformer {
        public final String injectedClassName = "com.xueyou.agentdemo.App";
        public final String methodName = "hello";
     
        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            className = className.replace("/", ".");
    //        System.out.println(className);
            if (className.equals(injectedClassName)) {
                CtClass ctclass = null;
                try {
                    ctclass = ClassPool.getDefault().get(className);// 使用全称,用于取得字节码类<使用javassist>
                    CtMethod ctmethod = ctclass.getDeclaredMethod(methodName);// 得到这方法实例
                    ctmethod.insertBefore("System.out.println(11111111);");
                    return ctclass.toBytecode();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

预加载类

package com.xueyou.demo;
 
import com.xueyou.demo.agent.FirstAgent;
 
import java.lang.instrument.Instrumentation;
 
/**
 * Hello world!
 */
public class App {
    public static void premain(String agentOps, Instrumentation inst) {
        System.out.println("=========premain方法执行========");
        System.out.println(agentOps);
        // 添加Transformer
        inst.addTransformer(new FirstAgent());
    }
}

maven指定预加载类

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                       <PremainClass>com.xueyou.demo.App</Premain-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

如何使用

这里有两种方式指定javaagent:
1)使用命令行:java -javaagent:XXX.jar ddd.jar
2)使用idea的vm option选项。

clipboard.png

转: https://blog.csdn.net/wild46c...


木木甫
494 声望23 粉丝

已工作的应届生。希望能和大家多多交流技术问题,