java agent 使用及实现代码

时间:2022-09-22 16:42:33

java agent的作用

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

如何实现

1.实现javaagent需要实现premain方法
2.必须在manifest.mf文件中有premain-class

demo实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
    }
  }

预加载类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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指定预加载类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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选项。

java agent 使用及实现代码

总结

以上所述是小编给大家介绍的java agent 使用及实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://segmentfault.com/a/1190000015765838