Example usage for org.objectweb.asm.tree MethodInsnNode MethodInsnNode

List of usage examples for org.objectweb.asm.tree MethodInsnNode MethodInsnNode

Introduction

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

Prototype

public MethodInsnNode(final int opcode, final String owner, final String name, final String descriptor,
        final boolean isInterface) 

Source Link

Document

Constructs a new MethodInsnNode .

Usage

From source file:appeng.transformer.asm.ASMTweaker.java

License:Open Source License

@Nullable
@Override/*  w  w  w . ja  v a 2s  .c o m*/
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (basicClass == null) {
        return null;
    }

    try {
        if (transformedName != null && this.privateToPublicMethods.containsKey(transformedName)) {
            ClassNode classNode = new ClassNode();
            ClassReader classReader = new ClassReader(basicClass);
            classReader.accept(classNode, 0);

            for (PublicLine set : this.privateToPublicMethods.get(transformedName)) {
                this.makePublic(classNode, set);
            }

            // CALL VIRTUAL!
            if (transformedName.equals("net.minecraft.client.gui.inventory.GuiContainer")) {
                for (MethodNode mn : classNode.methods) {
                    if (mn.name.equals("func_146977_a") || (mn.name.equals("a") && mn.desc.equals("(Lzk;)V"))) {
                        MethodNode newNode = new MethodNode(Opcodes.ACC_PUBLIC, "func_146977_a_original",
                                mn.desc, mn.signature, EXCEPTIONS);
                        newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
                        newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
                        newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name,
                                mn.name, mn.desc, false));
                        newNode.instructions.add(new InsnNode(Opcodes.RETURN));
                        this.log(newNode.name + newNode.desc + " - New Method");
                        classNode.methods.add(newNode);
                        break;
                    }
                }

                for (MethodNode mn : classNode.methods) {
                    if (mn.name.equals("func_73863_a") || mn.name.equals("drawScreen")
                            || (mn.name.equals("a") && mn.desc.equals("(IIF)V"))) {
                        Iterator<AbstractInsnNode> i = mn.instructions.iterator();
                        while (i.hasNext()) {
                            AbstractInsnNode in = i.next();
                            if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
                                MethodInsnNode n = (MethodInsnNode) in;
                                if (n.name.equals("func_146977_a")
                                        || (n.name.equals("a") && n.desc.equals("(Lzk;)V"))) {
                                    this.log(n.name + n.desc + " - Invoke Virtual");
                                    mn.instructions.insertBefore(n, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                                            n.owner, n.name, n.desc, false));
                                    mn.instructions.remove(in);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            classNode.accept(writer);
            return writer.toByteArray();
        }
    } catch (Throwable ignored) {
    }

    return basicClass;
}

From source file:bitmmo.PatternElementTest.java

License:Open Source License

@Test
public void testMethodElement() {
    MethodInsnNode correct = new MethodInsnNode(PUTFIELD, null, null, null, false);
    MethodInsnNode correct2 = new MethodInsnNode(PUTFIELD, null, "good", null, false);
    MethodInsnNode incorrect = new MethodInsnNode(GETFIELD, null, "bad", null, false);

    MethodElement e = new MethodElement(correct);

    assertEquals(true, e.matches(correct));
    assertEquals(true, e.matches(correct2));
    assertEquals(false, e.matches(incorrect));
}

From source file:bitmmo.PatternTest.java

License:Open Source License

@Test
public void testContains() {
    InsnList list = new InsnList();
    // this is just junk code
    // its contents is irrelevant
    list.add(new InsnNode(DUP));
    list.add(new InsnNode(ISUB));
    list.add(new InsnNode(IMUL));
    list.add(new MethodInsnNode(PUTFIELD, null, null, null, false));
    list.add(new InsnNode(IADD));

    Pattern correct = new PatternBuilder().add(new InstructionElement(ISUB), new InstructionElement(IMUL),
            new MethodElement(new MethodInsnNode(PUTFIELD, null, null, null, false))).build();

    Pattern incorrect = new PatternBuilder().add(new InstructionElement(ISUB), new InstructionElement(DUP))
            .build();// w ww  .  j  a va 2  s .c om

    assertEquals(true, correct.contains(list));
    assertEquals(false, incorrect.contains(list));
}

From source file:bitmmo.PatternTest.java

License:Open Source License

@Test
public void testOffset() {
    InsnList list = new InsnList();
    // this is just junk code
    // its contents is irrelevant
    list.add(new InsnNode(DUP));
    list.add(new InsnNode(ISUB));
    list.add(new InsnNode(IMUL));
    list.add(new MethodInsnNode(PUTFIELD, null, null, null, false));
    list.add(new InsnNode(IADD));

    Pattern correct = new PatternBuilder().add(new InstructionElement(ISUB), new InstructionElement(IMUL))
            .build();/* w w  w . j  av  a  2s.com*/
    Pattern correct2 = new PatternBuilder().add(new InstructionElement(IMUL),
            new MethodElement(new MethodInsnNode(PUTFIELD, null, null, null, false))).build();

    assertEquals(1, correct.getOffset(list));
    assertEquals(2, correct2.getOffset(list));
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchData(ClassNode node) {
    Logger.Info("Patching data (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        if (methodNode.name.equals("a") && methodNode.desc.equals("([BBZ)V")) {
            // Data hook patches
            AbstractInsnNode lastNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(lastNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Item", "patchItemNames", "()V", false));
        }//from  www .  j  a  v a2 s.c  o  m
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchRenderer(ClassNode node) {
    Logger.Info("Patching renderer (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        // Renderer present hook
        if (methodNode.desc.equals("(Ljava/awt/Graphics;III)V")) {
            AbstractInsnNode findNode = methodNode.instructions.getFirst();
            FieldInsnNode imageNode = null;

            while (findNode.getOpcode() != Opcodes.POP) {
                findNode = findNode.getNext();
                if (findNode == null) {
                    Logger.Error("Unable to find present hook");
                    break;
                }/*from w w  w  .  ja va2 s  .c  o m*/
            }

            while (findNode.getOpcode() != Opcodes.INVOKESPECIAL) {
                if (findNode.getOpcode() == Opcodes.GETFIELD)
                    imageNode = (FieldInsnNode) findNode;

                AbstractInsnNode prev = findNode.getPrevious();
                methodNode.instructions.remove(findNode);
                findNode = prev;
            }

            methodNode.instructions.insert(findNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Renderer",
                    "present", "(Ljava/awt/Graphics;Ljava/awt/Image;)V", false));
            methodNode.instructions.insert(findNode,
                    new FieldInsnNode(Opcodes.GETFIELD, node.name, imageNode.name, imageNode.desc));
            methodNode.instructions.insert(findNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insert(findNode, new VarInsnNode(Opcodes.ALOAD, 1));
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IILjava/lang/String;IIBI)V")) {
            AbstractInsnNode start = methodNode.instructions.getFirst();
            while (start != null) {
                if (start.getOpcode() == Opcodes.ALOAD && start.getNext().getOpcode() == Opcodes.ILOAD
                        && start.getNext().getNext().getOpcode() == Opcodes.INVOKEVIRTUAL
                        && start.getNext().getNext().getNext().getOpcode() == Opcodes.ISTORE) {
                    break;
                }

                start = start.getNext();
            }
            start = start.getPrevious();

            LabelNode finishLabel = ((JumpInsnNode) start.getPrevious().getPrevious()).label;
            LabelNode failLabel = new LabelNode();

            methodNode.instructions.insertBefore(start, new IntInsnNode(Opcodes.BIPUSH, 126));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPNE, failLabel));

            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPGE, failLabel));

            methodNode.instructions.insertBefore(start, new IntInsnNode(Opcodes.BIPUSH, 126));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPNE, failLabel));

            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_1));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                    "java/lang/String", "substring", "(II)Ljava/lang/String;"));
            methodNode.instructions.insertBefore(start, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I"));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ISTORE, 4));
            methodNode.instructions.insertBefore(start, new IincInsnNode(10, 5));

            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.GOTO, finishLabel));

            methodNode.instructions.insertBefore(start, failLabel);
        }
    }
}

From source file:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

private static InsnList throwExceptionsList(MethodNode methodNode, ClassNode classNode) {
    try {/*  ww  w .j  a v  a2s.co m*/
        String runtimeException = Type.getInternalName(RuntimeException.class);
        Constructor<RuntimeException> constructor = RuntimeException.class.getConstructor(String.class);

        InsnList instructions = new InsnList();
        instructions.add(new TypeInsnNode(Opcodes.NEW, runtimeException));
        instructions.add(new InsnNode(Opcodes.DUP));

        String className = classNode.name.replace('/', '.');
        instructions.add(new LdcInsnNode("Method " + methodNode.name + " in " + className + " not mocked. "
                + "See http://g.co/androidstudio/not-mocked for details."));
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, runtimeException, CONSTRUCTOR,
                Type.getType(constructor).getDescriptor(), false));
        instructions.add(new InsnNode(Opcodes.ATHROW));

        return instructions;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.builtbroken.mc.patch.ClassTransformer.java

/** Fixes {@link net.minecraft.tileentity.TileEntityChest#invalidate()} causing inf loops on chunk edges */
private void injectInvalidateEdit(ClassNode cn) {
    final MethodNode method = ASMUtility.getMethod(cn, "invalidate", "func_145843_s");

    if (method != null) {
        //Create method call
        final InsnList nodeAdd = new InsnList();
        nodeAdd.add(new VarInsnNode(ALOAD, 0));
        nodeAdd.add(new MethodInsnNode(INVOKESTATIC, HOOK_CLASS, "chestInvalidate",
                "(Lnet/minecraft/tileentity/TileEntityChest;)V", false));

        //Inject method call at top of method
        ListIterator<AbstractInsnNode> it = method.instructions.iterator();
        MethodInsnNode checkForAdjacentChests = null;
        while (it.hasNext()) {
            AbstractInsnNode node = it.next();
            if (node instanceof MethodInsnNode) {
                if (((MethodInsnNode) node).name.equals("checkForAdjacentChests")) {
                    checkForAdjacentChests = (MethodInsnNode) node;
                }//w w  w. j a va  2  s  .  c om
            }
        }
        if (checkForAdjacentChests != null) {
            //Inject replacement
            method.instructions.insertBefore(method.instructions.get(method.instructions.size() - 1), nodeAdd);
            //Remove broken code
            method.instructions.remove(checkForAdjacentChests);
        }
    } else {
        CoreMod.logger.error("Failed to find 'public void invalidate()' in TileEntityChest.class");
    }
}

From source file:com.builtbroken.mc.patch.ClassTransformer.java

/** Fixes {@link net.minecraft.world.World#notifyBlockOfNeighborChange(int, int, int, Block)} causing chunks to load */
private void injectNotifyBlockOfNeighborChange(ClassNode cn) {
    final MethodNode method = ASMUtility.getMethod(cn, "notifyBlockOfNeighborChange", "func_147460_e");

    if (method != null) {
        final InsnList edit = new InsnList();
        edit.add(new VarInsnNode(ALOAD, 0));
        edit.add(new VarInsnNode(ILOAD, 1)); //TODO update as needed
        edit.add(new VarInsnNode(ILOAD, 2));
        edit.add(new VarInsnNode(ILOAD, 3));
        edit.add(new VarInsnNode(ALOAD, 4));
        edit.add(new MethodInsnNode(INVOKESTATIC, HOOK_CLASS, "notifyBlockOfNeighborChange",
                "(Lnet/minecraft/world/World;IIILnet/minecraft/block/Block;)V", false));
        edit.add(new InsnNode(RETURN));
        MethodInsnNode m = ASMUtility.getMethodeNode(method, "onNeighborBlockChange", "func_149695_a");
        method.instructions.insertBefore(m, edit);
        method.instructions.remove(m);/*from w  w w.j  av a  2 s . c  om*/
    } else {
        CoreMod.logger.error(
                "Failed to find 'public void notifyBlockOfNeighborChange(int, int, int, Block)' in World.class");
    }
}

From source file:com.builtbroken.profiler.asm.WorldTransformer.java

/** {@link World#setBlock(int, int, int, Block, int, int)} */
private void injectSetBlock(ClassNode cn) {
    MethodNode setBlockMethod = getMethod(cn, "setBlock", "(IIIL" + getName(CLASS_KEY_BLOCK) + ";II)Z");

    if (setBlockMethod != null) {
        //Create method call
        final InsnList nodeAdd = new InsnList();

        nodeAdd.add(new VarInsnNode(Opcodes.ALOAD, 0));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 1));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 2));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 3));
        nodeAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onBlockChange",
                "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));

        //Inject method call at top of method
        setBlockMethod.instructions.insertBefore(setBlockMethod.instructions.get(0), nodeAdd);

        //Locate all return points from the method
        List<AbstractInsnNode> returnNodes = new ArrayList();
        for (int i = 0; i < setBlockMethod.instructions.size(); i++) {
            AbstractInsnNode ain = setBlockMethod.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNodes.add(ain);/*from w w w  .  j  a  va2 s  . c o  m*/
            }
        }

        //Inject calls in front of return points
        for (AbstractInsnNode node : returnNodes) {
            //Create method call
            final InsnList nodeAdd2 = new InsnList();
            nodeAdd2.add(new VarInsnNode(Opcodes.ALOAD, 0));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 1));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 2));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 3));
            nodeAdd2.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onPostBlockChange",
                    "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));
            //Inject method call before return node
            setBlockMethod.instructions.insertBefore(node, nodeAdd2);
        }
    }
}