Java ASM Simple Example

Source: Internet
Author: User

Test code is relatively simple
public class Test {/** * @param args */public static void main (string[] args) {    test01 ();} static void test01 () {    System.out.println ("Hello in test01!");}}
Processing Test.class, adding test03 generation and invocation
MV.VISITFIELDINSN (opcodes.getstatic, "Java/lang/system", "Out", "ljava/io/printstream;"); Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Org.objectweb.asm.classadapter;import Org.objectweb.asm.classreader;import Org.objectweb.asm.classvisitor;import Org.objectweb.asm.classwriter;import Org.objectweb.asm.methodadapter;import Org.objectweb.asm.MethodVisitor; Import Org.objectweb.asm.opcodes;public class Test_08 {/** * @param args * @throws IOException */Publ IC static void Main (string[] args) throws IOException {FileInputStream fis = new FileInputStream ("D:\\test.class")        ;        Classreader reader = new Classreader (FIS);        Classwriter cw = new Classwriter (classwriter.compute_frames);        Reader.accept (New Myclassvisivator (CW), 0);    WriteToFile (Cw.tobytearray (), "D:\\test.class"); } static void WriteToFile (byte[] bytes, String fileName) {try {(new File)]. CreatenewfiLe ();        } catch (IOException E1) {e1.printstacktrace ();        } FileOutputStream fos = null;            try {fos = new FileOutputStream (fileName);            Fos.write (bytes);        Fos.flush (); } catch (Exception e) {}finally{try {if (fos! = null) {Fos.close ()                ;            }} catch (IOException e) {e.printstacktrace ();        }}}} Static class Myclassvisivator extends Classadapter {classvisitor mCv;            Public Myclassvisivator (Classvisitor CV) {super (CV);        MCV = CV; } @Override public methodvisitor visitmethod (int access, string name, String desc, string signature, string[            ] exceptions) {methodvisitor mv = Super.visitmethod (access, name, desc, signature, exceptions);         if ("Main". Equals (name)) {Mymethodvisitor MMV = new Mymethodvisitor (MV);       return MMV;        } return MV; } @Override public void Visitend () {methodvisitor mv = Mcv.visitmethod (opcodes.acc_static, "test            "," () V ", NULL, NULL);                if (mv! = null) {Mv.visitcode ();                MV.VISITFIELDINSN (opcodes.getstatic, "Java/lang/system", "Out", "ljava/io/printstream;");                MV.VISITLDCINSN ("Hello in test03!");                MV.VISITMETHODINSN (opcodes.invokevirtual, "Java/io/printstream", "println", "(ljava/lang/string;) V");                MV.VISITINSN (Opcodes.return);                MV.VISITMAXS (0, 0);            Mv.visitend ();        }}}} Static class Mymethodvisitor extends Methodadapter {methodvisitor mMv;            Public Mymethodvisitor (Methodvisitor mv) {super (MV);        MMv = MV; } @Override public void visitinsn (int opcode) {if (opcode = = Opcodes.return) {Sy Stem.out.println ("Debug");            MV.VISITMETHODINSN (opcodes.invokestatic, "Test", "test03", "() V");        } super.visitinsn (opcode);        } @Override public void Visitend () {super.visitend (); }    }}

Related JAR Packages

Java ASM Simple Example

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.