Example usage for org.objectweb.asm.tree InsnList getFirst

List of usage examples for org.objectweb.asm.tree InsnList getFirst

Introduction

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

Prototype

public AbstractInsnNode getFirst() 

Source Link

Document

Returns the first instruction in this list.

Usage

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  ww  w .ja v  a  2s.c  o  m*/
            }
            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.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static InsnList getImportantList(InsnList list) {

    if (list.size() == 0)
        return list;

    final HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext())
        if (insn instanceof LabelNode)
            labels.put((LabelNode) insn, (LabelNode) insn);

    final InsnList importantNodeList = new InsnList();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {

        if (insn instanceof LabelNode || insn instanceof LineNumberNode)
            continue;

        importantNodeList.add(insn.clone(labels));
    }/*www  . j  a  va2s.  c  om*/

    return importantNodeList;
}

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

License:Creative Commons License

public static List<InsnListSection> insnListFindL(InsnList haystack, InsnList needle) {

    final HashSet<LabelNode> controlFlowLabels = new HashSet<LabelNode>();

    for (AbstractInsnNode insn = haystack.getFirst(); insn != null; insn = insn.getNext())
        switch (insn.getType()) {

        case 8:/*from  www  . j av  a  2  s.c o m*/
        case 15:
            break;

        case 7:
            final JumpInsnNode jinsn = (JumpInsnNode) insn;
            controlFlowLabels.add(jinsn.label);
            break;

        case 11:
            final TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
            for (final LabelNode label : tsinsn.labels)
                controlFlowLabels.add(label);
            break;

        case 12:
            final LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
            for (final LabelNode label : lsinsn.labels)
                controlFlowLabels.add(label);
            break;
        }

    final LinkedList<InsnListSection> list = new LinkedList<InsnListSection>();

    nextsection: for (int start = 0; start <= haystack.size() - needle.size(); start++) {
        final InsnListSection section = insnListMatchesL(haystack, needle, start, controlFlowLabels);

        if (section != null) {

            for (final InsnListSection asection : list)
                if (asection.last == section.last)
                    continue nextsection;

            list.add(section);
        }
    }

    return list;
}

From source file:net.malisis.core.asm.AsmUtils.java

License:Open Source License

public static AbstractInsnNode findInstruction(MethodNode method, InsnList matches) {
    AbstractInsnNode node = method.instructions.getFirst();
    AbstractInsnNode match = matches.getFirst();
    while (node != null) {
        if (insnEqual(node, match)) {
            AbstractInsnNode m = match.getNext();
            AbstractInsnNode n = node.getNext();
            while (m != null && n != null && insnEqual(m, n)) {
                m = m.getNext();//from  w ww .  j a  v  a 2 s. c om
                n = n.getNext();
            }
            if (m == null)
                return node;
        }

        node = node.getNext();
    }
    return null;
}

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

License:Open Source License

public static Map<LabelNode, LabelNode> cloneLabels(InsnList insns) {
    HashMap<LabelNode, LabelNode> labelMap = new HashMap<LabelNode, LabelNode>();
    for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn.getType() == 8) {
            labelMap.put((LabelNode) insn, new LabelNode());
        }/* w  w  w  . ja  v  a2  s .  c  om*/
    }
    return labelMap;
}

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

License:Open Source License

public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnList insns) {
    InsnList clone = new InsnList();
    for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
        clone.add(insn.clone(labelMap));
    }/*from   w w w .ja  v a  2  s.  c  o  m*/

    return clone;
}

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

License:Open Source License

public void visitInsnList(InsnList list) {
    text.clear();/*from   w w w. j  a  v a 2 s  . com*/
    if (labelNames == null) {
        labelNames = new HashMap<Label, String>();
    } else {
        labelNames.clear();
    }

    buildingLabelMap = true;
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn.getType() == 8) {
            visitLabel(((LabelNode) insn).getLabel());
        }
    }

    text.clear();
    buildingLabelMap = false;

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        _visitInsn(insn);
    }
}

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

License:Open Source License

public static InsnList getImportantList(InsnList list) {
    if (list.size() == 0) {
        return list;
    }/* w ww .  j a  v  a2 s .c o  m*/

    HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }
    }

    InsnList importantNodeList = new InsnList();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
            continue;
        }

        importantNodeList.add(insn.clone(labels));
    }
    return importantNodeList;
}

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

License:Open Source License

public static List<InsnListSection> insnListFindL(InsnList haystack, InsnList needle) {
    HashSet<LabelNode> controlFlowLabels = new HashSet<LabelNode>();

    for (AbstractInsnNode insn = haystack.getFirst(); insn != null; insn = insn.getNext()) {
        switch (insn.getType()) {
        case 8:/* w ww  .jav a 2  s  .co  m*/
        case 15:
            break;
        case 7:
            JumpInsnNode jinsn = (JumpInsnNode) insn;
            controlFlowLabels.add(jinsn.label);
            break;
        case 11:
            TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
            for (LabelNode label : tsinsn.labels) {
                controlFlowLabels.add(label);
            }
            break;
        case 12:
            LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
            for (LabelNode label : lsinsn.labels) {
                controlFlowLabels.add(label);
            }
            break;
        }
    }

    LinkedList<InsnListSection> list = new LinkedList<InsnListSection>();
    nextsection: for (int start = 0; start <= haystack.size() - needle.size(); start++) {
        InsnListSection section = insnListMatchesL(haystack, needle, start, controlFlowLabels);
        if (section != null) {
            for (InsnListSection asection : list) {
                if (asection.last == section.last) {
                    continue nextsection;
                }
            }

            list.add(section);
        }
    }

    return list;
}

From source file:org.diorite.inject.controller.TransformerFieldInjector.java

License:Open Source License

private void injectFieldsIn(MethodNode rootNode) {
    InsnList instructions = rootNode.instructions;
    if (instructions.size() == 0) {
        return;//  ww  w  . j  a v a2 s  . c om
    }
    AbstractInsnNode node = instructions.getFirst();
    while (node != null) {
        while (!(node instanceof FieldInsnNode) || !AsmUtils.isPutField(node.getOpcode())) {
            node = node.getNext();
            if (node == null) {
                return;
            }
        }
        this.trackFieldToInject((FieldInsnNode) node, rootNode.instructions);
        node = node.getNext();
    }
}