热卖商品
新闻详情
ASM框架研究_ayupch的博客-CSDN博客
来自 : CSDN技术社区
发布时间:2021-03-24
public static void main(String[] args) throws IOException { ClassWriter classWriter new ClassWriter(0); AddField addField new AddField(classWriter); ClassReader classReader new ClassReader( com.pch.asm.Remove ); classReader.accept(addField,0); FileOutputStream fileOutputStream new FileOutputStream( D://AddField.class ); fileOutputStream.write(classWriter.toByteArray());
添加属性后的class文件内容如下
package com.pch.asm;public class AddTest { public static String name; private int age; public int newAdd; public AddTest() { public int getAge() { return this.age; public static String getName() { return name; void print() { System.out.println(this.age);改变修改方法
例如 增加方法的执行耗时
public class DEMO { public static void changeClassMethod() { try { ClassReader reader new ClassReader( com.pch.asm.TestTime ); ClassWriter writer new ClassWriter(reader, ClassWriter.COMPUTE_MAXS); ClassVisitor change new ChangeVisitor(writer); reader.accept(change, ClassReader.EXPAND_FRAMES); try { FileOutputStream fos new FileOutputStream( D:/TestTime.class ); fos.write(writer.toByteArray()); fos.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); * 方法变化visitor private static class ChangeVisitor extends ClassVisitor { ChangeVisitor(ClassVisitor classVisitor) { super(Opcodes.ASM8, classVisitor); Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor methodVisitor super.visitMethod(access, name, desc, signature, exceptions); if (name.equals( init )) { return methodVisitor; return new ChangeAdapter(Opcodes.ASM8, methodVisitor, access, name, desc); * 使用AdviceAdapter要引入asm-commons * 方法体具体变化的操作都在这里操作 private static class ChangeAdapter extends AdviceAdapter { /** 参数的index */ private int startTimeId -1; /** 方法名 */ private String methodName; ChangeAdapter(int api, MethodVisitor mv, int access, String name, String desc) { super(api, mv, access, name, desc); methodName name; * 进入方法体时调用 Override protected void onMethodEnter() { super.onMethodEnter(); startTimeId newLocal(Type.LONG_TYPE); mv.visitMethodInsn(INVOKESTATIC, java/lang/System , currentTimeMillis , ()J , false); mv.visitIntInsn(LSTORE, startTimeId); * 退出方法体调用 * param opcode opcode Override protected void onMethodExit(int opcode) { super.onMethodExit(opcode); int durationId newLocal(Type.LONG_TYPE); mv.visitMethodInsn(INVOKESTATIC, java/lang/System , currentTimeMillis , ()J , false); mv.visitVarInsn(LLOAD, startTimeId); mv.visitInsn(LSUB); mv.visitVarInsn(LSTORE, durationId); mv.visitFieldInsn(GETSTATIC, java/lang/System , out , Ljava/io/PrintStream; ); mv.visitTypeInsn(NEW, java/lang/StringBuilder ); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, java/lang/StringBuilder , init , ()V , false); mv.visitLdcInsn( The cost time of methodName () is ); mv.visitMethodInsn(INVOKEVIRTUAL, java/lang/StringBuilder , append , (Ljava/lang/String;)Ljava/lang/StringBuilder; , false); mv.visitVarInsn(LLOAD, durationId); mv.visitMethodInsn(INVOKEVIRTUAL, java/lang/StringBuilder , append , (J)Ljava/lang/StringBuilder; , false); mv.visitLdcInsn( ms! ); mv.visitMethodInsn(INVOKEVIRTUAL, java/lang/StringBuilder , append , (Ljava/lang/String;)Ljava/lang/StringBuilder; , false); mv.visitMethodInsn(INVOKEVIRTUAL, java/lang/StringBuilder , toString , ()Ljava/lang/String; , false); mv.visitMethodInsn(INVOKEVIRTUAL, java/io/PrintStream , println , (Ljava/lang/String;)V , false);
通过上面的介绍我们已经对ASM有了一定了解。
ASM相关我们先介绍到这 有说的不准确的地方欢迎指正 下一篇文章我们介绍另一个字节码框架javassist。
本文链接: http://asmresearch.immuno-online.com/view-683708.html
发布于 : 2021-03-24
阅读(0)
最新动态
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
2021-03-24
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616