File size: 3,548 Bytes
b27bfb3
 
 
 
1
2
3
4
5
{"harness": "opencode", "name": "Sealed Classes in Bytecode", "id": "lang-0036", "type": "session"}
{"message": {"content": "You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.", "role": "system"}, "type": "message"}
{"message": {"content": "How are sealed classes represented in bytecode? Show me permitted subclasses and the PermittedSubclasses attribute.", "role": "user"}, "type": "message"}
{"message": {"content": "Sealed classes use the PermittedSubclasses attribute in the class file:\n\nimport org.objectweb.asm.*;\n\npublic class SealedClassExample {\n    public static byte[] generateSealedClass() {\n        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);\n        cw.visit(Opcodes.V17, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_FINAL,\n                 \"com/example/Shape\", null, \"java/lang/Object\", null);\n        cw.visitSource(\"Shape.java\", null);\n\n        cw.visitPermittedSubclass(\"com/example/Circle\");\n        cw.visitPermittedSubclass(\"com/example/Rectangle\");\n        cw.visitPermittedSubclass(\"com/example/Triangle\");\n\n        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, \"area\", \"()D\", null, null);\n        mv.visitEnd();\n\n        cw.visitEnd();\n        return cw.toByteArray();\n    }\n\n    public static byte[] generatePermittedSubclass() {\n        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);\n        cw.visit(Opcodes.V17, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,\n                 \"com/example/Circle\", null, \"com/example/Shape\", null);\n        cw.visitSource(\"Circle.java\", null);\n\n        cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, \"radius\", \"D\", null, null).visitEnd();\n\n        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, \"<init>\", \"(D)V\", null, null);\n        mv.visitCode();\n        mv.visitVarInsn(Opcodes.ALOAD, 0);\n        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, \"com/example/Shape\", \"<init>\", \"()V\", false);\n        mv.visitVarInsn(Opcodes.ALOAD, 0);\n        mv.visitVarInsn(Opcodes.DLOAD, 1);\n        mv.visitFieldInsn(Opcodes.PUTFIELD, \"com/example/Circle\", \"radius\", \"D\");\n        mv.visitInsn(Opcodes.RETURN);\n        mv.visitMaxs(3, 3);\n        mv.visitEnd();\n\n        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, \"area\", \"()D\", null, null);\n        mv.visitCode();\n        mv.visitVarInsn(Opcodes.ALOAD, 0);\n        mv.visitFieldInsn(Opcodes.GETFIELD, \"com/example/Circle\", \"radius\", \"D\");\n        mv.visitInsn(Opcodes.D2D);\n        mv.visitLdcInsn(Math.PI);\n        mv.visitInsn(Opcodes.DMUL);\n        mv.visitInsn(Opcodes.DRETURN);\n        mv.visitMaxs(2, 1);\n        mv.visitEnd();\n\n        cw.visitEnd();\n        return cw.toByteArray();\n    }\n}\n\nSealed class bytecode structure:\n- The sealed class has ACC_FINAL flag (cannot be extended by unlisted classes)\n- PermittedSubclasses attribute lists all allowed subclasses by internal name\n- Subclasses can be in different packages and modules\n- Permitted subclasses must be in the same module (or same package for unnamed module)\n- Subclasses can be final, sealed, or non-sealed\n- Non-sealed classes effectively open the hierarchy (no PermittedSubclasses needed)\n- The JVM verifier enforces that only permitted subclasses can extend the sealed class\n\nSealed interfaces follow the same pattern but with ACC_INTERFACE | ACC_ABSTRACT | ACC_FINAL.", "role": "assistant"}, "type": "message"}