Java ClassAdapter-class And Method Code Example

In ASM, a ClassAdapter is a class that can be used to modify the bytecode of an existing Java class. It works by allowing you to override certain methods that are called when the bytecode for different parts of the class is visited.

Here is an example of how you might use a ClassAdapter to add a new field to an existing class:

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.ClassAdapter;

public class Example {
    public static void main(String[] args) throws Exception {
        // Read the existing class file as a byte array
        byte[] bytes = readClassFile();

        // Use the ClassReader to parse the bytecode
        ClassReader cr = new ClassReader(bytes);

        // Create a ClassWriter that will be used to generate the modified bytecode
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

        // Create a ClassAdapter that will override the visitField method
        ClassAdapter classAdapter = new ClassAdapter(cw) {
            public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
                // Create a new FieldVisitor that will visit the field
                FieldVisitor fv = super.visitField(access, name, desc, signature, value);

                // Check if the field is the one we want to modify
                if (name.equals("fieldName")) {
                    // Add a new field with the same name and type as the original field
                    FieldVisitor newFv = cv.visitField(ACC_PUBLIC, "newField", Type.getDescriptor(int.class), null, null);

                return fv;

        // Have the ClassReader accept the ClassAdapter, which will cause it to call the overridden methods
        cr.accept(classAdapter, ClassReader.EXPAND_FRAMES);

        // Generate the modified bytecode as a byte array
        byte[] modifiedBytes = cw.toByteArray();

In this example, the ClassReader is used to parse the existing class file and the ClassWriter is used to generate the modified bytecode. The ClassAdapter overrides the visitField method and adds a new field with the same name and type as the original field. When the accept method is called on the ClassReader, it will call the appropriate methods on the ClassAdapter, which will in turn generate the modified bytecode.