Java Dynamic Agent generates $PROXY0 source code operations

Source: Internet
Author: User

    1. $Proxy The class is dynamically generated and generated at run time. Specific source code extraction reference is: Http://www.cnblogs.com/ctgulong/p/5011614.html?utm_source=tuicool&utm_medium=referral blog


2. Write down the process of generating source code in detail according to the contents of the above blog. Refer to Previous project http://yinyueml.blog.51cto.com/4841237/1876473

1) First, you need a generator class for the agent. Javaagent main functions are as follows:

  • You can intercept the class file before loading it and modify the bytecode.

  • You can change the bytecode of a loaded class at run time, but there are a lot of limitations in this case, which will be explained in detail later

  • And a few other niche features.

    • Get all the classes that have been loaded, this feature allows us to obtain the class file of the generated classes of the dynamic proxy, which can be used to view the source code after being recompiled.

    • Gets all the classes that have been initialized (the Clinit method, which is a subset of the above)

    • Get the size of an object

    • Adding a jar to the bootstrap classpath as a high priority is bootstrapclassloader loaded

    • Add a jar to the classpath for Appclassloard to load

    • Set the prefix for some native methods, and make regular matches when looking for native methods

2) The code is as follows

package jdkreview;import java.io.file;import java.io.fileoutputstream;import  Java.lang.instrument.classfiletransformer;import java.lang.instrument.illegalclassformatexception;import  java.lang.instrument.Instrumentation;import java.security.ProtectionDomain;public class  Customagent implements classfiletransformer { //    public static  void premain (String agentargs, instrumentation inst)  {         inst.addtransformer (New customagent ());    }      @Override     public byte[] transform (classloader loader,  string classname, class<?> classbeingredefined,             protectiondomain protectiondomain, byte[] classfilebuffer)   Throws illegalclassformatexception {        if  (!classname.startswith ("Java")  & & !classname.startswith ("Sun"))  {             //  is neither Java nor Sun's beginning             //   Export Code             int lastindexof =  classname.lastindexof ("/")  + 1;             string filename = classname.substring (lastIndexOf)  +  ". Class";             exportclazztofile ("D:/javacode/",  Filename, classfilebuffer);             System.out.println (classname +  " --> exported succeess!");         }            return classfilebuffer;     }    /**     *      * @ param dirpath     * directory with/To end, and must exist      *  @param   filename     *  @param  data     */     private void exportclazztofile (string dirpath, string filename, byte [] data)  {        try {             file file = new file (dirPath +  FileName);            if  (!file.exists ())  {                 File.createnewfile ();                 }                 FileOutputStream fos =  New fileoutputstream (file);             Fos.write (data);             fos.close ();             } catch  (exception e)  {             system.out.println ("exception  Occured while doing some file operation ");             e.printstacktrace ();        }     }}

3) Export the customagent as a jar package and modify the contents of the MANIFEST.MF file as follows:

Manifest-version:1.0premain-class:jdkreview.customagent

This configuration is intended primarily for Premain-class to represent a class with a premain static method.

4) When running the client class using the Java Dynamic Agent, add the parameters of the JVM

-javaagent:d:/customagent.jar

The following results are then run:

Proxypattern/client--exported Succeess!proxypattern/gameplayer-exported succeess!proxypattern/ Igameplayer--exported Succeess!proxypattern/gameplayih-exported succeess!com/sun/proxy/$Proxy 0-- Exported succeess! someone re-login with my account login user name: yinyueml; Player: Zhang San online Zhang San is playing strange. Zhang San upgraded.

Now through

Exportclazztofile ("d:/javacode/", FileName, Classfilebuffer);

You can find the $proxy0.class file in the configuration path.

5) The source code can be obtained by jad.exe the $proxy0.class with the anti-compilation application.

Java Dynamic Agent generates $PROXY0 source code operations

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.