首页 > 开发 > Java > 正文

java字节码框架ASM操作字节码的方法浅析

2024-07-13 10:03:19
字体:
来源:转载
供稿:网友

之前我们已经对ASM进行的详细的介绍,需要的朋友们可以点击这里:javascript/51233.html">java字节码框架ASM的深入学习

JVM的类型签名对照表

 

Type Signature Java Type
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L fully-qualified-class ;fully-qualified-class
[ type type[]
( arg-types ) ret-type method type

 

比如,java方法是

long f (int n, String s, int[] arr);

对应的类型签名就是

f (ILjava/lang/String;[I)J

再比如,java方法是

private void hi(double a, List<String> b);

那对应的类型签名就是

hi (DLjava/util/List;)V

接下来可以利用ASM进行验证上述两个类型签名是否正确:

public class Test { public static void main(String[] args) throws Exception { ClassPrinter printer = new ClassPrinter(); //读取静态内部类Bazhang ClassReader cr = new ClassReader("Test$Bazhang"); cr.accept(printer, 0); } //静态内部类 static class Bazhang { public Bazhang(int a) { } private long f (int n, String s, int[] arr){  return 0; } private void hi(double a, List<String> b){ } } static class ClassPrinter extends ClassVisitor { public ClassPrinter() {  super(Opcodes.ASM5); } @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {  super.visit(version, access, name, signature, superName, interfaces);  //打印出父类name和本类name  System.out.println(superName + " " + name); } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {  //打印出方法名和类型签名  System.out.println(name + " " + desc);  return super.visitMethod(access, name, desc, signature, exceptions); } }}

最后打印出来的内容:

java/lang/Object Test$Bazhang<init> ()Vf (ILjava/lang/String;[I)Jhi (DLjava/util/List;)V

验证了之前的正确性,其中可以看到默认构造函数也打印出来了。

那么接下来干点有意思的事,我们往Bazhang类里新增和方法,就定为:

public void newFunc(String str){ }

这个时候就需要用到ClassWriter了,用于拼接字节码,具体关于ClassReader、ClassVisitor、ClassWriter的文章可以查看这篇文章:ASM源码学习之ClassReader、ClassVisitor与ClassWriter详解

public static void main(String[] args) throws Exception { ClassReader cr = new ClassReader(Bazhang.class.getName()); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); cr.accept(cw, Opcodes.ASM5); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newFunc", "(Ljava/lang/String;)V", null, null); mv.visitInsn(Opcodes.RETURN); mv.visitEnd(); // 获取生成的class文件对应的二进制流 byte[] code = cw.toByteArray();  //将二进制流写到out/下 FileOutputStream fos = new FileOutputStream("out/Bazhang222.class"); fos.write(code); fos.close();}

这样就会在out/文件夹下生成Bazhang222.class:

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//import java.util.List;class Test$Bazhang { Test$Bazhang() { } private long f(int n, String s, int[] arr) { return 0L; } private void hi(double a, List<String> b) { } public void newFunc(String var1) { }}

结合之前整理的JVM指令集,使用ASM直接操作字节码也是没问题的,结尾附上ASM源码下载地址http://forge.ow2.org/projects/asm/

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表