{"harness": "opencode", "name": "ASM Integration with Gradle and Maven", "id": "lang-0038", "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 do I integrate ASM with Gradle and Maven build systems? Show me plugin configurations for bytecode transformation.", "role": "user"}, "type": "message"} {"message": {"content": "ASM integrates with build systems through plugins, agents, and custom tasks:\n\n// build.gradle (Groovy) - Using ASM for bytecode transformation\nplugins {\n id 'java'\n}\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n implementation 'org.ow2.asm:asm:9.6'\n implementation 'org.ow2.asm:asm-tree:9.6'\n implementation 'org.ow2.asm:asm-util:9.6'\n}\n\ntask transformClasses {\n doLast {\n def inputDir = file(\"${buildDir}/classes/java/main\")\n def outputDir = file(\"${buildDir}/transformed\")\n outputDir.mkdirs()\n\n inputDir.eachFileRecurse { file ->\n if (file.name.endsWith('.class')) {\n def bytes = file.bytes\n def cr = new org.objectweb.asm.ClassReader(bytes)\n def cw = new org.objectweb.asm.ClassWriter(cr, org.objectweb.asm.ClassWriter.COMPUTE_FRAMES)\n def cv = new org.objectweb.asm.ClassVisitor(org.objectweb.asm.Opcodes.ASM9, cw) {\n @Override\n org.objectweb.asm.MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {\n def mv = super.visitMethod(access, name, desc, sig, exceptions)\n return new org.objectweb.asm.MethodVisitor(org.objectweb.asm.Opcodes.ASM9, mv) {\n @Override\n void visitMethodInsn(int opcode, String owner, String mname, String mdesc, boolean isInterface) {\n println \"Found call: ${owner}.${mname}\"\n super.visitMethodInsn(opcode, owner, mname, mdesc, isInterface)\n }\n }\n }\n }\n cr.accept(cv, 0)\n def outFile = new File(outputDir, file.path.substring(inputDir.path.length()))\n outFile.parentFile.mkdirs()\n outFile.bytes = cw.toByteArray()\n }\n }\n }\n}\n\n// build.gradle.kts (Kotlin DSL)\nplugins {\n java\n}\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n implementation(\"org.ow2.asm:asm:9.6\")\n implementation(\"org.ow2.asm:asm-tree:9.6\")\n implementation(\"org.ow2.asm:asm-util:9.6\")\n}\n\ntasks.register(\"transformedJar\") {\n dependsOn(\"classes\")\n from(\"build/transformed\")\n archiveBaseName.set(\"transformed\")\n}\n\ntasks.register(\"transformClasses\") {\n doLast {\n val inputDir = layout.buildDirectory.dir(\"classes/java/main\").get().asFile\n val outputDir = layout.buildDirectory.dir(\"transformed\").get().asFile\n outputDir.mkdirs()\n\n inputDir.walkTopDown().filter { it.extension == \"class\" }.forEach { file ->\n val bytes = file.readBytes()\n val cr = org.objectweb.asm.ClassReader(bytes)\n val cw = org.objectweb.asm.ClassWriter(cr, org.objectweb.asm.ClassWriter.COMPUTE_FRAMES)\n val cv = object : org.objectweb.asm.ClassVisitor(org.objectweb.asm.Opcodes.ASM9, cw) {\n override fun visitMethod(access: Int, name: String, desc: String, sig: String?, exceptions: Array?): org.objectweb.asm.MethodVisitor {\n val mv = super.visitMethod(access, name, desc, sig, exceptions)\n return object : org.objectweb.asm.MethodVisitor(org.objectweb.asm.Opcodes.ASM9, mv) {\n override fun visitMethodInsn(opcode: Int, owner: String, mname: String, mdesc: String, isInterface: Boolean) {\n println(\"Found call: $owner.$mname\")\n super.visitMethodInsn(opcode, owner, mname, mdesc, isInterface)\n }\n }\n }\n }\n cr.accept(cv, 0)\n val outFile = File(outputDir, file.relativeTo(inputDir).path)\n outFile.parentFile.mkdirs()\n outFile.writeBytes(cw.toByteArray())\n }\n }\n}\n\n\n\n 4.0.0\n com.example\n asm-transform\n 1.0-SNAPSHOT\n \n \n org.ow2.asm\n asm\n 9.6\n \n \n org.ow2.asm\n asm-tree\n 9.6\n \n \n org.ow2.asm\n asm-util\n 9.6\n \n \n \n \n \n org.codehaus.mojo\n exec-maven-plugin\n 3.1.0\n \n com.example.AsmTransformer\n \n \n \n \n\n\nFor Java agents using ASM (attach to running JVM):\npublic class AsmAgent {\n public static void premain(String args, Instrumentation inst) {\n inst.addTransformer((loader, className, classBeingRedefined, protectionDomain, classfileBuffer) -> {\n if (className == null) return null;\n ClassReader cr = new ClassReader(classfileBuffer);\n ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);\n ClassVisitor cv = new ClassVisitor(Opcodes.ASM9, cw) {};\n cr.accept(cv, 0);\n return cw.toByteArray();\n });\n }\n}\n\nBuild the agent JAR with:\nManifest-Version: 1.0\nPremain-Class: com.example.AsmAgent\nCan-Redefine-Classes: true", "role": "assistant"}, "type": "message"}