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

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

Introduction

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

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

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

License:Open Source License

/** {@inheritDoc} */
@Override/*from   w ww.j ava 2 s .com*/
public boolean isApplicable(BytecodeInstruction instruction) {
    AbstractInsnNode node = instruction.getASMNode();
    int opcode = node.getOpcode();
    if (opcodesInt.contains(opcode))
        return true;
    else if (opcodesLong.contains(opcode))
        return true;
    else if (opcodesFloat.contains(opcode))
        return true;
    else if (opcodesDouble.contains(opcode))
        return true;

    return false;
}

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

License:Open Source License

/** {@inheritDoc} */
@Override// w w  w  .  j ava  2  s .  c  o  m
public boolean isApplicable(BytecodeInstruction instruction) {
    AbstractInsnNode node = instruction.getASMNode();
    int opcode = node.getOpcode();
    if (opcodesInt.contains(opcode))
        return true;
    else if (opcodesIntShift.contains(opcode))
        return true;
    else if (opcodesLong.contains(opcode))
        return true;
    else if (opcodesLongShift.contains(opcode))
        return true;

    return false;
}

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:/*from  ww w. j  a v  a2 s . co m*/
        throw new RuntimeException("Unknown constant: " + constant.getOpcode());
    }
}

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

License:Open Source License

/**
 * <p>/*from  w w  w  .ja v a  2  s.c  o  m*/
 * copy
 * </p>
 *
 * @param orig
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public static InsnList copy(InsnList orig) {
    Iterator<?> it = orig.iterator();
    InsnList copy = new InsnList();
    while (it.hasNext()) {
        AbstractInsnNode node = (AbstractInsnNode) it.next();

        if (node instanceof VarInsnNode) {
            VarInsnNode vn = (VarInsnNode) node;
            copy.add(new VarInsnNode(vn.getOpcode(), vn.var));
        } else if (node instanceof FieldInsnNode) {
            FieldInsnNode fn = (FieldInsnNode) node;
            copy.add(new FieldInsnNode(fn.getOpcode(), fn.owner, fn.name, fn.desc));
        } else if (node instanceof InsnNode) {
            if (node.getOpcode() != Opcodes.POP)
                copy.add(new InsnNode(node.getOpcode()));
        } else if (node instanceof LdcInsnNode) {
            copy.add(new LdcInsnNode(((LdcInsnNode) node).cst));
        } else {
            throw new RuntimeException("Unexpected node type: " + node.getClass());
        }
    }
    return copy;
}

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

License:Open Source License

/**
 * <p>//from  w w  w .  ja v  a  2s .c om
 * getInfectionDistance
 * </p>
 *
 * @param type
 *            a {@link org.objectweb.asm.Type} object.
 * @param original
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(Type type, AbstractInsnNode original, InsnList mutant) {
    // TODO: Treat reference types different!

    InsnList distance = new InsnList();

    if (original instanceof VarInsnNode) {
        VarInsnNode node = (VarInsnNode) original;
        distance.add(new VarInsnNode(node.getOpcode(), node.var));
        if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
            addReferenceDistanceCheck(distance, type, mutant);
        else
            addPrimitiveDistanceCheck(distance, type, mutant);

    } else if (original instanceof FieldInsnNode) {
        if (original.getOpcode() == Opcodes.GETFIELD)
            distance.add(new InsnNode(Opcodes.DUP)); //make sure to re-load this for GETFIELD

        FieldInsnNode node = (FieldInsnNode) original;
        distance.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
        if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
            addReferenceDistanceCheck(distance, type, mutant);
        else
            addPrimitiveDistanceCheck(distance, type, mutant);

    } else if (original instanceof IincInsnNode) {
        distance.add(Mutation.getDefaultInfectionDistance());
    }
    return distance;
}

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

License:Open Source License

private Map<String, InsnList> getLocalReplacements(MethodNode mn, String desc, AbstractInsnNode node,
        Frame frame) {/* www  .  j ava 2  s  . c om*/
    Map<String, InsnList> replacements = new HashMap<String, InsnList>();

    //if (desc.equals("I"))
    //   return replacements;

    int otherNum = -1;
    if (node instanceof VarInsnNode) {
        VarInsnNode vNode = (VarInsnNode) node;
        otherNum = vNode.var;
    }
    if (otherNum == -1)
        return replacements;

    int currentId = mn.instructions.indexOf(node);
    logger.info("Looking for replacements at position " + currentId + " of variable " + otherNum + " of type "
            + desc);

    //   return replacements;

    for (Object v : mn.localVariables) {
        LocalVariableNode localVar = (LocalVariableNode) v;
        int startId = mn.instructions.indexOf(localVar.start);
        int endId = mn.instructions.indexOf(localVar.end);
        logger.info("Checking local variable " + localVar.name + " of type " + localVar.desc + " at index "
                + localVar.index);
        if (!localVar.desc.equals(desc))
            logger.info("- Types do not match");
        if (localVar.index == otherNum)
            logger.info("- Replacement = original");
        if (currentId < startId)
            logger.info("- Out of scope (start)");
        if (currentId > endId)
            logger.info("- Out of scope (end)");
        BasicValue newValue = (BasicValue) frame.getLocal(localVar.index);
        if (newValue == BasicValue.UNINITIALIZED_VALUE)
            logger.info("- Not initialized");

        if (localVar.desc.equals(desc) && localVar.index != otherNum && currentId >= startId
                && currentId <= endId && newValue != BasicValue.UNINITIALIZED_VALUE) {

            logger.info("Adding local variable " + localVar.name + " of type " + localVar.desc + " at index "
                    + localVar.index + ",  " + startId + "-" + endId + ", " + currentId);
            InsnList list = new InsnList();
            if (node.getOpcode() == Opcodes.GETFIELD) {
                list.add(new InsnNode(Opcodes.POP)); // Remove field owner from stack
            }

            list.add(new VarInsnNode(getLoadOpcode(localVar), localVar.index));
            replacements.put(localVar.name, list);
        }
    }
    return replacements;
}

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

License:Open Source License

private Map<String, InsnList> getFieldReplacements(MethodNode mn, String className, String desc,
        AbstractInsnNode node) {
    Map<String, InsnList> alternatives = new HashMap<String, InsnList>();

    boolean isStatic = (mn.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;

    String otherName = "";
    if (node instanceof FieldInsnNode) {
        FieldInsnNode fNode = (FieldInsnNode) node;
        otherName = fNode.name;//from   ww w  .java 2 s  .  c  o m
    }
    try {
        logger.info("Checking class " + className);
        Class<?> clazz = Class.forName(className, false, ReplaceVariable.class.getClassLoader());

        for (Field field : TestClusterUtils.getFields(clazz)) {
            // We have to use a special version of canUse to avoid
            // that we access the CUT before it is fully initialised
            if (!canUse(field))
                continue;

            Type type = Type.getType(field.getType());
            logger.info("Checking replacement field variable " + field.getName());

            if (field.getName().equals(otherName))
                continue;

            if (isStatic && !(Modifier.isStatic(field.getModifiers())))
                continue;

            if (type.getDescriptor().equals(desc)) {
                logger.info("Adding replacement field variable " + field.getName());
                InsnList list = new InsnList();
                if (node.getOpcode() == Opcodes.GETFIELD) {
                    list.add(new InsnNode(Opcodes.POP)); // Remove field owner from stack
                }

                // new fieldinsnnode
                if (Modifier.isStatic(field.getModifiers()))
                    list.add(new FieldInsnNode(Opcodes.GETSTATIC, className.replace('.', '/'), field.getName(),
                            type.getDescriptor()));
                else {
                    list.add(new VarInsnNode(Opcodes.ALOAD, 0)); // this
                    list.add(new FieldInsnNode(Opcodes.GETFIELD, className.replace('.', '/'), field.getName(),
                            type.getDescriptor()));
                }
                alternatives.put(field.getName(), list);
            } else {
                logger.info("Descriptor does not match: " + field.getName() + " - " + type.getDescriptor());
            }
        }
    } catch (Throwable t) {
        logger.info("Class not found: " + className);
        // TODO Auto-generated catch block
        //e.printStackTrace();
    }
    return alternatives;
}

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

License:Open Source License

/**
 * <p>matches</p>//from   w ww .  j  a  v  a  2  s  . c  o m
 *
 * @param instructions a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a boolean.
 */
public boolean matches(InsnList instructions) {
    int match = 0;

    AbstractInsnNode node = instructions.getFirst();
    while (node != instructions.getLast()) {
        if (node.getType() == AbstractInsnNode.FRAME || node.getType() == AbstractInsnNode.LABEL
                || node.getType() == AbstractInsnNode.LINE) {
            node = node.getNext();
            continue;
        } else {
            boolean found = false;
            for (int opcode : pattern[match]) {
                if (node.getOpcode() == opcode) {
                    match++;
                    found = true;
                    break;
                }
            }
            if (!found)
                match = 0;
        }
        if (match == pattern.length)
            return true;

        node = node.getNext();
    }

    return false;
}

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

License:Open Source License

/**
 * <p>getNextMatch</p>/* www .ja  v a 2s .c o  m*/
 *
 * @param start a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param instructions a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 */
public AbstractInsnNode getNextMatch(AbstractInsnNode start, InsnList instructions) {
    int match = 0;

    AbstractInsnNode node = start;
    AbstractInsnNode startNode = start;
    while (node != instructions.getLast()) {
        if (node.getType() == AbstractInsnNode.FRAME || node.getType() == AbstractInsnNode.LABEL
                || node.getType() == AbstractInsnNode.LINE) {
            node = node.getNext();
            continue;
        } else {
            boolean found = false;
            for (int opcode : pattern[match]) {
                if (node.getOpcode() == opcode) {
                    if (match == 0)
                        startNode = node;
                    match++;
                    found = true;
                    break;
                }
            }
            if (!found)
                match = 0;
        }
        if (match == pattern.length) {
            return startNode;
        }

        node = node.getNext();
    }

    return null;

}

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

License:Open Source License

/** {@inheritDoc} */
@Override//w w w  . j  a va2 s. com
public BasicValue naryOperation(AbstractInsnNode insn, @SuppressWarnings("rawtypes") List values)
        throws AnalyzerException {
    if (insn.getOpcode() == Opcodes.INVOKESTATIC) {
        MethodInsnNode mn = (MethodInsnNode) insn;
        if (mn.owner.equals(Type.getInternalName(BooleanHelper.class)) && mn.name.startsWith("String")) {
            return STRING_BOOLEAN;
        }
    }
    return super.naryOperation(insn, values);
}