Example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getOpcode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode.

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

From source file:name.martingeisse.minimal.compiler.Compiler.java

License:Open Source License

private void unsupported(final AbstractInsnNode node) {
    System.err.println("unsupported instruction node " + node + ", opcode: " + node.getOpcode());
}

From source file:net.afterchat.mocap.MocapClassTransformer.java

License:Open Source License

public byte[] patchClassASM(String name, byte[] bytes) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);//from   w ww .  j  a va 2 s . c o  m
    Iterator<MethodNode> methods = classNode.methods.iterator();

    while (methods.hasNext()) {
        MethodNode m = methods.next();

        if ((m.name.equals("onBlockPlacedBy"))
                || (m.name.equals("a") && m.desc.equals("(Lahb;IIILsv;Ladd;)V"))) {
            logger.info("** MOCAP - Patching onBlockPlacedBy: " + m.name);
            AbstractInsnNode currentNode = null;
            Iterator<AbstractInsnNode> iter = m.instructions.iterator();
            int index = -1;

            while (iter.hasNext()) {
                index++;
                currentNode = iter.next();

                /**
                 * Just prior to the original empty function return, inject code to trigger
                 * our custom block place event.
                 */
                if (currentNode.getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new TypeInsnNode(NEW, "net/afterchat/mocap/LivingPlaceBlockEvent"));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new VarInsnNode(ALOAD, 5));
                    toInject.add(new VarInsnNode(ALOAD, 6));
                    toInject.add(new VarInsnNode(ILOAD, 2));
                    toInject.add(new VarInsnNode(ILOAD, 3));
                    toInject.add(new VarInsnNode(ILOAD, 4));
                    toInject.add(new MethodInsnNode(INVOKESPECIAL, "net/afterchat/mocap/LivingPlaceBlockEvent",
                            "<init>",
                            "(Lnet/minecraft/entity/EntityLivingBase;Lnet/minecraft/item/ItemStack;III)V"));
                    toInject.add(new VarInsnNode(ASTORE, 7));
                    toInject.add(new FieldInsnNode(GETSTATIC, "net/minecraftforge/common/MinecraftForge",
                            "EVENT_BUS", "Lcpw/mods/fml/common/eventhandler/EventBus;"));
                    toInject.add(new VarInsnNode(ALOAD, 7));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cpw/mods/fml/common/eventhandler/EventBus",
                            "post", "(Lcpw/mods/fml/common/eventhandler/Event;)Z"));
                    toInject.add(new InsnNode(POP));

                    m.instructions.insertBefore(currentNode, toInject);
                }
            }
        }
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.cazzar.corelib.asm.MethodTransformer.java

License:Open Source License

private boolean isReturn(AbstractInsnNode node) {
    switch (node.getOpcode()) {
    case Opcodes.RET:
    case Opcodes.RETURN:
    case Opcodes.ARETURN:
    case Opcodes.DRETURN:
    case Opcodes.FRETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
        return true;

    default:/*from  w  ww . ja v a 2 s  .  c om*/
        return false;
    }
}

From source file:net.doubledoordev.inventorylock.asm.Transformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, READER_FLAGS);

    boolean isPlayer = transformedName.equals(ENTITY_PLAYER_OWNER_NAME);
    if (isPlayer)
        LOGGER.info("Found EntityPlayer");

    for (MethodNode method : classNode.methods) {
        InsnList list = method.instructions;
        if (isPlayer && INSTANCE.mapMethodDesc(method.desc).equals(ENTITY_PLAYER_DESC)
                && INSTANCE.mapMethodName(name, method.name, method.desc).equals(ENTITY_PLAYER_TARGET)) {
            final LabelNode newLabel = new LabelNode();
            LOGGER.info("Found canOpen");
            AbstractInsnNode node = list.getFirst();
            while (node.getOpcode() != IRETURN && node != list.getLast()) {
                if (node.getOpcode() == IFEQ)
                    ((JumpInsnNode) node).label = newLabel;
                node = node.getNext();//from w w w  . j av  a 2  s  . c  om
            }
            if (node.getOpcode() != IRETURN)
                throw new RuntimeException("ASM failed. (return not found)");
            final AbstractInsnNode target = node;
            while (node.getType() != LABEL && node != list.getLast())
                node = node.getNext();
            if (node.getType() != LABEL)
                throw new RuntimeException("ASM failed. (label not found)");
            final LabelNode label = ((LabelNode) node);

            //Adding "else if (code instanceof BetterLockCode) return ((BetterLockCode) code).contains(this.getUniqueID());"
            InsnList inject = new InsnList();

            inject.add(newLabel);
            inject.add(new VarInsnNode(ALOAD, 1));
            inject.add(new TypeInsnNode(INSTANCEOF, BETTER_LOCK_TYPE));
            inject.add(new JumpInsnNode(IFEQ, label));
            inject.add(new VarInsnNode(ALOAD, 1));
            inject.add(new TypeInsnNode(CHECKCAST, BETTER_LOCK_TYPE));
            inject.add(new VarInsnNode(ALOAD, 0));
            //inject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ENTITY_PLAYER_OWNER, ENTITY_PLAYER_GET_UUID, ENTITY_PLATER_GET_UUID_DESC, false));
            inject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, BETTER_LOCK_TYPE, BETTER_LOCK_CONTAINS,
                    BETTER_LOCK_CONTAINS_DESC, false));
            inject.add(new InsnNode(IRETURN));

            list.insert(target, inject);
            LOGGER.info("Injected elseif into EntityPlayer's canOpen");
        }
        for (AbstractInsnNode node = list.getFirst(); node != list.getLast(); node = node.getNext()) {
            if (node.getOpcode() != INVOKESTATIC)
                continue;
            MethodInsnNode methodInsnNode = ((MethodInsnNode) node);
            //                if (transformedName.equals("net.minecraft.tileentity.TileEntityLockable"))
            //                    LOGGER.info("In {} ({}) Method {}.{}{} Translated {}.{}{}", name, transformedName,
            //                            methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc,
            //                            INSTANCE.map(methodInsnNode.owner), INSTANCE.mapMethodName(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc), INSTANCE.mapMethodDesc(methodInsnNode.desc).equals(LOCK_CODE_DESC));
            if (INSTANCE.map(methodInsnNode.owner).equals(LOCK_CODE_OWNER)
                    && INSTANCE.mapMethodDesc(methodInsnNode.desc).equals(LOCK_CODE_DESC)
                    && INSTANCE.mapMethodName(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc)
                            .equals(LOCK_CODE_TARGET)) {
                methodInsnNode.owner = LOCK_CODE_OWNER_REPLACE;
                methodInsnNode.name = LOCK_CODE_NAME;
                LOGGER.info("Replaced call in class {} ({}), method {}{}", name, transformedName, method.name,
                        method.desc);
            }
        }
    }

    final ClassWriter writer = new ClassWriter(WRITER_FLAGS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.dries007.tfctweaks.asm.FluidContainerRegistryCT.java

License:Open Source License

private byte[] magic(byte[] bytes) {
    FMLLog.info("Found the FluidContainerRegistry class...");
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);//from   w  w w  .j  av a 2s. co  m
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("<clinit>") && m.desc.equals("()V")) {
            FMLLog.info("Found the <clinit> method...");

            ListIterator<AbstractInsnNode> i = m.instructions.iterator();
            while (i.hasNext()) {
                AbstractInsnNode node = i.next();
                if (!(node instanceof FieldInsnNode) || node.getOpcode() != GETSTATIC)
                    continue;
                FieldInsnNode fieldInsnNode = ((FieldInsnNode) node);
                if (!fieldInsnNode.owner.equals("net/minecraftforge/fluids/FluidRegistry"))
                    continue;
                if (!fieldInsnNode.name.equals("WATER") && !fieldInsnNode.name.equals("LAVA"))
                    continue;
                if (!fieldInsnNode.desc.equals("Lnet/minecraftforge/fluids/Fluid;"))
                    continue;
                do {
                    i.remove();
                    node = i.next();
                } while (node.getOpcode() != POP);
                i.remove(); // remove last pop
                FMLLog.info("[FluidContainerRegistryCT] Removed the " + fieldInsnNode.name + " registration.");
                done++;
            }
        }
    }

    if (done != DONE) {
        FMLLog.severe(
                "\n######################################################################################\n"
                        + "######################################################################################\n"
                        + "######################################################################################\n"
                        + "OUR ASM FLUID HACK FAILED! PLEASE MAKE AN ISSUE REPORT ON GITHUB WITH A COMPLETE MODLIST! https://github.com/dries007/TFC-Tweaks\n"
                        + "Done %d out of %d ASM tweaks on class FluidContainerRegistry\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n\n",
                done, DONE);
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.dries007.tfctweaks.asm.FluidRegistryCT.java

License:Open Source License

private byte[] magic(byte[] bytes) {
    FMLLog.info("Found the FluidRegistry class...");
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);//from  ww  w .java  2 s.  c  o  m
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("<clinit>") && m.desc.equals("()V")) {
            FMLLog.info("Found the <clinit> method...");

            ListIterator<AbstractInsnNode> i = m.instructions.iterator();
            while (i.hasNext()) {
                AbstractInsnNode node = i.next();
                if (!(node instanceof FieldInsnNode) || node.getOpcode() != GETSTATIC)
                    continue;
                FieldInsnNode fieldInsnNode = ((FieldInsnNode) node);
                if (!fieldInsnNode.owner.equals("net/minecraftforge/fluids/FluidRegistry"))
                    continue;
                if (!fieldInsnNode.name.equals("WATER") && !fieldInsnNode.name.equals("LAVA"))
                    continue;
                if (!fieldInsnNode.desc.equals("Lnet/minecraftforge/fluids/Fluid;"))
                    continue;
                node = i.next();
                if (!(node instanceof MethodInsnNode) || node.getOpcode() != INVOKESTATIC)
                    continue;
                MethodInsnNode methodInsnNode = ((MethodInsnNode) node);
                if (!methodInsnNode.owner.equals("net/minecraftforge/fluids/FluidRegistry"))
                    continue;
                if (!methodInsnNode.name.equals("registerFluid"))
                    continue;
                if (!methodInsnNode.desc.equals("(Lnet/minecraftforge/fluids/Fluid;)Z"))
                    continue;
                node = i.next();
                if (!(node instanceof InsnNode) || node.getOpcode() != POP)
                    continue;
                InsnNode insnNode = ((InsnNode) node);
                m.instructions.remove(fieldInsnNode);
                m.instructions.remove(methodInsnNode);
                m.instructions.remove(insnNode);
                FMLLog.info("[FluidRegistryCT] Removed the " + fieldInsnNode.name + " registration.");
                done++;
            }
        } else if (m.name.equals("getFluid")
                && m.desc.equals("(Ljava/lang/String;)Lnet/minecraftforge/fluids/Fluid;")) {
            FMLLog.info("Found the getFluid method...");
            InsnList insnList = new InsnList();
            {
                LabelNode labelFirstIf = new LabelNode();
                insnList.add(new FieldInsnNode(GETSTATIC, "net/dries007/tfctweaks/util/FluidHacks",
                        "makeAllWaterFTCWater", "Z"));
                insnList.add(new JumpInsnNode(IFEQ, labelFirstIf));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new LdcInsnNode("water"));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFEQ, labelFirstIf));
                insnList.add(new FieldInsnNode(GETSTATIC, "com/bioxx/tfc/api/TFCFluids", "FRESHWATER",
                        "Lnet/minecraftforge/fluids/Fluid;"));
                insnList.add(new InsnNode(ARETURN));
                insnList.add(labelFirstIf);
            }
            {
                LabelNode lableSecondIf = new LabelNode();
                insnList.add(new FieldInsnNode(GETSTATIC, "net/dries007/tfctweaks/util/FluidHacks",
                        "makeAllLavaFTCLava", "Z"));
                insnList.add(new JumpInsnNode(IFEQ, lableSecondIf));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new LdcInsnNode("lava"));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFEQ, lableSecondIf));
                insnList.add(new FieldInsnNode(GETSTATIC, "com/bioxx/tfc/api/TFCFluids", "LAVA",
                        "Lnet/minecraftforge/fluids/Fluid;"));
                insnList.add(new InsnNode(ARETURN));
                insnList.add(lableSecondIf);
            }
            m.instructions.insertBefore(m.instructions.getFirst(), insnList);

            FMLLog.info("[FluidRegistryCT] Edited getFluid(String) : Fluid.");
            done++;
        } else if (m.name.equals("isFluidRegistered")) {
            if (m.desc.equals("(Lnet/minecraftforge/fluids/Fluid;)Z")) {
                InsnList insnList = new InsnList();
                LabelNode falseLabel = new LabelNode();
                LabelNode trueLabel = new LabelNode();
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new JumpInsnNode(IFNULL, falseLabel));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/fluids/Fluid", "getName",
                        "()Ljava/lang/String;", false));
                insnList.add(new MethodInsnNode(INVOKESTATIC, "net/minecraftforge/fluids/FluidRegistry",
                        "isFluidRegistered", "(Ljava/lang/String;)Z", false));
                insnList.add(new JumpInsnNode(IFEQ, falseLabel));
                insnList.add(new InsnNode(ICONST_1));
                insnList.add(new JumpInsnNode(GOTO, trueLabel));
                insnList.add(falseLabel);
                insnList.add(new InsnNode(ICONST_0));
                insnList.add(trueLabel);
                insnList.add(new InsnNode(IRETURN));
                // replace entire method
                m.instructions.clear();
                m.instructions.add(insnList);

                FMLLog.info("[FluidRegistryCT] Edited isFluidRegistered(Fluid) : bool.");
                done++;
            } else if (m.desc.equals("(Ljava/lang/String;)Z")) {
                InsnList insnList = new InsnList();
                LabelNode trueLabel = new LabelNode();
                insnList.add(new LdcInsnNode("water"));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFNE, trueLabel));
                insnList.add(new LdcInsnNode("lava"));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                        "(Ljava/lang/Object;)Z", false));
                insnList.add(new JumpInsnNode(IFNE, trueLabel));
                insnList.add(new FieldInsnNode(GETSTATIC, "net/minecraftforge/fluids/FluidRegistry", "fluids",
                        "Lcom/google/common/collect/BiMap;"));
                insnList.add(new VarInsnNode(ALOAD, 0));
                insnList.add(new MethodInsnNode(INVOKEINTERFACE, "com/google/common/collect/BiMap",
                        "containsKey", "(Ljava/lang/Object;)Z", true));
                LabelNode falseLabel = new LabelNode();
                insnList.add(new JumpInsnNode(IFEQ, falseLabel));
                insnList.add(trueLabel);
                insnList.add(new InsnNode(ICONST_1));
                LabelNode returnLabel = new LabelNode();
                insnList.add(new JumpInsnNode(GOTO, returnLabel));
                insnList.add(falseLabel);
                insnList.add(new InsnNode(ICONST_0));
                insnList.add(returnLabel);
                insnList.add(new InsnNode(IRETURN));

                // replace entire method
                m.instructions.clear();
                m.instructions.add(insnList);

                FMLLog.info("[FluidRegistryCT] Edited isFluidRegistered(String) : bool.");
                done++;
            }
        } else if (m.name.equals("getFluidStack")) {
            LabelNode notNullNode = null;
            { // Grab first jump node label
                ListIterator<AbstractInsnNode> i = m.instructions.iterator();
                while (i.hasNext()) {
                    AbstractInsnNode node = i.next();
                    if (node.getOpcode() == IFNE) {
                        notNullNode = ((JumpInsnNode) node).label;
                        break;
                    }
                }
            }

            InsnList insnList = new InsnList();
            insnList.add(new LdcInsnNode("water"));
            insnList.add(new VarInsnNode(ALOAD, 0));
            insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                    "(Ljava/lang/Object;)Z", false));
            insnList.add(new JumpInsnNode(IFNE, notNullNode));
            insnList.add(new LdcInsnNode("lava"));
            insnList.add(new VarInsnNode(ALOAD, 0));
            insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals",
                    "(Ljava/lang/Object;)Z", false));
            insnList.add(new JumpInsnNode(IFNE, notNullNode));

            // add to the beginning of the list, leave the rest of the method in place.
            m.instructions.insert(insnList);

            FMLLog.info("[FluidRegistryCT] Edited getFluidStack(String, int) : FluidStack.");
            done++;
        }
    }

    if (done != DONE) {
        FMLLog.severe(
                "\n######################################################################################\n"
                        + "######################################################################################\n"
                        + "######################################################################################\n"
                        + "OUR ASM FLUID HACK FAILED! PLEASE MAKE AN ISSUE REPORT ON GITHUB WITH A COMPLETE MODLIST! https://github.com/dries007/TFC-Tweaks\n"
                        + "Done %d out of %d ASM tweaks on class FluidRegistry\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n"
                        + "########################################################################################\n\n",
                done, DONE);
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.dv8tion.ClassTransformer.java

License:Apache License

/**
 * Helper method to modify the getAllUsernames method in ServerConfigurationManager.
 * Takes into account obfuscated method names based on boolean.
 * //from   w w w . j  a  va2 s  .com
 * Replaces the call "return astring" at the end of ServerConfigurationManager's
 * getAllUsernames method with "return NameLoader.loadNames(astring)"
 * Because of how The JVM and ByteCode work with arrays, we do not need to
 * remove any instructions, only inject code. The array "astring" in the
 * call "return astring" will be provided as the param for the loadNames method.
 * 
 * @param className
 *            The class name, proceeded by the package it is in, if it is in one.
 * @param classData
 *            The byte code of the class.
 * @param obfuscated
 *            Is the code obfuscated?
 * @return
 *         Returns the modified byte code of the class.
 */
public byte[] patchClassWithASM(String className, byte[] classData, boolean obfuscated) {
    String methodName = obfuscated ? "d" : "getAllUsernames";

    ClassNode classNode = new ClassNode(Opcodes.ASM4);
    ClassReader classReader = new ClassReader(classData);
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);

    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        MethodNode m = methods.next();
        int arrayReturn_index = -1;

        if ((m.name.equals(methodName) && m.desc.equals("()[Ljava/lang/String;"))) {
            AbstractInsnNode currentNode = null;
            Iterator<AbstractInsnNode> iter = m.instructions.iterator();

            int index = -1;

            while (iter.hasNext()) {
                index++;
                currentNode = iter.next();

                if (currentNode.getOpcode() == Opcodes.ARETURN) {
                    arrayReturn_index = index;
                }
            }

            //Calls NameLoader.loadNames(String[]) 
            m.instructions.insertBefore(m.instructions.get(arrayReturn_index),
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "net/dv8tion/NameLoader", "loadNames",
                            "([Ljava/lang/String;)[Ljava/lang/String;"));

            System.out.println("[IRC NameBridge] Patching Complete!");
            break;
        }
    }
    //ASM specific for cleaning up and returning the final bytes for JVM processing.
    //Use 0 here instead of like ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS because
    //We need to have ASM recalculate things.  
    ClassWriter writer = new ClassWriter(0);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.enilink.composition.cache.behaviours.CacheBehaviourMethodProcessor.java

License:Open Source License

@Override
public void process(BehaviourClassNode classNode, ExtendedMethod method) throws Exception {
    boolean isAbstract = method.instructions.size() == 0;

    Type returnType = Type.getReturnType(method.desc);

    Cacheable cacheable = AsmUtils.findAnnotation(Cacheable.class, method.getOverriddenMethod());
    String cacheKey = cacheable.key().isEmpty() ? method.name : cacheable.key();

    BehaviourMethodGenerator gen = new BehaviourMethodGenerator(method);
    if (!isAbstract) {
        gen.pushInsns();//from   w w  w.  j a  v a2 s.c  o m
    }

    gen.loadThis();
    gen.getField("cache", Type.getType(IPropertyCache.class));
    gen.loadBean();
    gen.push(cacheKey);
    gen.loadArgArray();
    gen.invokeInterface(Type.getType(IPropertyCache.class), new org.objectweb.asm.commons.Method("get",
            OBJECT_TYPE, new Type[] { OBJECT_TYPE, OBJECT_TYPE, Type.getType(Object[].class) }));
    gen.dup();

    Label isNull = gen.newLabel();
    gen.ifNull(isNull);

    gen.unbox(returnType);
    gen.returnValue();

    gen.mark(isNull);
    gen.pop();

    if (!isAbstract) {
        gen.peekInsns().insertBefore(gen.peekInsns().getFirst(), gen.instructions);
        gen.instructions.clear();
    }

    int result = gen.newLocal(returnType);

    if (!isAbstract) {
        // add caching instructions
        for (AbstractInsnNode insn : method.instructions.toArray()) {
            if (insn.getOpcode() >= IRETURN & insn.getOpcode() <= ARETURN) {
                gen.pushInsns();

                storeValue(result, returnType, cacheKey, gen);

                gen.peekInsns().insertBefore(insn, gen.instructions);
                gen.instructions.clear();
            }
        }
    } else {
        // generate super call and store value in cache
        gen.loadThis();
        gen.loadArgs();
        gen.invokeSpecial(classNode.getParentType(),
                new org.objectweb.asm.commons.Method(method.name, method.desc));
        storeValue(result, returnType, cacheKey, gen);
        gen.returnValue();
    }
}

From source file:net.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

/**
 * Compares whether or not two instructions are equal.
 * /*from  w w  w. j  ava2 s . co m*/
 * @param node1: The first instruction.
 * @param node2: The second instruction.
 * @return boolean: True if they are the same, false if they are not.
 */
public static boolean insnEqual(AbstractInsnNode node1, AbstractInsnNode node2) {

    if (node1.getType() != node2.getType())
        return false;

    else if (node1.getOpcode() != node2.getOpcode())
        return false;

    switch (node2.getType()) {

    case VAR_INSN:
        return varInsnEqual((VarInsnNode) node1, (VarInsnNode) node2);

    case TYPE_INSN:
        return typeInsnEqual((TypeInsnNode) node1, (TypeInsnNode) node2);

    case FIELD_INSN:
        return fieldInsnEqual((FieldInsnNode) node1, (FieldInsnNode) node2);

    case METHOD_INSN:
        return methodInsnEqual((MethodInsnNode) node1, (MethodInsnNode) node2);

    case LDC_INSN:
        return ldcInsnEqual((LdcInsnNode) node1, (LdcInsnNode) node2);

    case IINC_INSN:
        return iincInsnEqual((IincInsnNode) node1, (IincInsnNode) node2);

    case INT_INSN:
        return intInsnEqual((IntInsnNode) node1, (IntInsnNode) node2);

    default:
        return true;
    }
}

From source file:net.lyonlancer5.mcmp.karasu.asm.KarasuTransformer.java

License:Apache License

private static void transformEntityLivingBase(ClassNode classNode) {
    Constants.LOGGER.info("======== Project Karasu ~ Water Walking Enchantment Patch ========");
    Constants.LOGGER.info("Patching net.minecraft.entity.EntityLivingBase");

    for (MethodNode method : classNode.methods) {
        if (method.desc.equals("(FF)V")) {

            AbstractInsnNode movefNode = null;
            AbstractInsnNode motionxNode = null;
            AbstractInsnNode motionzNode = null;
            AbstractInsnNode motionyNode = null;
            AbstractInsnNode[] moveList = method.instructions.toArray();
            int var11 = moveList.length;

            for (int var12 = 0; var12 < var11; ++var12) {
                AbstractInsnNode instruction = moveList[var12];
                if (instruction.getOpcode() == Opcodes.ALOAD) {
                    if (((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == Opcodes.DUP
                            && motionxNode == null) {
                        motionxNode = instruction;
                    } else if (((VarInsnNode) instruction).var == 0
                            && instruction.getNext().getOpcode() == Opcodes.DUP && motionyNode == null) {
                        motionyNode = instruction;
                    } else if (((VarInsnNode) instruction).var == 0
                            && instruction.getNext().getOpcode() == Opcodes.DUP && motionzNode == null) {
                        motionzNode = instruction;
                    } else if (movefNode == null && ((VarInsnNode) instruction).var == 0
                            && instruction.getNext().getOpcode() == Opcodes.FLOAD
                            && ((VarInsnNode) instruction.getNext()).var == 1) {
                        movefNode = instruction;
                    }/*  w ww  .  j  av  a 2 s  .c  o m*/
                }
            }

            int var14;
            InsnList var15;

            if (method.name.equals("e")) {
                Constants.LOGGER.info("Found matching method: " + method.name + " " + method.desc + " - (Obf)");
                if (motionxNode != null) {
                    for (var14 = 0; var14 < 6; ++var14) {
                        motionxNode = motionxNode.getNext();
                        method.instructions.remove(motionxNode.getPrevious());
                    }
                    Constants.LOGGER.info("Patching sv.w");
                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new InsnNode(Opcodes.DUP));
                    var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "w",
                            "D"));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel",
                            "()D", false));
                    var15.add(new InsnNode(Opcodes.DMUL));
                    var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "w",
                            "D"));
                    method.instructions.insertBefore(motionxNode, var15);
                    Constants.LOGGER.info("Patched sv.w");
                }

                if (motionzNode != null) {
                    for (var14 = 0; var14 < 6; ++var14) {
                        motionzNode = motionzNode.getNext();
                        method.instructions.remove(motionzNode.getPrevious());
                    }

                    Constants.LOGGER.info("Patching sv.y");
                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new InsnNode(Opcodes.DUP));
                    var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "y",
                            "D"));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel",
                            "()D", false));
                    var15.add(new InsnNode(Opcodes.DMUL));
                    var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "y",
                            "D"));
                    method.instructions.insertBefore(motionzNode, var15);
                    Constants.LOGGER.info("Patched sv.y");
                }

                if (movefNode != null) {
                    for (var14 = 0; var14 < 14; ++var14) {
                        movefNode = movefNode.getNext();
                        method.instructions.remove(movefNode.getPrevious());
                    }

                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new VarInsnNode(Opcodes.FLOAD, 1));
                    var15.add(new VarInsnNode(Opcodes.FLOAD, 2));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyVerticalVel", "()F",
                            false));
                    var15.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                            "a", "(FFF)V", false));
                    method.instructions.insertBefore(movefNode, var15);
                    Constants.LOGGER.info("Patching method -- sv.a (FFF)V");
                }

                Constants.LOGGER.info("Patch success!");
                return;
            } else if (method.name.equals("moveEntityWithHeading")) {
                Constants.LOGGER
                        .info("Found matching method: " + method.name + " " + method.desc + " - (Deobf)");
                if (motionxNode != null) {
                    for (var14 = 0; var14 < 6; ++var14) {
                        motionxNode = motionxNode.getNext();
                        method.instructions.remove(motionxNode.getPrevious());
                    }

                    Constants.LOGGER.info("Patching EntityLivingBase.motionX");
                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new InsnNode(Opcodes.DUP));
                    var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                            "motionX", "D"));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel",
                            "()D", false));
                    var15.add(new InsnNode(Opcodes.DMUL));
                    var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                            "motionX", "D"));
                    method.instructions.insertBefore(motionxNode, var15);
                    Constants.LOGGER.info("Patched EntityLivingBase.motionX");
                }

                if (motionzNode != null) {
                    for (var14 = 0; var14 < 6; ++var14) {
                        motionzNode = motionzNode.getNext();
                        method.instructions.remove(motionzNode.getPrevious());
                    }

                    Constants.LOGGER.info("Patching EntityLivingBase.motionZ");
                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new InsnNode(Opcodes.DUP));
                    var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                            "motionZ", "D"));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel",
                            "()D", false));
                    var15.add(new InsnNode(Opcodes.DMUL));
                    var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                            "motionZ", "D"));
                    method.instructions.insertBefore(motionzNode, var15);
                    Constants.LOGGER.info("Patched EntityLivingBase.motionZ");
                }

                if (movefNode != null) {
                    for (var14 = 0; var14 < 14; ++var14) {
                        movefNode = movefNode.getNext();
                        method.instructions.remove(movefNode.getPrevious());
                    }

                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new VarInsnNode(Opcodes.FLOAD, 1));
                    var15.add(new VarInsnNode(Opcodes.FLOAD, 2));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyVerticalVel", "()F",
                            false));
                    var15.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                            "moveFlying", "(FFF)V", false));
                    method.instructions.insertBefore(movefNode, var15);
                    Constants.LOGGER.info("Patched method - EntityLivingBase.moveFlying (FFF)V");
                }

                Constants.LOGGER.info("Patch success!");
                return;
            } else if (method.name.equals("func_70612_e")) {
                Constants.LOGGER.info("Found matching method: " + method.name + " " + method.desc + " - (srg)");
                if (motionxNode != null) {
                    for (var14 = 0; var14 < 6; ++var14) {
                        motionxNode = motionxNode.getNext();
                        method.instructions.remove(motionxNode.getPrevious());
                    }

                    Constants.LOGGER.info("Patching EntityLivingBase.field_70159_w");
                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new InsnNode(Opcodes.DUP));
                    var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                            "field_70159_w", "D"));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel",
                            "()D", false));
                    var15.add(new InsnNode(Opcodes.DMUL));
                    var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                            "field_70159_w", "D"));
                    method.instructions.insertBefore(motionxNode, var15);
                    Constants.LOGGER.info("Patched EntityLivingBase.field_70159_w");
                }

                if (motionzNode != null) {
                    for (var14 = 0; var14 < 6; ++var14) {
                        motionzNode = motionzNode.getNext();
                        method.instructions.remove(motionzNode.getPrevious());
                    }

                    Constants.LOGGER.info("Patching EntityLivingBase.field_70179_y");
                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new InsnNode(Opcodes.DUP));
                    var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                            "field_70179_y", "D"));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel",
                            "()D", false));
                    var15.add(new InsnNode(Opcodes.DMUL));
                    var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                            "field_70179_y", "D"));
                    method.instructions.insertBefore(motionzNode, var15);
                    Constants.LOGGER.info("Patched EntityLivingBase.field_70179_y");
                }

                if (movefNode != null) {
                    for (var14 = 0; var14 < 14; ++var14) {
                        movefNode = movefNode.getNext();
                        method.instructions.remove(movefNode.getPrevious());
                    }

                    var15 = new InsnList();
                    var15.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    var15.add(new VarInsnNode(Opcodes.FLOAD, 1));
                    var15.add(new VarInsnNode(Opcodes.FLOAD, 2));
                    var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                            "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyVerticalVel", "()F",
                            false));
                    var15.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                            "func_70060_a", "(FFF)V", false));
                    method.instructions.insertBefore(movefNode, var15);
                    Constants.LOGGER.info("Patched method - EntityLivingBase.func_70060_a (FFF)V");
                }

                Constants.LOGGER.info("Patch success!");
                return;
            }
        }
    }
    // #OVERKILL
    throw new RuntimeException("Patch FAILED -- No matching method found");

}