Master the ASM Core framework: Easily implement the byte code operation of the Java class library
Master the ASM Core framework: Easily implement the byte code operation of the Java class library
Summary: ASM is a powerful Java bytecode operation framework that can perform a more underlying operation on the Java file without modifying the source code.This article will guide readers to master the basic knowledge of the ASM Core framework and provide some Java code examples to help readers better understand and apply the ASM Core framework.
introduction:
In the development of Java, sometimes we need to perform more underlying operations on the Java files, such as modifying the method, attributes, annotations and other information during runtime.Under normal circumstances, these operations can be achieved through the reflection mechanism, but the efficiency of reflection is low and it is not applicable in some scenarios.At this time, the ASM framework can play a role.
1. What is ASM?
ASM (full name: very small meta language library, in fact, is not a new language, but a set of API collection of Java bytecode operation. The main features are as follows:
-Al lightweight: ASM framework contains only a few core classes and interfaces, which is very lightweight and easy to use.
-The high performance: The design of the ASM framework considers performance issues and can operate the byte code efficiently at runtime.
-The flexibility: The ASM framework can be suitable for different Java versions, and it provides rich API for developers.
2. The composition of the ASM core framework
ASM Core framework consists of the following core components:
-ClassReader: Used to read the compiled Java files and analyze its structure.
-ClassWriter: The byte code for generating new Java files.
3. Use ASM Core framework to operate byte code operation
Below we use some specific examples to demonstrate how to use the ASM Core framework for bytecode operation.
Example 1: Add a new method
import org.objectweb.asm.*;
class MyClassVisitor extends ClassVisitor {
public MyClassVisitor(int api, ClassVisitor cv) {
super(api, cv);
}
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (name.equals("existingMethod")) {
MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
// Generate a new method
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("Hello, ASM!");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
public class MyTransformer {
public byte[] transform(byte[] classBytes) {
ClassReader cr = new ClassReader(classBytes);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
MyClassVisitor visitor = new MyClassVisitor(Opcodes.ASM8, cw);
cr.accept(visitor, 0);
return cw.toByteArray();
}
}
Example 2: Modify an existing method
import org.objectweb.asm.*;
class MyClassVisitor extends ClassVisitor {
public MyClassVisitor(int api, ClassVisitor cv) {
super(api, cv);
}
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (name.equals("existingMethod")) {
MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
// Modify the byte code of the existing method
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("Hello, ASM!");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
public class MyTransformer {
public byte[] transform(byte[] classBytes) {
ClassReader cr = new ClassReader(classBytes);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
MyClassVisitor visitor = new MyClassVisitor(Opcodes.ASM8, cw);
cr.accept(visitor, 0);
return cw.toByteArray();
}
}
in conclusion:
By using the ASM Core framework, we can easily implement the bytecode operation of the Java class library.Whether it is adding a new method, modifying existing methods or other more underlying operations, the ASM Core framework can meet our needs.Mastering the ASM Core framework can make us more flexibly operate the byte code and improve the performance and functions of the Java application.
reference:
1. ASM official website: https://asm.ow2.io/
2. "ASM 5 series tutorial" by Eric Bruno
The above is a knowledge article about "mastering the ASM Core framework: Easily realizing the bytecode operation of the Java class library". I hope this article can help readers in -depth understanding of the basic knowledge of the ASM Core framework and be able to use it flexibly in practice.