javassist和asm比较

Javassist:

• Javassist (Java Programming Assistant) makes Java bytecode manipulation simple. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it. Unlike other similar bytecode editors
• Javassist provides two levels of API: source level and bytecode level. If the users use the source-level API, they can edit a class file without knowledge of the specifications of the Java bytecode. The whole API is designed with only the vocabulary of the Java language. You can even specify inserted bytecode in the form of source text; Javassist compiles it on the fly. On the other hand, the bytecode-level API allows the users to directly edit a class file as other editors.
• Javassist lets you inspect, edit, and create Java binary classes.
• Javassist isn’t the only library for working with bytecode, but it does have one feature in particular that makes it a great starting point for experimenting with classworking: you can use Javassist to alter the bytecode of a Java class without actually needing to learn anything about bytecode or the Java virtual machine (JVM) architecture.
• Aspect Oriented Programming: Javassist can be a good tool for adding new methods into a class and for inserting before/after/around advice at the both caller and callee sides.
• Reflection: One of applications of Javassist is runtime reflection; Javassist enables Java programs to use a metaobject that controls method calls on base-level objects. No specialized compiler or virtual machine are needed.
• Javassist also provides lower-level API for directly editing a class file. To use this level of API, you need detailed knowledge of the Java bytecode and the class file format while this level of API allows you any kind of modification of class files.

ASM:

• ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or dynamically generate classes, directly in binary form. Provided common transformations and analysis algorithms allow to easily assembling custom complex transformations and code analysis tools.
• ASM offer similar functionality as other bytecode frameworks, but it is focused on simplicity of use and performance. Because it was designed and implemented to be as small and as fast as possible, it makes it very attractive for using in dynamic systems*.
• ASM is a Java class manipulation tool designed to dynamically generate and manipulate Java classes, which are useful techniques to implement adaptable systems. ASM is based on a new approach, compared to equivalent existing tools, which consists in using the “visitor” design pattern without explicitly representing the visited tree with objects. This new approach gives much better performances than those of existing tools, for most of practical needs.

Comparison between Javassist & ASM:

• Javassist source level API is much easier to use than the actual bytecode manipulation in ASM
• Javassist provides a higher level abstraction layer over complex bytecode level operations. Javassist source level API requires very less or no knowledge of actual bytecodes, so much easier & faster to implement.
• Javassist uses reflection mechanism which makes it slower compared to ASM which uses Classworking techniques at runtime.
• Overall ASM is much faster & gives better performance than Javassist. Javassist uses a simplified version of Java source code, which it then compiles into bytecode. That makes Javassist very easy to use, but it also limits the use of bytecode to what can be expressed within the limits of the Javassist source code.
• In conclusion, if someone needs easier way of dynamically manipulating or creating Java classes Javassist API should be used & where the performance is the key issue ASM library should be used.

Table 1. Class construction times

Framework First time Later times
Javassist 257 5.2
BCEL 473 5.5
ASM 62.4 1.1
The Table 1 results show that ASM does live up to its billing as faster than the other frameworks, and this advantage applies both to startup time and to repeated uses.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavassistASM都是Java字节码操作库,它们都提供了字节码的修改和生成功能,但是它们的API和使用方式都有所不同。如果要将Javassist代码转换为ASM代码,需要先了解ASM的API和使用方式。 下面是一个简单的示例,演示如何将Javassist代码转换为ASM代码: ```java // Javassist代码 ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.get("com.example.MyClass"); CtMethod ctMethod = ctClass.getDeclaredMethod("myMethod"); ctMethod.insertBefore("System.out.println(\"Before\");"); // 转换为ASM代码 ClassReader classReader = new ClassReader("com.example.MyClass"); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor classVisitor = new MyClassVisitor(classWriter); classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES); // 自定义ClassVisitor class MyClassVisitor extends ClassVisitor { public MyClassVisitor(ClassWriter classWriter) { super(Opcodes.ASM5, classWriter); } @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("myMethod")) { methodVisitor = new MyMethodVisitor(methodVisitor); } return methodVisitor; } } // 自定义MethodVisitor class MyMethodVisitor extends MethodVisitor { public MyMethodVisitor(MethodVisitor methodVisitor) { super(Opcodes.ASM5, methodVisitor); } @Override public void visitCode() { super.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Before"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } } ``` 上述代码中,我们首先使用Javassist修改了`com.example.MyClass`类中的`myMethod`方法,在方法体的开头插入了一行输出语句。然后使用ASM将该类转换为字节码,并使用自定义的`ClassVisitor`和`MethodVisitor`访问字节码,将输出语句的Javassist代码转换为相应的ASM代码。 需要注意的是,JavassistASM的API和语法不同,因此转换时需要仔细处理。同时,ASM在性能和灵活性方面都比Javassist更优秀,因此在需要高性能和灵活性的场景下,建议使用ASM
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值