Example usage for org.objectweb.asm Opcodes GETFIELD

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

Introduction

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

Prototype

int GETFIELD

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

Click Source Link

Usage

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

License:Open Source License

/** {@inheritDoc} */
@Override/*w w w . j a v  a2 s.co  m*/
public BasicValue unaryOperation(AbstractInsnNode insn, BasicValue value) throws AnalyzerException {
    if (insn.getOpcode() == Opcodes.INSTANCEOF) {
        return BOOLEAN_VALUE;
    } else if (insn.getOpcode() == Opcodes.GETFIELD) {
        FieldInsnNode fieldNode = (FieldInsnNode) insn;
        if (BooleanTestabilityTransformation.isTransformedField(fieldNode.owner, fieldNode.name,
                fieldNode.desc))
            return BOOLEAN_VALUE;
    }
    return super.unaryOperation(insn, value);
}

From source file:org.evosuite.instrumentation.error.NullPointerExceptionInstrumentation.java

License:Open Source License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    // If non-static, add a null check

    if (opcode == Opcodes.GETFIELD) {
        mv.visitInsn(Opcodes.DUP);//from  w  w w  .ja  v  a  2  s  .c o m
        insertBranch(Opcodes.IFNONNULL, "java/lang/NullPointerException");

    } else if (opcode == Opcodes.PUTFIELD && !methodName.equals("<init>")) {
        if (Type.getType(desc).getSize() == 2) {
            // 2 words
            // v1 v2 v3
            mv.visitInsn(Opcodes.DUP2_X1);
            // v2 v3 v1 v2 v3

            mv.visitInsn(Opcodes.POP2);
            // v2 v3 v1

            mv.visitInsn(Opcodes.DUP_X2);
            // v1 v2 v3 v1

        } else {
            // 1 word
            mv.visitInsn(Opcodes.DUP2);
            //mv.visitInsn(Opcodes.SWAP);
            mv.visitInsn(Opcodes.POP);
        }
        insertBranch(Opcodes.IFNONNULL, "java/lang/NullPointerException");
    }
}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*w  w  w.  j  a v  a 2  s.  com*/
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {
    List<Mutation> mutations = new LinkedList<Mutation>();

    FieldInsnNode node = (FieldInsnNode) instruction.getASMNode();
    Type fieldType = Type.getType(node.desc);

    // insert mutation into bytecode with conditional
    InsnList mutation = new InsnList();
    logger.debug("Mutation deletefield for statement " + node.name + node.desc);
    if (node.getOpcode() == Opcodes.GETFIELD) {
        logger.debug("Deleting source of type " + node.owner);
        mutation.add(new InsnNode(Opcodes.POP));
    }
    mutation.add(getDefault(fieldType));
    // insert mutation into pool
    Mutation mutationObject = MutationPool.addMutation(className, methodName,
            NAME + " " + node.name + node.desc, instruction, mutation, getInfectionDistance(node, mutation));

    mutations.add(mutationObject);
    return mutations;
}

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

License:Open Source License

/**
 * <p>/*from w  w w .  j a  v  a  2s.  com*/
 * getInfectionDistance
 * </p>
 * 
 * @param original
 *            a {@link org.objectweb.asm.tree.FieldInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(FieldInsnNode original, InsnList mutant) {
    InsnList distance = new InsnList();

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

    distance.add(new FieldInsnNode(original.getOpcode(), original.owner, original.name, original.desc));
    Type type = Type.getType(original.desc);

    if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("[")) {
        ReplaceVariable.addReferenceDistanceCheck(distance, type, mutant);
    } else {
        ReplaceVariable.addPrimitiveDistanceCheck(distance, type, mutant);
    }

    return distance;
}

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

License:Open Source License

/** {@inheritDoc} */
@Override/* www .j a  va2  s. co  m*/
public boolean isApplicable(BytecodeInstruction instruction) {
    return instruction.getASMNode().getOpcode() == Opcodes.GETFIELD
            || instruction.getASMNode().getOpcode() == Opcodes.GETSTATIC;

}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*ww w. j ava 2  s  .c  o m*/
public boolean isApplicable(BytecodeInstruction instruction) {
    AbstractInsnNode node = instruction.getASMNode();
    switch (node.getOpcode()) {
    case Opcodes.ILOAD:
    case Opcodes.LLOAD:
    case Opcodes.FLOAD:
    case Opcodes.DLOAD:
        return true;
    case Opcodes.GETFIELD:
    case Opcodes.GETSTATIC:
        FieldInsnNode fieldNode = (FieldInsnNode) instruction.getASMNode();
        Type type = Type.getType(fieldNode.desc);
        if (type == Type.BYTE_TYPE || type == Type.SHORT_TYPE || type == Type.LONG_TYPE
                || type == Type.FLOAT_TYPE || type == Type.DOUBLE_TYPE || type == Type.BOOLEAN_TYPE
                || type == Type.INT_TYPE) {
            return true;
        }
    default:
        return false;
    }
}

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

License:Open Source License

/**
 * <p>//from w w w  .  j a v a 2 s. 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.  jav  a 2s.c  o  m
    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) {//  ww w  .j a  v  a2s .  c  o  m
    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;
    }
    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.mutation.ReplaceVariable.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from  w w  w.ja  va  2s.  c o  m*/
public boolean isApplicable(BytecodeInstruction instruction) {
    return instruction.isLocalVariableUse() || instruction.getASMNode().getOpcode() == Opcodes.GETSTATIC
            || instruction.getASMNode().getOpcode() == Opcodes.GETFIELD;
}