美文网首页
java-Agent-demo

java-Agent-demo

作者: tianlang136520 | 来源:发表于2018-05-01 23:13 被阅读129次
    public class HelloWorld {
        public static void main(String arg[]) {
            System.out.println("The age is 9");
        }
    }
    
    
    import java.lang.instrument.Instrumentation;
    
    public class MySimpleAgent {
        public static void premain(String agentArgs,Instrumentation inst) {
            inst.addTransformer(new MySimpleTransformer());
        }
    }
    
    import java.lang.instrument.ClassFileTransformer;
    import java.lang.instrument.IllegalClassFormatException;
    import java.security.ProtectionDomain;
    
    public class MySimpleTransformer implements ClassFileTransformer {
        public byte[] transform(ClassLoader classloader,
                                String classname,
                                Class redefinedclass,
                                ProtectionDomain protectiondomain,
                                byte b[]) throws IllegalClassFormatException {
            if(!classname.endsWith("HelloWorld"))
                return(null);
    
            String line = "";
            for(int i=0; i < b.length;i++){
                line += Byte.toString(b[i]) + " ";
                if(line.length() > 60) {
                    System.out.println(line);
                    line = "";
                }
                if(b[i] == (byte)'9')
                    b[i] = (byte)'0';
            }
            System.out.println(line);
            System.out.println("The number of bytes in HelloWorld: " + b.length);
            return(b);
        }
    }
    

    MANIFEST.MF :(此处有坑,:号后要有空格,否则打jar包回报错!)

    Manifest-Version: 1.0
    Premain-Class: MySimpleAgent
    Created-By: 1.8.0_151 (Oracle Corporation)
    
    打包报错:
    java.io.IOException: invalid header field
        at java.util.jar.Attributes.read(Attributes.java:410)
        at java.util.jar.Manifest.read(Manifest.java:199)
        at java.util.jar.Manifest.<init>(Manifest.java:69)
        at sun.tools.jar.Main.run(Main.java:176)
        at sun.tools.jar.Main.main(Main.java:1288)
    

    插播一条命令:删除.class 结尾:

    find . -name "*.class" | xargs rm
    

    打包命令:

    javac *.java
    

    打Agent jar包

    jar cvfm MySimpleAgent.jar MANIFEST.MF MySimpleAgent.class MySimpleTransformer.class
    

    运行HelloWorld

    java -javaagent:MySimpleAgent.jar HelloWorld
    

    运行结果:

     ~/dev/my-process/my-dev-doc/agent  java -javaagent:MySimpleAgent.jar HelloWorld
    objc[52894]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin/java (0x10eb484c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10ebd44e0). One of the two will be used. Which one is undefined.
    -54 -2 -70 -66 0 0 0 52 0 29 10 0 6 0 15 9 0 16 0 17 8 0 18 10
    0 19 0 20 7 0 21 7 0 22 1 0 6 60 105 110 105 116 62 1 0 3 40
    41 86 1 0 4 67 111 100 101 1 0 15 76 105 110 101 78 117 109 98
    101 114 84 97 98 108 101 1 0 4 109 97 105 110 1 0 22 40 91 76
    106 97 118 97 47 108 97 110 103 47 83 116 114 105 110 103 59
    41 86 1 0 10 83 111 117 114 99 101 70 105 108 101 1 0 15 72 101
    108 108 111 87 111 114 108 100 46 106 97 118 97 12 0 7 0 8 7
    0 23 12 0 24 0 25 1 0 12 84 104 101 32 97 103 101 32 105 115
    32 57 7 0 26 12 0 27 0 28 1 0 10 72 101 108 108 111 87 111 114
    108 100 1 0 16 106 97 118 97 47 108 97 110 103 47 79 98 106 101
    99 116 1 0 16 106 97 118 97 47 108 97 110 103 47 83 121 115 116
    101 109 1 0 3 111 117 116 1 0 21 76 106 97 118 97 47 105 111
    47 80 114 105 110 116 83 116 114 101 97 109 59 1 0 19 106 97
    118 97 47 105 111 47 80 114 105 110 116 83 116 114 101 97 109
    1 0 7 112 114 105 110 116 108 110 1 0 21 40 76 106 97 118 97
    47 108 97 110 103 47 83 116 114 105 110 103 59 41 86 0 33 0 5
    0 6 0 0 0 0 0 2 0 1 0 7 0 8 0 1 0 9 0 0 0 29 0 1 0 1 0 0 0 5
    42 -73 0 1 -79 0 0 0 1 0 10 0 0 0 6 0 1 0 0 0 1 0 9 0 11 0 12
    0 1 0 9 0 0 0 37 0 2 0 1 0 0 0 9 -78 0 2 18 3 -74 0 4 -79 0 0
    0 1 0 10 0 0 0 10 0 2 0 0 0 3 0 8 0 4 0 1 0 13 0 0 0 2 0 14
    The number of bytes in HelloWorld: 426
    The age is 0
     ~/dev/my-process/my-dev-doc/agent 
    

    age 由 9 更改成 0 !

    所有文件如图:


    image.png

    相关文章

      网友评论

          本文标题:java-Agent-demo

          本文链接:https://www.haomeiwen.com/subject/tmofrftx.html