Example usage for org.objectweb.asm.tree MethodNode visitMaxs

List of usage examples for org.objectweb.asm.tree MethodNode visitMaxs

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode visitMaxs.

Prototype

@Override
    public void visitMaxs(final int maxStack, final int maxLocals) 

Source Link

Usage

From source file:de.sanandrew.core.manpack.transformer.TransformPlayerDismountCtrl.java

License:Creative Commons License

/**
 * Transforms the Entity.class by adding a new method called _SAP_canDismountOnInput.<br>
 * This method can be overridden by any entity to control wether or not the rider can dismount via sneaking (usually by pressing LSHIFT for the player).
 *
 * @param bytes     the class bytes to be transformed
 * @return the transformed class bytes/*from   w  w w.java  2s. c  o m*/
 */
private static byte[] transformEntity(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_DISMOUNT_ON_INPUT);
    method.visitCode();
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitInsn(Opcodes.ICONST_1);
    method.visitInsn(Opcodes.IRETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0);
    method.visitLocalVariable("player", ASMNames.CL_T_ENTITY_PLAYER, null, l0, l1, 1);
    method.visitMaxs(1, 2);
    method.visitEnd();

    clazz.methods.add(method);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
    return bytes;
}

From source file:me.themallard.bitmmo.impl.plugin.inputactiontracker.InputActionTrackerPlugin.java

License:Open Source License

private void createSetKeyDown(ClassNode cn) {
    InsnList insn = new InsnList();

    MethodNode mn = new MethodNode(cn, ACC_PUBLIC, "setKeyDown", "(LHTMud/InputActionTracker$ActionType;Z)V",
            null, null);//w  w  w. j  a  v  a  2 s .co  m

    // ((Key) this.enumMap.get(actionType)).setKeyPressed(value);

    // aload0
    // getfield this.enumMap Ljava/util/EnumMap;
    // aload1
    // invokevirtual \
    // java/util/EnumMap.get((Ljava/lang/Object;)Ljava/lang/Object;);
    // checkcast Key
    // aload2
    // invokevirtual Key.setKeyPressed((Z)V);
    // return

    mn.visitMaxs(3, 3);

    insn.add(new VarInsnNode(ALOAD, 0));

    FieldNode enumMap = cn.getField(null, "Ljava/util/EnumMap;");
    insn.add(new FieldInsnNode(GETFIELD, cn.name, enumMap.name, enumMap.desc));

    insn.add(new VarInsnNode(ALOAD, 1));

    insn.add(new MethodInsnNode(INVOKEVIRTUAL, "java/util/EnumMap", "get",
            "(Ljava/lang/Object;)Ljava/lang/Object;", false));

    insn.add(new TypeInsnNode(CHECKCAST, "Key"));

    insn.add(new VarInsnNode(ILOAD, 2));

    insn.add(new MethodInsnNode(INVOKEVIRTUAL, "Key", "setKeyPressed", "(Z)V", false));

    insn.add(new InsnNode(RETURN));

    mn.instructions.insert(insn);

    cn.methods.add(mn);
}

From source file:naftoreiclag.dontdigleft.transformer.ResizePlayerBoudingBox.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (name.equals("net.minecraft.entity.player.EntityPlayer") || name.equals("xl")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);

        classReader.accept(classNode, 0);

        MethodNode mv = new MethodNode(ASM4, ACC_PROTECTED, "setSize", "(FF)V", null, null);

        mv.visitCode();// w  w w . j  a  v a2  s  .  c o m
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitInsn(FCMPL);
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitLdcInsn(new Float("1.8"));
        mv.visitInsn(FCMPL);
        mv.visitJumpInsn(IFNE, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 1);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, "net/minecraft/entity/Entity", "setSize", "(FF)V");
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitInsn(RETURN);
        Label l5 = new Label();
        mv.visitLabel(l5);
        mv.visitLocalVariable("this", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l5, 0);
        mv.visitLocalVariable("par1", "F", null, l0, l5, 1);
        mv.visitLocalVariable("par2", "F", null, l0, l5, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(cw);
        mv.accept(cw);

        return cw.toByteArray();
    } else {
        return basicClass;
    }
}

From source file:net.petercashel.jmsDd.util.ASMTransformer.java

License:Apache License

public static byte[] transform(String name, byte[] bytes) {
    if (debug)/*from  ww  w  .  j av  a2s.c  om*/
        System.out.println(bytes.length);
    ClassNode classNode = new ClassNode();
    String classNameASM = name.replace('.', '/');
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    boolean DoModInit = false;
    boolean HasInit = false;
    String initDesc = "()V";
    boolean lockDesc = false;

    try {
        try {
            for (int i = 0; i < classNode.visibleAnnotations.size(); i++) {
                AnnotationNode ann = (AnnotationNode) classNode.visibleAnnotations.get(i);
                if (ann.desc.equalsIgnoreCase("Lnet/petercashel/jmsDd/module/Module;")) {
                    try {
                        if (debug)
                            System.out.println("ANNOTE!");
                        DoModInit = true;
                        Map<String, Object> values = asmList2Map(ann.values);
                        ModuleSystem.modulesToLoad.put(
                                values.get("ModuleName").toString().replace("[", "").replace("]", ""),
                                classNode.name.replace("/", "."));
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
            }
        } catch (Exception e) {
        }

        try {
            if (DoModInit) {

                for (int i = 0; i < classNode.methods.size(); i++) {
                    MethodNode m = (MethodNode) classNode.methods.get(i);
                    if (m.name.contentEquals("<init>")) {
                        initDesc = m.desc;
                        if (m.desc.contentEquals("()V")) {
                            HasInit = true;
                            if (debug)
                                System.out.println("Found <init>");
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive?");
    try {

        //          L0
        //             LINENUMBER 43 L0
        //             ALOAD 0
        //             INVOKESPECIAL // CLASSNAME FOR ASM  // ()V    ////// This is effectically a super() call but to the discovered constructor
        //            L1
        //             LINENUMBER 44 L1
        //             INVOKESTATIC net/petercashel/jmsDd/API/API$Impl.getAPI ()Lnet/petercashel/jmsDd/API/API;
        //             ALOAD 0
        //             INVOKEINTERFACE net/petercashel/jmsDd/API/API.registerEventBus (Ljava/lang/Object;)V
        //            L2
        //             LINENUMBER 46 L2
        //             RETURN
        //            L3
        //             LOCALVARIABLE this // "L" + CLASSNAME FOR ASM + ";" // L0 L3 0
        //             LOCALVARIABLE e Lnet/petercashel/jmsDd/event/module/DummyEvent; L0 L3 1
        //             MAXSTACK = 2
        //             MAXLOCALS = 2
        if (DoModInit) {
            if (HasInit) {
                if (debug)
                    System.out.println("Adding Extra Constructor to " + name);
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, classNameASM, "<init>", initDesc);

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);

            } else {
                System.err.println("WARNING! " + name
                        + " Doesn't have a default no-args constructor.  Module loader cannot chain load the constructor. \n If you are recieving this error and your module has no constructors defined, or a no-args constructor defined, \n please report the bug to the author of JMSDd.");
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                //INVOKESPECIAL java/lang/Object.<init> ()V ////// There is no other constructor, call super() to Object.
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);
            }
            classNode.visitEnd();
            ClassWriter wr = new ClassWriter(0);
            classNode.accept(wr);

            bytes = wr.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive.");
    if (debug)
        System.out.println(bytes.length);
    if (plugins.size() > 0) {
        for (ASMPlugin p : plugins) {
            try {
                bytes = p.transform(name, bytes);
            } catch (Exception e) {

            }
        }
    }

    return bytes;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ASMHelper.java

License:Open Source License

public static void copy(MethodNode src, MethodNode dst) {
    Map<LabelNode, LabelNode> labelMap = cloneLabels(src.instructions);
    dst.instructions = cloneInsnList(labelMap, src.instructions);
    dst.tryCatchBlocks = cloneTryCatchBlocks(labelMap, src.tryCatchBlocks);
    if (src.localVariables != null) {
        dst.localVariables = cloneLocals(labelMap, src.localVariables);
    }/*from ww  w.  ja v a 2  s .  c om*/
    dst.visibleAnnotations = src.visibleAnnotations;
    dst.invisibleAnnotations = src.invisibleAnnotations;
    dst.visitMaxs(src.maxStack, src.maxLocals);
}

From source file:org.qkit.core.asm.adapters.AddCustomGetterAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;/*w  w  w  .ja  va 2s  .co  m*/

    for (FieldNode f : cn.fields) {
        if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) {
            isFieldPresent = true;
            signature = f.signature;
            if ((f.access & ACC_STATIC) != 0) {
                isStatic = true;
            }
            break;
        }
    }

    for (MethodNode mv : cn.methods) {
        if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) {
            isMethodPresent = true;
            break;
        }
    }

    if (isFieldPresent && !isMethodPresent) {
        MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + getterDesc, signature, null);

        mn.instructions.add(new VarInsnNode(ALOAD, 0));
        mn.instructions
                .add(new FieldInsnNode(isStatic ? GETSTATIC : GETFIELD, cn.name, fieldName, fieldDescriptor));
        mn.instructions.add(new InsnNode(retInsn));

        mn.visitMaxs(3, 3);
        mn.visitEnd();
        cn.methods.add(mn);

        System.out.println("          [+M] " + fieldDescriptor + " " + getterName + "() identified as "
                + cn.name + "." + fieldName);
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}

From source file:org.qkit.core.asm.adapters.AddGetterAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;//from  www .  ja  va2  s.  c  om

    for (FieldNode f : cn.fields) {
        if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) {
            isFieldPresent = true;
            signature = f.signature;
            if ((f.access & ACC_STATIC) != 0) {
                isStatic = true;
            }
            break;
        }
    }

    for (MethodNode mv : cn.methods) {
        if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) {
            isMethodPresent = true;
            break;
        }
    }

    if (isFieldPresent && !isMethodPresent) {
        MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + fieldDescriptor, signature, null);

        mn.instructions.add(new VarInsnNode(ALOAD, 0));
        mn.instructions
                .add(new FieldInsnNode(isStatic ? GETSTATIC : GETFIELD, cn.name, fieldName, fieldDescriptor));
        mn.instructions.add(new InsnNode(retInsn));

        mn.visitMaxs(3, 3);
        mn.visitEnd();
        cn.methods.add(mn);

        System.out.println("          [+M] " + fieldDescriptor + " " + getterName + "() identified as "
                + cn.name + "." + fieldName);
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}

From source file:org.qkit.core.asm.adapters.AddMethodAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;//from ww  w.  j a v  a 2  s .c  om

    for (FieldNode f : cn.fields) {
        if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) {
            isFieldPresent = true;
            signature = f.signature;
            break;
        }
    }

    for (MethodNode mv : cn.methods) {
        if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) {
            isMethodPresent = true;
            break;
        }
    }

    if (isFieldPresent && !isMethodPresent) {
        MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + fieldDescriptor, signature, null);

        mn.instructions.add(new VarInsnNode(varInsn, 0));
        mn.instructions.add(new FieldInsnNode(GETFIELD, cn.name, fieldName, fieldDescriptor));
        mn.instructions.add(new InsnNode(retInsn));

        mn.visitMaxs(0, 0);

        cn.methods.add(mn);
        System.out.println(
                "[+Method Addition] " + fieldDescriptor + " " + getterName + "() returns " + fieldName);
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}