All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.imqa.asm.GlobalMethodVisitor Maven / Gradle / Ivy

There is a newer version: 2.25.11
Show newest version
package io.imqa.asm;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.AdviceAdapter;

import java.util.HashMap;

import io.imqa.injector.MappingDecoder;
import io.imqa.injector.util.BuildOption;
import io.imqa.injector.util.Logger;

public class GlobalMethodVisitor extends ClassVisitor {
    private HashMap checkList = new HashMap<>();

    public GlobalMethodVisitor(ClassVisitor cv) {
        super(Opcodes.ASM5, cv);
    }

    public void addCheckList(HashMap checkList) {
        this.checkList = checkList;
    }

    public void addCheckList(CheckList checkList, InjectMethod injectMethod) {
        this.checkList.put(checkList, injectMethod);
    }

    public void addCheckList(int opcode, String owner, String name, String desc, InjectMethod injectMethod) {
        this.checkList.put(new CheckList(opcode, owner, name, desc), injectMethod);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name,
                                     String desc, String signature, String[] exceptions) {
        MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
//        Logger.d("VisitMethod");
        return new CheckListMethodVisitor(Opcodes.ASM5, name, desc, mv);
    }

    class CheckListMethodVisitor extends AdviceAdapter {
        private String className = "";
        private String classDesc = "";

        protected CheckListMethodVisitor(int access, String name, String desc, MethodVisitor mv) {
            super(Opcodes.ASM5, mv, access, name, desc);

            this.className = className;
            this.classDesc = classDesc;
        }
        /*public CheckListMethodVisitor(int api) {
            super(api);
        }

        public CheckListMethodVisitor(int api, MethodVisitor mv, String className, String classDesc) {
            super(api, mv);

            this.className = className;
            this.classDesc = classDesc;
        }*/

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            if (BuildOption.networkInject && inject(opcode, owner, name, desc))
                return;

            super.visitMethodInsn(opcode, owner, name, desc, itf);
        }

        public boolean inject(int opcode, String owner, String name, String desc) {

            boolean isInject = false;

//            Logger.d("KEYSET", checkList.keySet().size()+"");
            for (CheckList item : checkList.keySet()) {
//                Logger.d("inject", item.owner + " / " + item.name + " / " + item.desc);
                if (item.opcode == opcode
                        && item.owner.equals(owner)
                        && item.name.equals(name)
                        && item.desc.equals(desc)) {
//                    Logger.d("inject", name);
                    InjectMethod injectMethod = checkList.get(item);

//                    mv.visitInsn(Opcodes.ASTORE);
//                    mv.visitTypeInsn(Opcodes.NEW, injectMethod.className);
//                    mv.visitInsn(Opcodes.DUP);

                    super.visitMethodInsn(opcode, owner, name, desc, false);

                    super.visitTypeInsn(Opcodes.CHECKCAST, MappingDecoder.getInstance().findClass("java/net/HttpURLConnection"));
                    super.visitMethodInsn(
                            injectMethod.opcode,
                            injectMethod.className,
                            injectMethod.methodName,
                            injectMethod.desc,
                            false);
//                    mv.visitInsn(Opcodes.ASTORE);

                    Logger.d("HTTP_INJECT", className + ", "+classDesc +", "+owner+", "+name+", "+desc);
                    isInject = true;
                }
            }

            return isInject;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy