tinycodinglm / lang /lang-0036.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.55 kB
opencode trace
System You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.

You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.

User

How are sealed classes represented in bytecode? Show me permitted subclasses and the PermittedSubclasses attribute.

Assistant

Sealed classes use the PermittedSubclasses attribute in the class file:

import org.objectweb.asm.*;

public class SealedClassExample {
public static byte[] generateSealedClass() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V17, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_FINAL,
"com/example/Shape", null, "java/lang/Object", null);
cw.visitSource("Shape.java", null);

markdown
cw.visitPermittedSubclass("com/example/Circle");
    cw.visitPermittedSubclass("com/example/Rectangle");
    cw.visitPermittedSubclass("com/example/Triangle");

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "area", "()D", null, null);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}

public static byte[] generatePermittedSubclass() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V17, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,
             "com/example/Circle", null, "com/example/Shape", null);
    cw.visitSource("Circle.java", null);

    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "radius", "D", null, null).visitEnd();

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(D)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/example/Shape", "<init>", "()V", false);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.DLOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "com/example/Circle", "radius", "D");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(3, 3);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "area", "()D", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "com/example/Circle", "radius", "D");
    mv.visitInsn(Opcodes.D2D);
    mv.visitLdcInsn(Math.PI);
    mv.visitInsn(Opcodes.DMUL);
    mv.visitInsn(Opcodes.DRETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}

}

Sealed class bytecode structure:

  • The sealed class has ACC_FINAL flag (cannot be extended by unlisted classes)
  • PermittedSubclasses attribute lists all allowed subclasses by internal name
  • Subclasses can be in different packages and modules
  • Permitted subclasses must be in the same module (or same package for unnamed module)
  • Subclasses can be final, sealed, or non-sealed
  • Non-sealed classes effectively open the hierarchy (no PermittedSubclasses needed)
  • The JVM verifier enforces that only permitted subclasses can extend the sealed class

Sealed interfaces follow the same pattern but with ACC_INTERFACE | ACC_ABSTRACT | ACC_FINAL.