tinycodinglm / lang /lang-0038.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
6.5 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 do I integrate ASM with Gradle and Maven build systems? Show me plugin configurations for bytecode transformation.

Assistant

ASM integrates with build systems through plugins, agents, and custom tasks:

// build.gradle (Groovy) - Using ASM for bytecode transformation
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.ow2.asm:asm:9.6'
implementation 'org.ow2.asm:asm-tree:9.6'
implementation 'org.ow2.asm:asm-util:9.6'
}

task transformClasses {
doLast {
def inputDir = file("${buildDir}/classes/java/main")
def outputDir = file("${buildDir}/transformed")
outputDir.mkdirs()

markdown
inputDir.eachFileRecurse { file ->
        if (file.name.endsWith('.class')) {
            def bytes = file.bytes
            def cr = new org.objectweb.asm.ClassReader(bytes)
            def cw = new org.objectweb.asm.ClassWriter(cr, org.objectweb.asm.ClassWriter.COMPUTE_FRAMES)
            def cv = new org.objectweb.asm.ClassVisitor(org.objectweb.asm.Opcodes.ASM9, cw) {
                @Override
                org.objectweb.asm.MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {
                    def mv = super.visitMethod(access, name, desc, sig, exceptions)
                    return new org.objectweb.asm.MethodVisitor(org.objectweb.asm.Opcodes.ASM9, mv) {
                        @Override
                        void visitMethodInsn(int opcode, String owner, String mname, String mdesc, boolean isInterface) {
                            println "Found call: ${owner}.${mname}"
                            super.visitMethodInsn(opcode, owner, mname, mdesc, isInterface)
                        }
                    }
                }
            }
            cr.accept(cv, 0)
            def outFile = new File(outputDir, file.path.substring(inputDir.path.length()))
            outFile.parentFile.mkdirs()
            outFile.bytes = cw.toByteArray()
        }
    }
}

}

// build.gradle.kts (Kotlin DSL)
plugins {
java
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.ow2.asm:asm:9.6")
implementation("org.ow2.asm:asm-tree:9.6")
implementation("org.ow2.asm:asm-util:9.6")
}

tasks.register<Jar>("transformedJar") {
dependsOn("classes")
from("build/transformed")
archiveBaseName.set("transformed")
}

tasks.register("transformClasses") {
doLast {
val inputDir = layout.buildDirectory.dir("classes/java/main").get().asFile
val outputDir = layout.buildDirectory.dir("transformed").get().asFile
outputDir.mkdirs()

markdown
inputDir.walkTopDown().filter { it.extension == "class" }.forEach { file ->
        val bytes = file.readBytes()
        val cr = org.objectweb.asm.ClassReader(bytes)
        val cw = org.objectweb.asm.ClassWriter(cr, org.objectweb.asm.ClassWriter.COMPUTE_FRAMES)
        val cv = object : org.objectweb.asm.ClassVisitor(org.objectweb.asm.Opcodes.ASM9, cw) {
            override fun visitMethod(access: Int, name: String, desc: String, sig: String?, exceptions: Array<String>?): org.objectweb.asm.MethodVisitor {
                val mv = super.visitMethod(access, name, desc, sig, exceptions)
                return object : org.objectweb.asm.MethodVisitor(org.objectweb.asm.Opcodes.ASM9, mv) {
                    override fun visitMethodInsn(opcode: Int, owner: String, mname: String, mdesc: String, isInterface: Boolean) {
                        println("Found call: $owner.$mname")
                        super.visitMethodInsn(opcode, owner, mname, mdesc, isInterface)
                    }
                }
            }
        }
        cr.accept(cv, 0)
        val outFile = File(outputDir, file.relativeTo(inputDir).path)
        outFile.parentFile.mkdirs()
        outFile.writeBytes(cw.toByteArray())
    }
}

}

<!-- pom.xml - Maven with ASM dependency -->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>asm-transform</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.6</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>9.6</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>9.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.example.AsmTransformer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

For Java agents using ASM (attach to running JVM):
public class AsmAgent {
public static void premain(String args, Instrumentation inst) {
inst.addTransformer((loader, className, classBeingRedefined, protectionDomain, classfileBuffer) -> {
if (className == null) return null;
ClassReader cr = new ClassReader(classfileBuffer);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new ClassVisitor(Opcodes.ASM9, cw) {};
cr.accept(cv, 0);
return cw.toByteArray();
});
}
}

Build the agent JAR with:
Manifest-Version: 1.0
Premain-Class: com.example.AsmAgent
Can-Redefine-Classes: true