Skip to content

adrian-kong/asm-playground

Repository files navigation

Description

Load, modify and save JAR file

Uses ASM, JDK 17

Usage

Build and add as dependencies

mvn clean install

Current run

// jar inputs
String jarIn = "asmtext.jar";
String jarOut = "asmtest-out.jar";

String className = "dev/Main.class";                                        // Class name to visit
        
new FileProcessCore().loadModifiers(className,ClassModifier.class);      // Load custom modifier
        .loadFile(new FileInputStream(jarIn))                               // Load file
        .editClasses()                                                      // Modifies classes
        .saveFile(new FileOutputStream(jarOut));                            // Save file

Add files to classpath if necessary (?)

Currently, to modify classes create a custom ClassModifier;

// Call #loadModifier with this class
new FileProcessCore().loadModifiers(className,CustomClassModifier.class);

/**
 * Custom sample class visitor
 */
public class CustomClassModifier extends ClassVisitor {

    public CustomClassModifier(int api, ClassVisitor classVisitor) {
        super(api, classVisitor);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);

        // find name
        if (name.equals("U")) {
            // custom method visitor
            return new MethodVisitor(ASM9, methodVisitor) {
                @Override
                public void visitLdcInsn(Object o) {
                    // change string to uppercase
                    if (o instanceof String str) {
                        o = str.toUpperCase();
                    }
                    super.visitLdcInsn(o);
                }
            };
        }
        return methodVisitor;
    }
}

Classes can also be overridden by loading two separate jars and overriding byte arrays. (No errors handled)

See agent

WIP, attach as -javaagent and to download JAR (?) maybe via runtime if possible

About

To fuel the bytecode addiction

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages