• 大小: 265KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: Java
  • 标签: Java  JavaAgent  agentmian  

资源简介

JavaAgent技术的Demo,agentmain方式 采用Javaassist技术实现字节码修改,VirtualMachine技术实现运行时代码织入

资源截图

代码片段和文件信息

package com.reason.demo.attachagent;

import com.sun.tools.attach.VirtualMachine;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * Created by liuruisen on 2018/11/18.
 */
public class AgentAttach {

    private static String agentPath = “/Users/liuruisen/IdeaProjects/javaagent/target/javaagent-1.0-SNAPSHOT.jar“;

    public static void main(String[] args) {

        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class vmClass = null;
        try {
            vmClass = classLoader.loadClass(“com.sun.tools.attach.VirtualMachine“);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        object vmobject = null;
        try {
            vmobject = vmClass.getMethod(“attach“ String.class).invoke(null args[0]);

            VirtualMachine virtualMachine = (VirtualMachine) vmobject;
            System.out.println(virtualMachine.id());

            Method loadAgentMethod = vmClass.getMethod(“loadAgent“ String.class String.class);

            loadAgentMethod.invoke(vmobject agentPath ““);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace(System.out);
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } finally {
            if (null != vmobject) {
                try {
                    vmClass.getMethod(“detach“ (Class[]) null).invoke(vmobject
                            (object[]) null);
                    System.out.println(“Detach Success“);
                } catch (Exception e) {

                }
            }
        }

        try {
            while (true) {
                TimeUnit.SECONDS.sleep(10);
                System.out.println(“HHAHHAHAH“);
            }
        } catch (InterruptedException e) {
            if (null != vmobject) {
                try {
                    vmClass.getMethod(“detach“ (Class[]) null).invoke(vmobject
                            (object[]) null);
                    System.out.println(“Detach Success“);
                } catch (Exception exx) {

                }
            }
            e.printStackTrace();
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-02 16:49  demo-spring\
     文件        5810  2018-11-06 20:41  demo-spring\mvnw.cmd
     文件        8433  2018-11-28 22:47  demo-spring\demo-spring.iml
     文件        2991  2018-11-28 22:48  demo-spring\pom.xml
     文件         268  2018-11-06 20:41  demo-spring\.gitignore
     目录           0  2018-11-06 20:41  demo-spring\.mvn\
     目录           0  2018-11-06 20:41  demo-spring\.mvn\wrapper\
     文件         116  2018-11-06 20:41  demo-spring\.mvn\wrapper\maven-wrapper.properties
     文件       48337  2018-11-06 20:41  demo-spring\.mvn\wrapper\maven-wrapper.jar
     文件        9113  2018-11-06 20:41  demo-spring\mvnw
     目录           0  2018-12-02 16:50  demo-spring\.idea\
     文件         172  2018-11-06 20:42  demo-spring\.idea\encodings.xml
     目录           0  2018-11-06 20:41  demo-spring\.idea\inspectionProfiles\
     目录           0  2018-11-28 22:47  demo-spring\.idea\libraries\
     文件         673  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_12.xml
     文件         548  2018-11-28 22:47  demo-spring\.idea\libraries\Maven__org_mybatis_mybatis_3_4_6.xml
     文件         583  2018-11-06 20:43  demo-spring\.idea\libraries\Maven__org_xmlunit_xmlunit_core_2_6_2.xml
     文件         621  2018-11-11 17:23  demo-spring\.idea\libraries\Maven__org_apache_logging_log4j_log4j_api_2_11_1.xml
     文件         692  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml
     文件         656  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__org_jboss_logging_jboss_logging_3_3_2_Final.xml
     文件         671  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_databind_2_9_7.xml
     文件         678  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__org_springframework_spring_webmvc_5_1_2_RELEASE.xml
     文件         722  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_9_7.xml
     文件         685  2018-11-06 20:43  demo-spring\.idea\libraries\Maven__org_springframework_spring_context_5_1_2_RELEASE.xml
     文件         768  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__org_springframework_boot_spring_boot_starter_web_2_1_0_RELEASE.xml
     文件         664  2018-11-06 20:43  demo-spring\.idea\libraries\Maven__org_springframework_spring_test_5_1_2_RELEASE.xml
     文件         789  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__org_springframework_boot_spring_boot_starter_tomcat_2_1_0_RELEASE.xml
     文件         570  2018-11-11 17:24  demo-spring\.idea\libraries\Maven__com_fasterxml_classmate_1_4_0.xml
     文件         657  2018-11-06 20:43  demo-spring\.idea\libraries\Maven__org_springframework_spring_jcl_5_1_2_RELEASE.xml
     文件         580  2018-11-06 20:43  demo-spring\.idea\libraries\Maven__org_hamcrest_hamcrest_core_1_3.xml
     文件         616  2018-11-11 17:23  demo-spring\.idea\libraries\Maven__ch_qos_logback_logback_classic_1_2_3.xml
............此处省略148个文件信息

评论

共有 条评论