Example usage for org.objectweb.asm Opcodes ICONST_0

List of usage examples for org.objectweb.asm Opcodes ICONST_0

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ICONST_0.

Prototype

int ICONST_0

To view the source code for org.objectweb.asm Opcodes ICONST_0.

Click Source Link

Usage

From source file:org.evosuite.instrumentation.ContainerTransformation.java

License:Open Source License

private static InsnList createNewIfThenElse(MethodInsnNode n) {
    LabelNode labelIsNotEmpty = new LabelNode();
    LabelNode labelEndif = new LabelNode();
    InsnList il = new InsnList();
    il.add(n);//ww  w .  j ava  2s  .com
    il.add(new JumpInsnNode(Opcodes.IFLE, labelIsNotEmpty));
    il.add(new InsnNode(Opcodes.ICONST_1));
    il.add(new JumpInsnNode(Opcodes.GOTO, labelEndif));
    il.add(labelIsNotEmpty);
    il.add(new InsnNode(Opcodes.ICONST_0));
    il.add(labelEndif);
    return il;
}

From source file:org.evosuite.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

private void createEmptyStaticReset() {
    logger.info("Creating brand-new static initializer in class {}", className);
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, ClassResetter.STATIC_RESET,
            "()V", null, null);
    mv.visitCode();/*  www.j a  v  a  2  s  .  c  om*/
    for (StaticField staticField : static_fields) {

        if (!finalFields.contains(staticField.name)) {

            logger.info("Adding bytecode for initializing field {}", staticField.name);

            if (staticField.value != null) {
                mv.visitLdcInsn(staticField.value);
            } else {
                Type type = Type.getType(staticField.desc);
                switch (type.getSort()) {
                case Type.BOOLEAN:
                case Type.BYTE:
                case Type.CHAR:
                case Type.SHORT:
                case Type.INT:
                    mv.visitInsn(Opcodes.ICONST_0);
                    break;
                case Type.FLOAT:
                    mv.visitInsn(Opcodes.FCONST_0);
                    break;
                case Type.LONG:
                    mv.visitInsn(Opcodes.LCONST_0);
                    break;
                case Type.DOUBLE:
                    mv.visitInsn(Opcodes.DCONST_0);
                    break;
                case Type.ARRAY:
                case Type.OBJECT:
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    break;
                }
            }
            mv.visitFieldInsn(Opcodes.PUTSTATIC, className, staticField.name, staticField.desc);

        }
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

From source file:org.evosuite.instrumentation.mutation.ReplaceConstant.java

License:Open Source License

private Object getValue(AbstractInsnNode constant) {
    switch (constant.getOpcode()) {
    case Opcodes.LDC:
        return ((LdcInsnNode) constant).cst;
    case Opcodes.ICONST_0:
        return 0;
    case Opcodes.ICONST_1:
        return 1;
    case Opcodes.ICONST_2:
        return 2;
    case Opcodes.ICONST_3:
        return 3;
    case Opcodes.ICONST_4:
        return 4;
    case Opcodes.ICONST_5:
        return 5;
    case Opcodes.ICONST_M1:
        return -1;
    case Opcodes.LCONST_0:
        return 0L;
    case Opcodes.LCONST_1:
        return 1L;
    case Opcodes.DCONST_0:
        return 0.0;
    case Opcodes.DCONST_1:
        return 1.0;
    case Opcodes.FCONST_0:
        return 0.0F;
    case Opcodes.FCONST_1:
        return 1.0F;
    case Opcodes.FCONST_2:
        return 2.0F;
    case Opcodes.SIPUSH:
        return ((IntInsnNode) constant).operand;
    case Opcodes.BIPUSH:
        return ((IntInsnNode) constant).operand;
    default://  w  ww  . j a v  a 2  s.  c o m
        throw new RuntimeException("Unknown constant: " + constant.getOpcode());
    }
}

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

License:Open Source License

/**
 * Replace boolean-returning method calls on String classes
 * //from  w w  w .j a v  a2s  .c o  m
 * @param mn
 */
@SuppressWarnings("unchecked")
private boolean transformStrings(MethodNode mn) {
    logger.info("Current method: {}", mn.name);
    boolean changed = false;
    ListIterator<AbstractInsnNode> iterator = mn.instructions.iterator();
    while (iterator.hasNext()) {
        AbstractInsnNode node = iterator.next();
        if (node instanceof MethodInsnNode) {
            MethodInsnNode min = (MethodInsnNode) node;
            if (min.owner.equals("java/lang/String")) {
                if (min.name.equals("equals")) {
                    changed = true;

                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringEquals",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(Object.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    /*
                            MethodInsnNode equalCheck = new MethodInsnNode(
                                    Opcodes.INVOKESTATIC,
                                    Type.getInternalName(BooleanHelper.class),
                                    "StringEqualsCharacterDistance",
                                    Type.getMethodDescriptor(Type.DOUBLE_TYPE,
                                                             new Type[] {
                                                                     Type.getType(String.class),
                                                                     Type.getType(Object.class) }));
                            mn.instructions.insertBefore(node, equalCheck);
                            mn.instructions.insertBefore(node, new LdcInsnNode(0.0));
                            mn.instructions.insertBefore(node, new InsnNode(Opcodes.DCMPG));
                            mn.instructions.remove(node);
                            */
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("equalsIgnoreCase")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringEqualsIgnoreCase",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("startsWith")) {
                    changed = true;
                    if (min.desc.equals("(Ljava/lang/String;)Z")) {
                        mn.instructions.insertBefore(node, new InsnNode(Opcodes.ICONST_0));
                    }
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringStartsWith",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class),
                                    Type.getType(String.class), Type.INT_TYPE }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("endsWith")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringEndsWith",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("isEmpty")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringIsEmpty",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();
                } else if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringMatches",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();
                } else if (min.name.equals("regionMatches")) {
                    Type[] argumentTypes = Type.getArgumentTypes(min.desc);
                    if (argumentTypes.length == 4) {
                        changed = true;
                        MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "StringRegionMatches",
                                Type.getMethodDescriptor(Type.INT_TYPE,
                                        new Type[] { Type.getType(String.class), Type.INT_TYPE,
                                                Type.getType(String.class), Type.INT_TYPE, Type.INT_TYPE }));
                        mn.instructions.insertBefore(node, equalCheck);
                        mn.instructions.remove(node);
                        TransformationStatistics.transformedStringComparison();

                    } else if (argumentTypes.length == 5) {
                        changed = true;
                        MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "StringRegionMatches",
                                Type.getMethodDescriptor(Type.INT_TYPE,
                                        new Type[] { Type.getType(String.class), Type.BOOLEAN_TYPE,
                                                Type.INT_TYPE, Type.getType(String.class), Type.INT_TYPE,
                                                Type.INT_TYPE }));
                        mn.instructions.insertBefore(node, equalCheck);
                        mn.instructions.remove(node);
                        TransformationStatistics.transformedStringComparison();
                    }
                }

            } else if (min.owner.equals("java/util/regex/Pattern")) {
                if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringMatchRegex",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class),
                                    Type.getType(CharSequence.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                }
            } else if (min.owner.equals("java/util/regex/Matcher")) {
                if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringMatchRegex",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(Matcher.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                }
            }
        }
    }
    return changed;
}

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

License:Open Source License

/**
 * <p>/*w w  w.  jav  a 2  s .c om*/
 * 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);
                        // branch.setOpcode(Opcodes.IFGE);
                    }
                } 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);
                        // branch.setOpcode(Opcodes.IFNE);
                    }
                } 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 }));

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

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  .jav  a2 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.StringTransformation.java

License:Open Source License

/**
 * Replace boolean-returning method calls on String classes
 * //from w  w  w  .j a  v a 2  s  .c o m
 * @param mn
 */
@SuppressWarnings("unchecked")
private boolean transformStrings(MethodNode mn) {
    logger.info("Current method: " + mn.name);
    boolean changed = false;
    ListIterator<AbstractInsnNode> iterator = mn.instructions.iterator();
    while (iterator.hasNext()) {
        AbstractInsnNode node = iterator.next();
        if (node instanceof MethodInsnNode) {
            MethodInsnNode min = (MethodInsnNode) node;
            if (min.owner.equals("java/lang/String")) {
                if (min.name.equals("equals")) {
                    changed = true;

                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringEquals",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(Object.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    /*
                            MethodInsnNode equalCheck = new MethodInsnNode(
                                    Opcodes.INVOKESTATIC,
                                    Type.getInternalName(BooleanHelper.class),
                                    "StringEqualsCharacterDistance",
                                    Type.getMethodDescriptor(Type.DOUBLE_TYPE,
                                                             new Type[] {
                                                                     Type.getType(String.class),
                                                                     Type.getType(Object.class) }));
                            mn.instructions.insertBefore(node, equalCheck);
                            mn.instructions.insertBefore(node, new LdcInsnNode(0.0));
                            mn.instructions.insertBefore(node, new InsnNode(Opcodes.DCMPG));
                            mn.instructions.remove(node);
                            */
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("equalsIgnoreCase")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringEqualsIgnoreCase",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("startsWith")) {
                    changed = true;
                    if (min.desc.equals("(Ljava/lang/String;)Z")) {
                        mn.instructions.insertBefore(node, new InsnNode(Opcodes.ICONST_0));
                    }
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringStartsWith",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class),
                                    Type.getType(String.class), Type.INT_TYPE }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("endsWith")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringEndsWith",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("isEmpty")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringIsEmpty",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();
                } else if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringMatches",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();
                } else if (min.name.equals("regionMatches")) {
                    Type[] argumentTypes = Type.getArgumentTypes(min.desc);
                    if (argumentTypes.length == 4) {
                        changed = true;
                        MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(StringHelper.class), "StringRegionMatches",
                                Type.getMethodDescriptor(Type.INT_TYPE,
                                        new Type[] { Type.getType(String.class), Type.INT_TYPE,
                                                Type.getType(String.class), Type.INT_TYPE, Type.INT_TYPE }),
                                false);
                        mn.instructions.insertBefore(node, equalCheck);
                        mn.instructions.remove(node);
                        TransformationStatistics.transformedStringComparison();

                    } else if (argumentTypes.length == 5) {
                        changed = true;
                        MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(StringHelper.class), "StringRegionMatches",
                                Type.getMethodDescriptor(Type.INT_TYPE,
                                        new Type[] { Type.getType(String.class), Type.BOOLEAN_TYPE,
                                                Type.INT_TYPE, Type.getType(String.class), Type.INT_TYPE,
                                                Type.INT_TYPE }),
                                false);
                        mn.instructions.insertBefore(node, equalCheck);
                        mn.instructions.remove(node);
                        TransformationStatistics.transformedStringComparison();
                    }
                }

            } else if (min.owner.equals("java/util/regex/Pattern")) {
                if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringMatchRegex",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class),
                                    Type.getType(CharSequence.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                }
            } else if (min.owner.equals("java/util/regex/Matcher")) {
                if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(StringHelper.class), "StringMatchRegex",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(Matcher.class) }),
                            false);
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                }
            }
        }
    }
    return changed;
}

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

License:Open Source License

/**
 * <p>//  ww w  .  j a v a2 s .  com
 * 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.evosuite.instrumentation.testability.transformer.BooleanCallsTransformer.java

License:Open Source License

@Override
protected AbstractInsnNode transformMethodInsnNode(MethodNode mn, MethodInsnNode methodNode) {
    if (methodNode.owner.equals(Type.getInternalName(BooleanHelper.class)))
        return methodNode;

    methodNode.desc = this.booleanTestabilityTransformation.transformMethodDescriptor(methodNode.owner,
            methodNode.name, methodNode.desc);
    methodNode.name = DescriptorMapping.getInstance().getMethodName(methodNode.owner, methodNode.name,
            methodNode.desc);//w ww.ja va2  s . c o  m
    if (DescriptorMapping.getInstance().isBooleanMethod(methodNode.desc)) {
        BooleanTestabilityTransformation.logger.info("Method needs value transformation: " + methodNode.name);
        if (DescriptorMapping.getInstance().hasBooleanParameters(methodNode.desc)) {
            BooleanTestabilityTransformation.logger
                    .info("Method needs parameter transformation: " + methodNode.name);
            TransformationStatistics.transformBackToBooleanParameter();
            int firstBooleanParameterIndex = -1;
            Type[] types = Type.getArgumentTypes(methodNode.desc);
            for (int i = 0; i < types.length; i++) {
                if (types[i].getDescriptor().equals("Z")) {
                    if (firstBooleanParameterIndex == -1) {
                        firstBooleanParameterIndex = i;
                        break;
                    }
                }
            }
            if (firstBooleanParameterIndex != -1) {
                int numOfPushs = types.length - 1 - firstBooleanParameterIndex;
                //                        int numOfPushs = types.length - firstBooleanParameterIndex;

                if (numOfPushs == 0) {
                    if (!(methodNode.getPrevious().getOpcode() == Opcodes.ICONST_1
                            || methodNode.getPrevious().getOpcode() == Opcodes.ICONST_0)) {

                        //the boolean parameter is the last parameter
                        MethodInsnNode booleanHelperInvoke = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "intToBoolean",
                                Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }));
                        mn.instructions.insertBefore(methodNode, booleanHelperInvoke);
                    }
                } else {
                    InsnList insnlist = new InsnList();

                    for (int i = 0; i < numOfPushs; i++) {
                        MethodInsnNode booleanHelperPushParameter;
                        if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE
                                || types[types.length - 1 - i] == Type.CHAR_TYPE
                                || types[types.length - 1 - i] == Type.BYTE_TYPE
                                || types[types.length - 1 - i] == Type.SHORT_TYPE
                                || types[types.length - 1 - i] == Type.INT_TYPE
                                || types[types.length - 1 - i] == Type.FLOAT_TYPE
                                || types[types.length - 1 - i] == Type.LONG_TYPE
                                || types[types.length - 1 - i] == Type.DOUBLE_TYPE) {
                            if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE) {
                                booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "pushParameter",
                                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }));
                            } else {
                                booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "pushParameter",
                                        Type.getMethodDescriptor(Type.VOID_TYPE,
                                                new Type[] { types[types.length - 1 - i] }));
                            }
                        } else {
                            booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                    Type.getInternalName(BooleanHelper.class), "pushParameter",
                                    Type.getMethodDescriptor(Type.VOID_TYPE,
                                            new Type[] { Type.getType(Object.class) }));
                        }

                        insnlist.add(booleanHelperPushParameter);
                    }
                    for (int i = firstBooleanParameterIndex; i < types.length; i++) {
                        if (i == firstBooleanParameterIndex) {
                            MethodInsnNode booleanHelperInvoke = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                    Type.getInternalName(BooleanHelper.class), "intToBoolean",
                                    Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }));
                            insnlist.add(booleanHelperInvoke);
                        } else {
                            MethodInsnNode booleanHelperPopParameter;
                            boolean objectNeedCast = false;
                            if (types[i] == Type.BOOLEAN_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.CHAR_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterChar",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.BYTE_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterByte",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.SHORT_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterShort",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.INT_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterInt",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.FLOAT_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterFloat",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.LONG_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterLong",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else if (types[i] == Type.DOUBLE_TYPE) {
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterDouble",
                                        Type.getMethodDescriptor(types[i], new Type[] {}));
                            } else {
                                objectNeedCast = true;
                                booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                        Type.getInternalName(BooleanHelper.class), "popParameterObject",
                                        Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}));
                            }

                            insnlist.add(booleanHelperPopParameter);
                            if (objectNeedCast) {
                                TypeInsnNode tin = new TypeInsnNode(Opcodes.CHECKCAST,
                                        types[i].getInternalName());
                                insnlist.add(tin);
                            }
                        }

                    }
                    mn.instructions.insertBefore(methodNode, insnlist);
                }
            }
        }
        if (Type.getReturnType(methodNode.desc).equals(Type.BOOLEAN_TYPE)) {
            BooleanTestabilityTransformation.logger
                    .info("Method needs return transformation: " + methodNode.name);
            TransformationStatistics.transformBackToBooleanParameter();
            MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC,
                    Type.getInternalName(BooleanHelper.class), "booleanToInt",
                    Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.BOOLEAN_TYPE }));
            mn.instructions.insert(methodNode, n);
            return n;
        }
    } else {
        BooleanTestabilityTransformation.logger.info("Method needs no transformation: " + methodNode.name);
    }

    // TODO: If this is a method that is not transformed, and it requires a Boolean parameter
    // then we need to convert this boolean back to an int
    // For example, we could use flow analysis to determine the point where the value is added to the stack
    // and insert a conversion function there
    return methodNode;
}

From source file:org.evosuite.instrumentation.testability.transformer.BooleanDefinitionTransformer.java

License:Open Source License

@Override
protected AbstractInsnNode transformInsnNode(MethodNode mn, InsnNode insnNode) {
    BooleanTestabilityTransformation.logger.info("Checking transformation of InsnNode ");
    if (insnNode.getOpcode() == Opcodes.ICONST_0
            && this.booleanTestabilityTransformation.isBooleanAssignment(insnNode, mn)) {
        TransformationStatistics.insertedGet();
        this.booleanTestabilityTransformation.insertGet(insnNode, mn.instructions);
    } else if (insnNode.getOpcode() == Opcodes.ICONST_1
            && this.booleanTestabilityTransformation.isBooleanAssignment(insnNode, mn)) {
        TransformationStatistics.insertedGet();
        this.booleanTestabilityTransformation.insertGet(insnNode, mn.instructions);
        //} else if (insnNode.getOpcode() == Opcodes.IRETURN
        //        && isBooleanAssignment(insnNode, mn)) {
        //   TransformationStatistics.insertedGet();
        //   insertGetBefore(insnNode, mn.instructions);
    }/*  w w w .  j av a 2 s  .  co m*/
    return insnNode;
}