Java ASM 简单例子

###Test的代码比较简单 public class Test { /** * @param args */ public static void main(String[] args) { test01(); }

1static void test01() { 2 System.out.println("Hello In Test01!"); 3} 4 5}

###对Test.class进行处理,添加test03的生成及调用 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

1import org.objectweb.asm.ClassAdapter; 2import org.objectweb.asm.ClassReader; 3import org.objectweb.asm.ClassVisitor; 4import org.objectweb.asm.ClassWriter; 5import org.objectweb.asm.MethodAdapter; 6import org.objectweb.asm.MethodVisitor; 7import org.objectweb.asm.Opcodes; 8public class Test_08 { 9 /** 10 * @param args 11 * @throws IOException 12 */ 13 public static void main(String[] args) throws IOException { 14 FileInputStream fis = new FileInputStream("D:\\Test.class"); 15 ClassReader reader = new ClassReader(fis); 16 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 17 reader.accept(new MyClassVisivator(cw), 0); 18 writeToFile(cw.toByteArray(), "D:\\Test.class"); 19 } 20 21 static void writeToFile(byte[] bytes, String fileName) { 22 try { 23 (new File(fileName)).createNewFile(); 24 } catch (IOException e1) { 25 e1.printStackTrace(); 26 } 27 FileOutputStream fos = null; 28 try { 29 fos = new FileOutputStream(fileName); 30 fos.write(bytes); 31 fos.flush(); 32 } catch (Exception e) { 33 }finally{ 34 try { 35 if (fos != null) { 36 fos.close(); 37 } 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 44 static class MyClassVisivator extends ClassAdapter { 45 ClassVisitor mCv; 46 public MyClassVisivator(ClassVisitor cv) { 47 super(cv); 48 mCv = cv; 49 } 50 51 @Override 52 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { 53 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); 54 if ("main".equals(name)) { 55 MyMethodVisitor mmv = new MyMethodVisitor(mv); 56 return mmv; 57 } 58 return mv; 59 } 60 61 @Override 62 public void visitEnd() { 63 MethodVisitor mv = mCv.visitMethod(Opcodes.ACC_STATIC, "test03", "()V", null, null); 64 if (mv != null) { 65 mv.visitCode(); 66 mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 67 mv.visitLdcInsn("Hello In Test03!"); 68 mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); 69 mv.visitInsn(Opcodes.RETURN); 70 mv.visitMaxs(0, 0); 71 mv.visitEnd(); 72 } 73 } 74 } 75 76 static class MyMethodVisitor extends MethodAdapter { 77 MethodVisitor mMv; 78 public MyMethodVisitor(MethodVisitor mv) { 79 super(mv); 80 mMv = mv; 81 } 82 83 @Override 84 public void visitInsn(int opcode) { 85 if (opcode == Opcodes.RETURN) { 86 System.out.println("Debug"); 87 mv.visitMethodInsn(Opcodes.INVOKESTATIC, "Test", "test03", "()V"); 88 } 89 super.visitInsn(opcode); 90 } 91 92 @Override 93 public void visitEnd() { 94 super.visitEnd(); 95 } 96 } 97}

相关jar包

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java第二次动手动脑

importjava.math.BigInteger;importjava.util.Scanner;publicclassCalculateN{/\\\@paramargs\/publicstaticvoidmain(String\\args){System.out.print

Java斜杠与反斜杠

直接通过正则表达式进行替换即可(备注:反斜杠为"",正斜杠为"/"),代码如下:publicclassTest{publicstaticvoidmain(String\\args){Stringpath"D:\\FTP\\admin\\bird.gif";//文件路径,双斜杠输出的是一个斜杠System.out.print

JVM调优YoungGC

先上代码:主函数:1.publicclassGCDemo{3.publicstaticvoidmain(String\\args)throwsInterruptedException{6.List<GCDataObjectlistnewArrayList<GCDat