Search in sources :

Example 96 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project MinecraftForge by MinecraftForge.

the class ItemStackTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (!"net.minecraft.item.ItemStack".equals(name))
        return basicClass;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    FieldNode itemField = null;
    for (FieldNode f : classNode.fields) {
        if (ITEM_TYPE.equals(f.desc) && itemField == null) {
            itemField = f;
        } else if (ITEM_TYPE.equals(f.desc)) {
            throw new RuntimeException("Error processing ItemStack - found a duplicate Item field");
        }
    }
    if (itemField == null) {
        throw new RuntimeException("Error processing ItemStack - no Item field declared (is the code somehow obfuscated?)");
    }
    MethodNode getItemMethod = null;
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("getItemRaw"))
            continue;
        if (GETITEM_DESC.equals(m.desc) && getItemMethod == null) {
            getItemMethod = m;
        } else if (GETITEM_DESC.equals(m.desc)) {
            throw new RuntimeException("Error processing ItemStack - duplicate getItem method found");
        }
    }
    if (getItemMethod == null) {
        throw new RuntimeException("Error processing ItemStack - no getItem method found (is the code somehow obfuscated?)");
    }
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("getItemRaw"))
            continue;
        for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext(); ) {
            AbstractInsnNode insnNode = it.next();
            if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode fi = (FieldInsnNode) insnNode;
                if (itemField.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) {
                    it.remove();
                    MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack", getItemMethod.name, getItemMethod.desc, false);
                    it.add(replace);
                }
            }
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 97 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Bookshelf by Darkhax-Minecraft.

the class ASMUtils method removeNeedleFromHaystack.

/**
     * Removes a specific set of instructions (the needle) from a much larger set of
     * instructions (the hay stack). Be cautious when using this method, as it is almost never
     * a good idea to remove instructions.
     *
     * @param haystack: A large list of instructions which is being searched through.
     * @param needle: A specific list of instructions which are to be removed from the larger
     *        instruction list.
     */
public static void removeNeedleFromHaystack(InsnList haystack, InsnList needle) {
    final int firstInd = haystack.indexOf(findFirstNodeFromNeedle(haystack, needle));
    final int lastInd = haystack.indexOf(findLastNodeFromNeedle(haystack, needle));
    final List<AbstractInsnNode> realNeedle = new ArrayList<>();
    for (int i = firstInd; i <= lastInd; i++) {
        realNeedle.add(haystack.get(i));
    }
    for (final AbstractInsnNode node : realNeedle) {
        haystack.remove(node);
    }
}
Also used : ArrayList(java.util.ArrayList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 98 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Bookshelf by Darkhax-Minecraft.

the class InstructionComparator method getImportantList.

// TODO: Add documentation
public static InsnList getImportantList(InsnList list) {
    if (list.size() == 0) {
        return list;
    }
    final HashMap<LabelNode, LabelNode> labels = new HashMap<>();
    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));
    }
    return importantNodeList;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) HashMap(java.util.HashMap) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList)

Example 99 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project bytecode-viewer by Konloch.

the class NumberNode method number.

public int number() {
    AbstractInsnNode insn = insn();
    int op = insn.opcode();
    switch(op) {
        case NEWARRAY:
        case BIPUSH:
        case SIPUSH:
            {
                return ((IntInsnNode) insn).operand;
            }
        case ICONST_M1:
        case ICONST_0:
        case ICONST_1:
        case ICONST_2:
        case ICONST_3:
        case ICONST_4:
        case ICONST_5:
            {
                return op - ICONST_0;
            }
        case LCONST_0:
        case LCONST_1:
            {
                return op - LCONST_0;
            }
        case FCONST_0:
        case FCONST_1:
        case FCONST_2:
            {
                return op - FCONST_0;
            }
        case DCONST_0:
        case DCONST_1:
            {
                return op - DCONST_0;
            }
        case LDC:
            {
                Object cst = ((LdcInsnNode) insn).cst;
                if (cst instanceof Number) {
                    return ((Number) cst).intValue();
                }
            }
        default:
            {
                return -1;
            }
    }
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 100 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class MethodAnalyzer method findRepresentative.

private AbstractInsnNode findRepresentative(AbstractInsnNode i) {
    AbstractInsnNode r = merged.get(i);
    while (r != null) {
        i = r;
        r = merged.get(i);
    }
    return i;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11