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:org.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static List<Integer> insnListFind(InsnList haystack, InsnList needle) {

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

    for (int start = 0; start <= haystack.size() - needle.size(); start++)
        if (insnListMatches(haystack, needle, start)) {
            list.add(start);/*  w ww  . j  a v a  2s.c  o  m*/
        }

    return list;
}

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

License:Creative Commons License

public static List<AbstractInsnNode> insnListFindEnd(InsnList haystack, InsnList needle) {

    final LinkedList<AbstractInsnNode> callNodes = new LinkedList<>();

    for (final int callPoint : insnListFind(haystack, needle)) {
        callNodes.add(haystack.get(callPoint + needle.size() - 1));
    }/*from w w  w .  j a v  a2  s.  com*/

    return callNodes;
}

From source file:org.evosuite.regression.bytecode.RegressionClassDiff.java

License:Open Source License

private static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) {
    HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>();
    try {/*  w  w w.  ja va  2  s. com*/
        ClassReader reader = new ClassReader(classAsInputStream);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, 0);
        @SuppressWarnings("unchecked")
        final List<MethodNode> methods = classNode.methods;
        Printer printer = new Textifier();
        TraceMethodVisitor mp = new TraceMethodVisitor(printer);
        for (MethodNode m : methods) {
            List<Integer> instructions = new ArrayList<>();

            InsnList inList = m.instructions;

            String mathodID = m.name + ": " + m.desc;
            System.out.println(mathodID);
            int[] methodInstructions = new int[inList.size()];
            for (int i = 0; i < inList.size(); i++) {
                int op = inList.get(i).getOpcode();
                methodInstructions[i] = op;
                AbstractInsnNode insn = inList.get(i);
                insn.accept(mp);

                // Uncomment the following comment block to print the bytecode
                // instructions
                // StringWriter sw = new StringWriter();
                // printer.print(new PrintWriter(sw));
                // printer.getText().clear();
                // System.out.println(sw.toString());
                // logger.warn("{} -> {}", sw.toString(), op);
                if (op != -1)
                    instructions.add(op);
            }
            methodInstructionsMap.put(mathodID, instructions);
        }
    } catch (IOException e) {
        // Will fail if ClassReader fails
        e.printStackTrace();
    }
    return methodInstructionsMap;
}

From source file:org.evosuite.regression.RegressionClassDiff.java

License:Open Source License

public static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) {
    HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>();
    try {/*from  w  w  w .ja  v a 2 s  .c  om*/
        ClassReader reader = new ClassReader(classAsInputStream);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, 0);
        @SuppressWarnings("unchecked")
        final List<MethodNode> methods = classNode.methods;
        Printer printer = new Textifier();
        TraceMethodVisitor mp = new TraceMethodVisitor(printer);
        for (MethodNode m : methods) {
            List<Integer> instructions = new ArrayList<>();

            InsnList inList = m.instructions;

            String mathodID = m.name + ": " + m.desc;
            System.out.println(mathodID);
            int[] methodInstructions = new int[inList.size()];
            for (int i = 0; i < inList.size(); i++) {
                int op = inList.get(i).getOpcode();
                methodInstructions[i] = op;
                AbstractInsnNode insn = inList.get(i);
                insn.accept(mp);

                // Uncomment the following comment block to print the bytecode
                // instructions
                // StringWriter sw = new StringWriter();
                // printer.print(new PrintWriter(sw));
                // printer.getText().clear();
                // System.out.println(sw.toString());
                // logger.warn("{} -> {}", sw.toString(), op);
                if (op != -1)
                    instructions.add(op);
            }
            methodInstructionsMap.put(mathodID, instructions);
        }
    } catch (IOException e) {
        // Will fail if ClassReader fails
        e.printStackTrace();
    }
    return methodInstructionsMap;
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/*  ww  w.  ja v  a2  s  .c  om*/
    FieldInsnNode fieldIns = null;

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
                final InsnList il = new InsnList();

                if (opcode == Opcodes.GETFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        il.add(new InsnNode(Opcodes.SWAP));
                    } else if (fieldType.getSize() == 2) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        // v
                        // GETFIELD
                        // v, w
                        il.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        il.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        // -> Call
                        // w
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class),
                        "notifyReadAccess",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/*from ww w .ja v a2s  .com*/
    FieldInsnNode fieldIns = null;

    // needed get right receiver var in case of PUTFIELD

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
                // construction of  
                //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
                // call
                final InsnList il = new InsnList();

                if (opcode == Opcodes.PUTFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2));
                        il.add(new InsnNode(Opcodes.POP));
                    } else if (fieldType.getSize() == 2) {
                        InsnList uglyList = new InsnList();
                        // v, w
                        uglyList.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        uglyList.add(new InsnNode(Opcodes.DUP));
                        // w, v, v
                        uglyList.add(new InsnNode(Opcodes.DUP2_X2));
                        // v, v, w, v, v
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // v, v, w
                        instructions.insertBefore(fieldIns, uglyList);
                        // PUTFIELD
                        // v
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions
                this.captureId++;
                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.spongepowered.asm.mixin.transformer.MixinTransformer.java

License:MIT License

/**
 * (Attempts to) find and patch field initialisers from the mixin into the
 * target class//from w  w w  .  j  a  v a 2s  .  co  m
 * 
 * @param targetClass
 * @param mixin
 */
private void applyInitialisers(ClassNode targetClass, MixinTargetContext mixin) {
    // Try to find a suitable constructor, we need a constructor with line numbers in order to extract the initialiser 
    MethodNode ctor = this.getConstructor(mixin);
    if (ctor == null) {
        return;
    }

    // Find the initialiser instructions in the candidate ctor
    InsnList initialiser = this.getInitialiser(mixin, ctor);
    if (initialiser == null || initialiser.size() == 0) {
        return;
    }

    // Patch the initialiser into the target class ctors
    for (MethodNode method : targetClass.methods) {
        if (MixinTransformer.INIT.equals(method.name)) {
            method.maxStack = Math.max(method.maxStack, ctor.maxStack);
            this.injectInitialiser(method, initialiser);
        }
    }
}

From source file:org.springsource.loaded.TypeDiffComputer.java

License:Apache License

/**
 * Determine if there any differences between the methods supplied. A MethodDelta object is built to record any differences and
 * stored against the type delta./*www . j a v  a  2  s .c  o m*/
 * 
 * @param oMethod 'old' method
 * @param nMethod 'new' method
 * @param td the type delta where changes are currently being accumulated
 */
private static void computeAnyMethodDifferences(MethodNode oMethod, MethodNode nMethod, TypeDelta td) {
    MethodDelta md = new MethodDelta(oMethod.name, oMethod.desc);
    if (oMethod.access != nMethod.access) {
        md.setAccessChanged(oMethod.access, nMethod.access);
    }
    // TODO annotations
    InsnList oInstructions = oMethod.instructions;
    InsnList nInstructions = nMethod.instructions;
    if (oInstructions.size() != nInstructions.size()) {
        md.setInstructionsChanged(oInstructions.toArray(), nInstructions.toArray());
    } else {
        // TODO Just interested in constructors right now - should add others
        if (oMethod.name.charAt(0) == '<') {
            String oInvokeSpecialDescriptor = null;
            String nInvokeSpecialDescriptor = null;
            int oUninitCount = 0;
            int nUninitCount = 0;
            boolean codeChange = false;
            for (int i = 0, max = oInstructions.size(); i < max; i++) {
                AbstractInsnNode oInstruction = oInstructions.get(i);
                AbstractInsnNode nInstruction = nInstructions.get(i);
                if (!codeChange) {
                    if (!sameInstruction(oInstruction, nInstruction)) {
                        codeChange = true;
                    }

                }
                if (oInstruction.getType() == AbstractInsnNode.TYPE_INSN) {
                    if (oInstruction.getOpcode() == Opcodes.NEW) {
                        oUninitCount++;
                    }
                }
                if (nInstruction.getType() == AbstractInsnNode.TYPE_INSN) {
                    if (nInstruction.getOpcode() == Opcodes.NEW) {
                        nUninitCount++;
                    }
                }
                if (oInstruction.getType() == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode mi = (MethodInsnNode) oInstruction;
                    if (mi.getOpcode() == INVOKESPECIAL && mi.name.equals("<init>")) {
                        if (oUninitCount == 0) {
                            // this is the one!
                            oInvokeSpecialDescriptor = mi.desc;
                        } else {
                            oUninitCount--;
                        }
                    }
                }
                if (nInstruction.getType() == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode mi = (MethodInsnNode) nInstruction;
                    if (mi.getOpcode() == INVOKESPECIAL && mi.name.equals("<init>")) {
                        if (nUninitCount == 0) {
                            // this is the one!
                            nInvokeSpecialDescriptor = mi.desc;
                        } else {
                            nUninitCount--;
                        }
                    }
                }
            }
            // Has the invokespecial changed?
            if (oInvokeSpecialDescriptor == null) {
                if (nInvokeSpecialDescriptor != null) {
                    md.setInvokespecialChanged(oInvokeSpecialDescriptor, nInvokeSpecialDescriptor);
                }
            } else {
                if (!oInvokeSpecialDescriptor.equals(nInvokeSpecialDescriptor)) {
                    md.setInvokespecialChanged(oInvokeSpecialDescriptor, nInvokeSpecialDescriptor);
                }
            }
            if (codeChange) {
                md.setCodeChanged(oInstructions.toArray(), nInstructions.toArray());
            }
        }
    }
    if (md.hasAnyChanges()) {
        // it needs recording
        td.addChangedMethod(md);
    }

}

From source file:org.teavm.parsing.ProgramParser.java

License:Apache License

public Program parse(MethodNode method, String className) {
    program = new Program();
    this.currentClassName = className;
    InsnList instructions = method.instructions;
    if (instructions.size() == 0) {
        return program;
    }/*  w  w w.  ja v a2  s .  c  om*/
    prepare(method);
    program.createBasicBlock();
    getBasicBlock(0);
    JumpInstruction insn = new JumpInstruction();
    insn.setTarget(program.basicBlockAt(1));
    program.basicBlockAt(0).getInstructions().add(insn);
    doAnalyze(method);
    assemble(method);
    for (int i = 0; i < program.basicBlockCount(); ++i) {
        BasicBlock block = program.basicBlockAt(i);
        for (int j = 0; j < block.getTryCatchBlocks().size(); ++j) {
            TryCatchBlock tryCatch = block.getTryCatchBlocks().get(j);
            if (tryCatch.getHandler() == block) {
                block.getTryCatchBlocks().remove(j--);
            }
        }
    }
    int signatureVars = countSignatureVariables(method.desc);
    while (program.variableCount() <= signatureVars) {
        program.createVariable();
    }
    program.basicBlockAt(0).getTryCatchBlocks()
            .addAll(ProgramUtils.copyTryCatches(program.basicBlockAt(1), program));
    return program;
}

From source file:org.teavm.parsing.ProgramParser.java

License:Apache License

private void prepare(MethodNode method) {
    InsnList instructions = method.instructions;
    minLocal = 0;/*from www  .  j av  a  2s.  c  om*/
    if ((method.access & Opcodes.ACC_STATIC) != 0) {
        minLocal = 1;
    }
    labelIndexes = new HashMap<>();
    lineNumbers = new HashMap<>();
    for (int i = 0; i < instructions.size(); ++i) {
        AbstractInsnNode node = instructions.get(i);
        if (node instanceof LabelNode) {
            labelIndexes.put(((LabelNode) node).getLabel(), i);
        }
        if (node instanceof LineNumberNode) {
            LineNumberNode lineNumberNode = (LineNumberNode) node;
            lineNumbers.put(lineNumberNode.start.getLabel(), lineNumberNode.line);
        }
    }
    for (LocalVariableNode localVar : method.localVariables) {
        int location = labelIndexes.get(localVar.start.getLabel());
        List<LocalVariableNode> vars = localVariableMap.get(location);
        if (vars == null) {
            vars = new ArrayList<>();
            localVariableMap.put(location, vars);
        }
        vars.add(localVar);
    }
    targetInstructions = new ArrayList<>(instructions.size());
    targetInstructions.addAll(Collections.nCopies(instructions.size(), null));
    basicBlocks.addAll(Collections.nCopies(instructions.size(), null));
    stackBefore = new StackFrame[instructions.size()];
    stackAfter = new StackFrame[instructions.size()];
}