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

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

Introduction

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

Prototype

int size

To view the source code for org.objectweb.asm.tree InsnList size.

Click Source Link

Document

The number of instructions in this list.

Usage

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:/*from   w  w w .j a v a  2  s  .c o 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:nova.core.wrapper.mc.forge.v17.asm.lib.InstructionComparator.java

License:Open Source License

private static InsnListSection insnListMatchesL(InsnList haystack, InsnList needle, int start,
        HashSet<LabelNode> controlFlowLabels) {
    int h = start, n = 0;
    for (; h < haystack.size() && n < needle.size(); h++) {
        AbstractInsnNode insn = haystack.get(h);
        if (insn.getType() == 15) {
            continue;
        }/*  w w  w.j  a  v a2 s.  c o m*/
        if (insn.getType() == 8 && !controlFlowLabels.contains(insn)) {
            continue;
        }

        if (!insnEqual(haystack.get(h), needle.get(n))) {
            return null;
        }
        n++;
    }
    if (n != needle.size()) {
        return null;
    }

    return new InsnListSection(haystack, start, h - 1);
}

From source file:org.brutusin.instrumentation.utils.Helper.java

License:Apache License

public static void viewByteCode(byte[] bytecode) {
    ClassReader cr = new ClassReader(bytecode);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);//from  w  ww .j  av  a2 s  .c o  m
    final List<MethodNode> mns = cn.methods;
    Printer printer = new Textifier();
    TraceMethodVisitor mp = new TraceMethodVisitor(printer);
    for (MethodNode mn : mns) {
        InsnList inList = mn.instructions;
        System.out.println(mn.name);
        for (int i = 0; i < inList.size(); i++) {
            inList.get(i).accept(mp);
            StringWriter sw = new StringWriter();
            printer.print(new PrintWriter(sw));
            printer.getText().clear();
            System.out.print(sw.toString());
        }
    }
}

From source file:org.coldswap.asm.field.ProtectedStaticFieldReplacer.java

License:Open Source License

public byte[] replace() {
    final ClassNode cn = new ClassNode(Opcodes.ASM5);
    ClassReader cr = new ClassReader(bytes);
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
    cr.accept(cn, 0);/*from   w ww . j  a va 2  s .  c o m*/
    // get a list of public/protected fields.
    Field[] refFields = aClass.getDeclaredFields();
    List asmFields = getFields(cn);
    Iterator iterator = asmFields.iterator();
    while (iterator.hasNext()) {
        final FieldNode fNode = (FieldNode) iterator.next();
        int protectedStatic = Opcodes.ACC_STATIC | Opcodes.ACC_PROTECTED;
        // search only for protected static fields
        if ((fNode.access == protectedStatic)) {
            // check if this field exist in the old loaded class
            // and if not proceed with the trick.
            boolean foundIt = false;
            for (Field refField : refFields) {
                if (fNode.name.equals(refField.getName())) {
                    if (fNode.desc.equals(Type.getType(refField.getType()).getDescriptor())) {
                        foundIt = true;
                        break;
                    }
                }
            }
            if (!foundIt) {
                // register a public static reference replacer
                String contClass = cn.name.substring(cn.name.lastIndexOf("/") + 1);
                String className = TransformerNameGenerator.getProtectedStaticFieldClassName(contClass,
                        fNode.name);
                // make field access as public static
                fNode.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
                replacerManager.registerFieldReferenceReplacer(new ProtectedStaticFieldReferenceReplacer(
                        contClass, fNode, classPackage + className, contClass));
                // remove the static reference from <clinit>
                InsnList insnList = cleanClInit(cn, fNode, true);
                // because the field might be inherited from superclass we need to copy
                // the initializer from the parent.
                if ((insnList == null) || (insnList.size() > 0)) {
                    insnList = inheritedInitCode.get(fNode);
                }
                // create a new class that contains the field
                // before anything change the field access to public static so that it can be referenced
                // from other classes.
                fNode.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
                byte[] newBClass = ByteCodeGenerator.newFieldClass(cn, fNode, insnList,
                        classPackage + className);
                try {
                    String cp = ClassUtil.getClassPath(aClass);
                    ByteCodeClassWriter.setClassPath(cp);
                    ByteCodeClassWriter.writeClass(className, newBClass);
                } catch (IOException e) {
                    logger.warning(e.toString());
                }

                iterator.remove();
                // replace every reference
                replaceReferences(cn, fNode);
            }
        }
    }
    cn.accept(cw);
    return cw.toByteArray();
}

From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }/*from w  ww  .j a v a2 s. c o  m*/
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);

            }
        }
    }
    return insnList;
}

From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }/*from  w ww  .j  a v a 2s .  co  m*/
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);
            }
        }
    }
    return insnList;
}

From source file:org.coldswap.util.ByteCodeGenerator.java

License:Open Source License

/**
 * Creates a new class containing the new static field.
 *
 * @param classNode        containing the old class.
 * @param fieldNode        containing the old field.
 * @param initInstructions a list of instructions that goes into <clinit>.
 * @param className        the name of the new class to be generated.
 * @return an array of bytes which builds the new class.
 *///  w ww  .j  a  v a  2 s. c  o m
@SuppressWarnings("unchecked")
public static byte[] newFieldClass(ClassNode classNode, FieldNode fieldNode, InsnList initInstructions,
        String className) {
    ClassNode newClass = new ClassNode();
    newClass.version = classNode.version;
    newClass.access = Opcodes.ACC_PUBLIC;
    newClass.signature = "L" + className + ";";
    newClass.name = className;
    newClass.superName = "java/lang/Object";
    newClass.fields.add(
            new FieldNode(fieldNode.access, fieldNode.name, fieldNode.desc, fieldNode.desc, fieldNode.value));
    if (initInstructions != null) {
        if (initInstructions.size() > 0) {
            MethodNode mn = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
            InsnList il = mn.instructions;
            il.add(new LabelNode());
            il.add(initInstructions);
            il.add(new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name, fieldNode.desc));
            il.add(new InsnNode(Opcodes.RETURN));
            newClass.methods.add(mn);
        }
    }

    ClassWriter newCWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    newClass.accept(newCWriter);
    return newCWriter.toByteArray();
}

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;//w w w  . j  a  va  2  s .  c  o m
    }
    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();
    }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractCreateDispatchCodeAdapter.java

License:Open Source License

private LabelNode findBreakLabel(InsnList instructions) {
    for (int i = instructions.size() - 1; i >= 0; i--) {
        AbstractInsnNode node = instructions.get(i);
        if (node.getType() == AbstractInsnNode.LABEL) {
            return (LabelNode) node;
        }//from   w w w. jav  a 2  s.co  m
    }
    throw new RuntimeException("Can't find break label to create dispatch code");
}

From source file:org.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<>();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext())
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }//from  ww  w . jav a2 s . co m

    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;
}