Dynamic generation class of Java Inverse Foundation

Source: Internet
Author: User
Tags modifier stringbuffer

Why this thing, on the one hand, the need for AOP framework, on the other hand, increases the difficulty of software reverse

The technology of dynamically generated classes is broadly divided into two categories, one is through the operation of bytecode framework such as cglib/javassist to achieve, the other is the JNI way, call Dll/so Library, in-memory dynamic restore. Both of these methods can implement hidden classes

See an example of a javassist dynamically generated class

Package com.vvvtimes;import java.lang.reflect.modifier;import javassist. Classpool;import javassist. Ctclass;import javassist. Ctconstructor;import javassist. Ctfield;import javassist. Ctmethod;import javassist. Ctnewmethod;public class dynamicgenerateclass {public static void main (String[]  args)  throws exception {// classpool:ctclass The container of the object classpool pool =  Classpool.getdefault ();//  generates a new public class by Classpool employee.javactclass ctclass =  Pool.makeclass ("Com.vvvtimes.bean.Employee");//  add field//  first add field Private string enamectfield  enamefield = new ctfield (Pool.getctclass ("java.lang.String"),  "ename",  ctclass) ; Enamefield.setmodifiers (modifier.private); Ctclass.addfield (Enamefield);//  next Add Field privtae int  Eagectfield eagefield = new ctfield (Pool.getctclass ("int"),  "Eage",  ctClass); EagefIeld.setmodifiers (modifier.private); Ctclass.addfield (Eagefield);//  next Add Field privtae int  Eagectfield esexfield = new ctfield (Pool.getctclass ("int"),  "Esex",  ctClass); Esexfield.setmodifiers (modifier.private); Ctclass.addfield (Esexfield);//  Add getter and Setter methods Ctclass.addmethod for fields Ename and Eno (Ctnewmethod.getter ("Getename",  enamefield)); Ctclass.addmethod (Ctnewmethod.setter ("Setename",  enamefield)) Ctclass.addmethod (CtNewMethod.getter (" Geteage ",  eagefield)), Ctclass.addmethod (Ctnewmethod.setter (" Seteage ",  eagefield)); CtClass.addMethod ( Ctnewmethod.getter ("Getsex",  esexfield)); Ctclass.addmethod (Ctnewmethod.setter ("Setsex",  esexField)) ;//  Add constructor Ctconstructor ctconstructor = new ctconstructor (new CtClass[] {},  ctclass);//  sets the function Body Stringbuffer buffer = new stringbuffer () for the constructor function, Buffer.append ("{ \ n "). Append (" ename=\ "gsls200808\" \ n "). Append (" eage=25;\n "). Append (" esex=1;\n} "); Ctconstructor.setbody (Buffer.tostring ());//  add the constructor to the new class Ctclass.addconstructor (ctconstructor);//   Add Custom method Ctmethod ctmethod = new ctmethod (ctclass.voidtype,  "PrintInfo",  new  ctclass[] {}, ctclass);//  for custom method set modifier ctmethod.setmodifiers (modifier.public);//  Set the function Body Stringbuffer buffer2 = new stringbuffer () for the custom method, Buffer2.append ("{\ n"). Append (" System.out.println (\ "begin!\"); \ n "). Append (" System.out.println (\ "name=\" +ename); \ n "). Append (" System.out.println (\ "age=\" +eage); \ n "). Append (" System.out.println (\ "sex=\" +esex); \ n "). Append (" System.out.println (\ "end!\"); \ n "). Append ("} "); Ctmethod.setbody (buffer2.tostring ()); Ctclass.addmethod (Ctmethod) ;//  to verify the effect, the following uses the reflection execution method Printinfoclass<?> clazz = ctclass.toclass (); object obj  = clazz.newinstance (); Obj.getclass (). GetMethod ("Printinfo",  new class[] {}). Invoke ( obj, new object[] {});}}

Third-party jar:javassist-3.20.0-ga.jar that need to be referenced

Run results

begin!name=gsls200808age=25sex=1end!

The meaning of the code is equivalent to creating a class named Com.vvvtimes.bean.Employee in memory and invoking the Printinfo method for output by reflection.


The content of this class is roughly the following, except that we are dynamically generating

package com.vvvtimes.bean;import java.io.printstream;public class employee{   private string ename =  "gsls200808";  private int eage = 25;   private int esex = 1;    public string getename ( )   {    return this.ename;  }    public  Void setename (string paramstring)   {    this.ename =  Paramstring;  }    public int geteage ()   {     return this.eage;  }    public void seteage (int  Paramint)   {    this.eage = paramInt;  }     public int getsex ()   {    return this.esex;  }     public&Nbsp;void setsex (Int paramint)   {    this.esex = paramInt;   }    public void printinfo ()   {     System.out.println ("begin!");     system.out.println ("Name="  + this.ename);     System.out.println ("age="  + this.eage);     system.out.println ("sex="  +  this.esex);     system.out.println ("end!");   }}

In the next article we'll talk about how to get the in-memory class

Dynamic generation class of Java Inverse Foundation

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.