Java-Changing code behavior outside the program

Source: Internet
Author: User

The title is a bit big, in fact the main java.lang.instrument under the content.



Previously written <jrebel-using JRebel to improve development efficiency > When you start a project with Maven, you have a parameter that is "-javaagent:d:\jrebel_5.6.0\jrebel.jar" when you add the VM parameter.

What is Javaagent? After java-help see the following information, the equivalent of nothing said ... :

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/47/75/wKiom1P6-yuxJJ9pAABS3osKHBw562.jpg "title=" Javaagent.jpg "alt=" Wkiom1p6-yuxjj9paabs3oskhbw562.jpg "/>



To say that the-javaagent parameter has to mention the Java proxy.

The Java proxy is not part of the application, and the Java Proxy uses instrumentation to redefine the running service.

We can change the running Java program with the instrumentation API without modifying the program code.

and the following section of the command we use is used to get the agent to work:

-javaagent:jarpath[=options]



The parameter value is a jar path, let's first try to write a proxy and export it as a jar.
Given the fact that there is no practical purpose, use the Java agent as a priority and try to write it as simple as possible.
The so-called Agent code implementation is a class with the Premain () method, but this method is not based on an abstraction, but is abruptly defined in the class.

Package Pac.testcase.basic.agent;import Java.lang.instrument.instrumentation;public class Testagent {public static VO    ID Premain (String agentargs, Instrumentation inst) {System.out.println ("Alvez Agent:::"); }}



Export the testagent as a jar, assuming my export path is "D:/myagent.jar".

Write a Main method, add the parameter "-javaagent:d:/myagent.jar" and run:

Package Pac.testcase.basic.agent;public class Testagentmain {public static void main (string[] args) throws interrupted    Exception {System.out.println ("code in Runtime::"); }}



Results hint:

Error occurred during initialization of VMS

Agent Library failed to init:instrument

Failed to find Premain-class manifest attribute in D:/myagent.jar


This requires adding a property in the MANIFEST.MF, such as this example to read as follows:

manifest-version:1.0

Premain-class:pac.testcase.basic.agent.testagent

Can-redefine-classes:true


Then you can run it, and the result output:

Alvez Agent:::

Code in Runtime::



This step is simply a way to execute the agent before the main method executes, a bit like the predecessor advice in interceptor or AOP.

I didn't want to start by saying "redefine a running service with instrumentation."

Yes, the key is instrumentation, this time try to use it to change the behavior of the main method.



We can add a classfiletransformer for instrumentation.

Classfiletransformer interface has no other level, the following is the exclusively concrete ...

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/47/7B/wKioL1P7GXSza-wUAACj6CxWF70535.jpg "title=" Concrete.jpg "alt=" Wkiol1p7gxsza-wuaacj6cxwf70535.jpg "/>



I also only provide a concrete, the Testagentmain.class in the code in runtime into the Alvez in runtime, as follows:

package pac.testcase.basic.agent;import java.lang.instrument.classfiletransformer;import  Java.lang.instrument.illegalclassformatexception;import java.security.protectiondomain;public class  TestTransformer implements ClassFileTransformer {     @Override      public byte[] transform (classloader loader, string classname,  class<?> classbeingredefined, protectiondomain protectiondomain, byte[]  Classfilebuffer)  throws IllegalClassFormatException {         if (Classname.equals ("Pac/testcase/basic/agent/testagentmain")) {             classfilebuffer = new string (ClassfileBuffer). Replace ("code "," Alvez "). GetBytes ();             System.out.println (New string (ClassfilebuFfer));         }        return  new byte[0];    }}



Do not forget to add the implemented Classfiletransformer to the instrumentation:

public static void Premain (String Agentargs, Instrumentation inst) {Inst.addtransformer (New Testtransformer ()); System.out.println ("Alvez Agent:::");}



Run output:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/47/7D/wKioL1P7I1DD75RRAAEordZDt2U145.jpg "title=" Result.jpg "alt=" Wkiol1p7i1dd75rraaeordzdt2u145.jpg "/>


This article is from the Alvez. -99.9% 0b/s "blog, be sure to keep this source http://alvez.blog.51cto.com/7711135/1544876

Java-Changing code behavior outside the program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.