Example usage for org.objectweb.asm.tree AbstractInsnNode getNext

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getNext

Introduction

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

Prototype

public AbstractInsnNode getNext() 

Source Link

Document

Returns the next instruction in the list to which this instruction belongs, if any.

Usage

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * This helper function determines whether the boolean on the stack at the
 * current position will be stored in a Boolean variable
 * //from   w  w  w.j  a va2  s  .c  o  m
 * @param position
 * @param mn
 * @return
 */
public boolean isBooleanAssignment(AbstractInsnNode position, MethodNode mn) {
    AbstractInsnNode node = position.getNext();
    logger.info("Checking for ISTORE after boolean");
    boolean done = false;
    while (!done) {

        if (node.getOpcode() == Opcodes.PUTFIELD || node.getOpcode() == Opcodes.PUTSTATIC) {
            // TODO: Check whether field is static
            logger.info("Checking field assignment");
            FieldInsnNode fn = (FieldInsnNode) node;
            if (Type.getType(DescriptorMapping.getInstance().getFieldDesc(fn.owner, fn.name,
                    fn.desc)) == Type.BOOLEAN_TYPE) {
                return true;
            } else {
                return false;
            }
        } else if (node.getOpcode() == Opcodes.ISTORE) {
            logger.info("Found ISTORE after boolean");

            VarInsnNode vn = (VarInsnNode) node;
            // TODO: Check whether variable at this position is a boolean
            if (isBooleanVariable(vn.var, mn)) {
                logger.info("Assigning boolean to variable ");
                return true;
            } else {
                logger.info("Variable is not a bool");
                return false;
            }
        } else if (node.getOpcode() == Opcodes.IRETURN) {
            logger.info("Checking return value of method " + cn.name + "." + mn.name);
            if (DescriptorMapping.getInstance().isTransformedOrBooleanMethod(cn.name, mn.name, mn.desc)) {
                logger.info("Method returns a bool");
                return true;
            } else {
                logger.info("Method does not return a bool");
                return false;
            }
        } else if (node.getOpcode() == Opcodes.BASTORE) {
            // We remove all bytes, so BASTORE is only used for booleans
            AbstractInsnNode start = position.getNext();
            boolean reassignment = false;
            while (start != node) {
                if (node instanceof InsnNode) {
                    reassignment = true;
                }
                start = start.getNext();
            }
            logger.info("Possible assignment to array?");
            if (reassignment)
                return false;
            else
                return true;

        } else if (node instanceof MethodInsnNode) {
            // if it is a boolean parameter of a converted method, then it needs to be converted
            // Problem: How do we know which parameter it represents?
            MethodInsnNode methodNode = (MethodInsnNode) node;
            String desc = DescriptorMapping.getInstance().getMethodDesc(methodNode.owner, methodNode.name,
                    methodNode.desc);
            Type[] types = Type.getArgumentTypes(desc);
            if (types.length > 0 && types[types.length - 1] == Type.BOOLEAN_TYPE) {
                return true;
            } else {
                return false;
            }

        } else if (node.getOpcode() == Opcodes.GOTO || node.getOpcode() == Opcodes.ICONST_0
                || node.getOpcode() == Opcodes.ICONST_1 || node.getOpcode() == -1) {
            logger.info("Continuing search");

            // continue search
        } else if (!(node instanceof LineNumberNode || node instanceof FrameNode)) {
            logger.info("Search ended with opcode " + node.getOpcode());

            return false;
        }
        if (node != mn.instructions.getLast())
            node = node.getNext();
        else
            done = true;
    }

    return false;
}

From source file:org.evosuite.instrumentation.testability.ComparisonTransformation.java

License:Open Source License

/**
 * <p>transformMethod</p>/*from   w  w w.  j  a  v  a  2  s  .  co  m*/
 *
 * @param mn a {@link org.objectweb.asm.tree.MethodNode} object.
 */
public void transformMethod(MethodNode mn) {
    AbstractInsnNode node = mn.instructions.getFirst();
    while (node != mn.instructions.getLast()) {
        AbstractInsnNode next = node.getNext();
        if (node instanceof InsnNode) {
            InsnNode in = (InsnNode) node;
            if (in.getOpcode() == Opcodes.LCMP) {
                insertLongComparison(in, mn.instructions);
                TransformationStatistics.transformedComparison();
            } else if (in.getOpcode() == Opcodes.DCMPG) {
                TransformationStatistics.transformedComparison();
                insertDoubleComparisonG(in, mn.instructions);
            } else if (in.getOpcode() == Opcodes.DCMPL) {
                TransformationStatistics.transformedComparison();
                insertDoubleComparisonL(in, mn.instructions);
            } else if (in.getOpcode() == Opcodes.FCMPG) {
                TransformationStatistics.transformedComparison();
                insertFloatComparisonG(in, mn.instructions);
            } else if (in.getOpcode() == Opcodes.FCMPL) {
                TransformationStatistics.transformedComparison();
                insertFloatComparisonL(in, mn.instructions);
            }
        }
        node = next;
    }
}

From source file:org.evosuite.instrumentation.testability.MethodNodeTransformer.java

License:Open Source License

/**
 * <p>transform</p>/*w ww.ja v a  2 s  .com*/
 *
 * @param mn a {@link org.objectweb.asm.tree.MethodNode} object.
 */
public void transform(MethodNode mn) {

    setupLocals(mn);

    Set<AbstractInsnNode> originalNodes = new HashSet<AbstractInsnNode>();
    AbstractInsnNode node = mn.instructions.getFirst();
    while (node != mn.instructions.getLast()) {
        originalNodes.add(node);
        node = node.getNext();
    }

    //int currentIndex = 0;

    node = mn.instructions.getFirst();
    //while (currentIndex < mn.instructions.size()) {
    boolean finished = false;
    while (!finished) {
        //while (node != mn.instructions.getLast()) {
        //node = mn.instructions.get(currentIndex);
        //int oldLength = mn.instructions.size();
        //         BytecodeInstruction insn = BytecodeInstructionPool.getInstruction(className,
        //                                                                           mn.name
        //                                                                                   + mn.desc,
        //                                                                           node);
        //         if (insn == null) {
        //            //         if (!originalNodes.contains(node)) {
        //            System.out.println("Node not present in original stuff " + node);
        //            // Only transform nodes present in original method
        //         } else 
        if (node instanceof MethodInsnNode) {
            node = transformMethodInsnNode(mn, (MethodInsnNode) node);
        } else if (node instanceof VarInsnNode) {
            node = transformVarInsnNode(mn, (VarInsnNode) node);
        } else if (node instanceof FieldInsnNode) {
            node = transformFieldInsnNode(mn, (FieldInsnNode) node);
        } else if (node instanceof InsnNode) {
            node = transformInsnNode(mn, (InsnNode) node);
        } else if (node instanceof TypeInsnNode) {
            node = transformTypeInsnNode(mn, (TypeInsnNode) node);
        } else if (node instanceof JumpInsnNode) {
            node = transformJumpInsnNode(mn, (JumpInsnNode) node);
        } else if (node instanceof LabelNode) {
            node = transformLabelNode(mn, (LabelNode) node);
        } else if (node instanceof IntInsnNode) {
            node = transformIntInsnNode(mn, (IntInsnNode) node);
        } else if (node instanceof MultiANewArrayInsnNode) {
            node = transformMultiANewArrayInsnNode(mn, (MultiANewArrayInsnNode) node);
        }
        //currentIndex += mn.instructions.size() - oldLength;
        //currentIndex++;

        if (node == mn.instructions.getLast()) {
            finished = true;
        } else {
            node = node.getNext();
        }
    }
}

From source file:org.evosuite.instrumentation.testability.StringTransformation.java

License:Open Source License

/**
 * <p>//from w ww .  j  a v  a  2s  . c  o  m
 * transformMethod
 * </p>
 * 
 * @param mn
 *            a {@link org.objectweb.asm.tree.MethodNode} object.
 * @return a boolean.
 */
public boolean transformMethod(MethodNode mn) {
    boolean changed = transformStrings(mn);
    if (changed) {
        try {
            mn.maxStack++;
            Analyzer a = new Analyzer(new StringBooleanInterpreter());
            a.analyze(cn.name, mn);
            Frame[] frames = a.getFrames();
            AbstractInsnNode node = mn.instructions.getFirst();
            boolean done = false;
            while (!done) {
                if (node == mn.instructions.getLast())
                    done = true;
                AbstractInsnNode next = node.getNext();
                int index = mn.instructions.indexOf(node);
                if (index >= frames.length)
                    break;
                Frame current = frames[index];
                if (current == null)
                    break;
                int size = current.getStackSize();
                if (node.getOpcode() == Opcodes.IFNE) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious())) {
                        logger.info("IFNE -> IFGT");
                        branch.setOpcode(Opcodes.IFGT);
                    }
                } else if (node.getOpcode() == Opcodes.IFEQ) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious())) {
                        logger.info("IFEQ -> IFLE");
                        branch.setOpcode(Opcodes.IFLE);
                    }
                } else if (node.getOpcode() == Opcodes.IF_ICMPEQ) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious().getPrevious())) {
                        if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) {
                            branch.setOpcode(Opcodes.IFLE);
                            mn.instructions.remove(node.getPrevious());
                        } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) {
                            branch.setOpcode(Opcodes.IFGT);
                            mn.instructions.remove(node.getPrevious());
                        }
                    }
                } else if (node.getOpcode() == Opcodes.IF_ICMPNE) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious().getPrevious())) {
                        if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) {
                            branch.setOpcode(Opcodes.IFGT);
                            mn.instructions.remove(node.getPrevious());
                        } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) {
                            branch.setOpcode(Opcodes.IFLE);
                            mn.instructions.remove(node.getPrevious());
                        }
                    }
                } else if (node.getOpcode() == Opcodes.IRETURN) {
                    if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious())) {
                        logger.info("IFEQ -> IFLE");
                        MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "intToBoolean",
                                Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }),
                                false);

                        mn.instructions.insertBefore(node, n);
                    }
                }
                node = next;
            }
        } catch (Exception e) {
            logger.warn("EXCEPTION DURING STRING TRANSFORMATION: " + e);
            return changed;
        }
    }
    return changed;
}

From source file:org.friz.bytecode.insn.InsnNodeUtility.java

License:Open Source License

public static AbstractInsnNode getNextInsn(AbstractInsnNode n) {
    while (n != null && n.getOpcode() == -1) {
        n = n.getNext();
    }/*from   www.  j  a va2s.  c  o  m*/
    return n;
}

From source file:org.jacoco.core.internal.analysis.filter.AbstractMatcher.java

License:Open Source License

/**
 * Returns first instruction from given and following it that is not
 * {@link AbstractInsnNode#FRAME}, {@link AbstractInsnNode#LABEL},
 * {@link AbstractInsnNode#LINE}.//ww  w . jav a2s.  c  o  m
 */
static AbstractInsnNode skipNonOpcodes(AbstractInsnNode cursor) {
    while (cursor != null && (cursor.getType() == AbstractInsnNode.FRAME
            || cursor.getType() == AbstractInsnNode.LABEL || cursor.getType() == AbstractInsnNode.LINE)) {
        cursor = cursor.getNext();
    }
    return cursor;
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static void filter(final IFilterOutput output, final List<TryCatchBlockNode> tryCatchBlocks,
        final TryCatchBlockNode catchAnyBlock) {
    final AbstractInsnNode e = next(catchAnyBlock.handler);
    final int size = size(e);
    if (size <= 0) {
        return;/*  w ww  . java2  s.  co  m*/
    }

    // Determine instructions inside regions
    final Set<AbstractInsnNode> inside = new HashSet<AbstractInsnNode>();
    for (final TryCatchBlockNode t : tryCatchBlocks) {
        if (t.handler == catchAnyBlock.handler) {
            AbstractInsnNode i = t.start;
            while (i != t.end) {
                inside.add(i);
                i = i.getNext();
            }
        }
    }

    // Find and merge duplicates at exits of regions
    for (final TryCatchBlockNode t : tryCatchBlocks) {
        if (t.handler == catchAnyBlock.handler) {
            boolean continues = false;
            AbstractInsnNode i = t.start;

            while (i != t.end) {
                switch (i.getType()) {
                case AbstractInsnNode.FRAME:
                case AbstractInsnNode.LINE:
                case AbstractInsnNode.LABEL:
                    break;
                case AbstractInsnNode.JUMP_INSN:
                    final AbstractInsnNode jumpTarget = next(((JumpInsnNode) i).label);
                    if (!inside.contains(jumpTarget)) {
                        merge(output, size, e, jumpTarget);
                    }
                    continues = i.getOpcode() != Opcodes.GOTO;
                    break;
                default:
                    switch (i.getOpcode()) {
                    case Opcodes.IRETURN:
                    case Opcodes.LRETURN:
                    case Opcodes.FRETURN:
                    case Opcodes.DRETURN:
                    case Opcodes.ARETURN:
                    case Opcodes.RETURN:
                    case Opcodes.ATHROW:
                        continues = false;
                        break;
                    default:
                        continues = true;
                        break;
                    }
                    break;
                }
                i = i.getNext();
            }

            i = next(i);
            if (continues && !inside.contains(i)) {
                merge(output, size, e, i);
            }
        }

        if (t != catchAnyBlock && t.start == catchAnyBlock.start && t.end == catchAnyBlock.end) {
            final AbstractInsnNode i = next(next(t.handler));
            if (!inside.contains(i)) {
                // javac's empty catch - merge after ASTORE
                merge(output, size, e, i);
            }
        }
    }
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static AbstractInsnNode next(AbstractInsnNode i) {
    do {//  w w w  .ja v  a  2 s . c  o m
        i = i.getNext();
    } while (i != null && (AbstractInsnNode.FRAME == i.getType() || AbstractInsnNode.LABEL == i.getType()
            || AbstractInsnNode.LINE == i.getType()));
    return i;
}

From source file:org.jacoco.core.internal.analysis.filter.StringSwitchEcjFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label case1 = new Label();
    final Label case2 = new Label();
    final Label case3 = new Label();
    final Label caseDefault = new Label();
    final Label h1 = new Label();
    final Label h2 = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);/*from ww  w .  ja  va 2  s. c  o m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitTableSwitchInsn(97, 98, caseDefault, h1, h2);
    final AbstractInsnNode switchNode = m.instructions.getLast();

    m.visitLabel(h1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("\0a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "\0a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case2);

    // goto default case
    m.visitJumpInsn(Opcodes.GOTO, caseDefault);

    m.visitLabel(h2);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("b");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "b", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case3);

    // goto default case
    m.visitJumpInsn(Opcodes.GOTO, caseDefault);
    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case2);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case3);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(caseDefault);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(switchNode, expectedNewTargets);
    assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
}

From source file:org.jacoco.core.internal.analysis.filter.StringSwitchEcjFilterTest.java

License:Open Source License

@Test
public void should_filter_when_default_is_first() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label case1 = new Label();
    final Label caseDefault = new Label();
    final Label h1 = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);/*  w w  w .j av a2 s. c o m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitLookupSwitchInsn(caseDefault, new int[] { 97 }, new Label[] { h1 });
    final AbstractInsnNode switchNode = m.instructions.getLast();

    m.visitLabel(h1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case1);

    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(caseDefault);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(switchNode, expectedNewTargets);
    assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
}