Example usage for org.objectweb.asm Opcodes POP

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

Introduction

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

Prototype

int POP

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

Click Source Link

Usage

From source file:org.elasticsearch.plan.a.External.java

License:Apache License

private void method(final ParserRuleContext source, final String name, final List<ExpressionContext> arguments,
        final boolean last) {
    final Struct struct = current.struct;

    Type[] types;//from w  ww. j a  v  a  2  s  .  c o m
    Segment segment0;
    Segment segment1 = null;

    if (current.dimensions > 0) {
        throw new IllegalArgumentException(error(source) + "Unexpected call [" + name + "] on an array.");
    } else if (last && write != null) {
        throw new IllegalArgumentException(error(source) + "Cannot assign a value to a call [" + name + "].");
    } else if (statik && "makearray".equals(name)) {
        if (!read) {
            throw new IllegalArgumentException(error(source) + "A newly created array must be assigned.");
        }

        types = new Type[arguments.size()];
        Arrays.fill(types, standard.intType);
        segment0 = new MakeSegment(source, current, arguments.size());
        current = getTypeWithArrayDimensions(struct, arguments.size());
    } else {
        final Constructor constructor = statik ? struct.constructors.get(name) : null;
        final Method method = statik ? struct.functions.get(name) : struct.methods.get(name);

        if (constructor != null) {
            types = new Type[constructor.arguments.size()];
            constructor.arguments.toArray(types);

            segments.add(new NewSegment(source, constructor.owner));

            if (read) {
                segments.add(new InstructionSegment(source, Opcodes.DUP));
            } else {
                current = standard.voidType;
                statement = true;
            }

            segment0 = new ConstructorSegment(source, constructor);
        } else if (method != null) {
            types = new Type[method.arguments.size()];
            method.arguments.toArray(types);

            if (!read) {
                final int size = method.rtn.metadata.size;

                if (size == 1) {
                    segment1 = new InstructionSegment(source, Opcodes.POP);
                } else if (size == 2) {
                    segment1 = new InstructionSegment(source, Opcodes.POP2);
                } else if (size != 0) {
                    throw new IllegalStateException(error(source) + "Unexpected type size.");
                }

                current = standard.voidType;
                statement = true;
            } else {
                current = method.rtn;
            }

            segment0 = new MethodSegment(source, method);
        } else {
            throw new IllegalArgumentException(
                    error(source) + "Unknown call [" + name + "] on type [" + struct.name + "].");
        }
    }

    if (arguments.size() != types.length) {
        throw new IllegalArgumentException();
    }

    for (int argument = 0; argument < arguments.size(); ++argument) {
        final ExpressionContext exprctx = adapter.getExpressionContext(arguments.get(argument));
        final ExpressionMetadata expremd = adapter.createExpressionMetadata(exprctx);
        expremd.to = types[argument];
        analyzer.visit(exprctx);

        segments.add(new NodeSegment(exprctx));
    }

    segments.add(segment0);

    if (segment1 != null) {
        segments.add(segment1);
    }
}

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   www.  j  a  va 2s .c  om*/
        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//ww  w .  ja v  a 2 s .  c  om
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.DeleteStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override/*www .  java  2  s.  c om*/
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {

    List<Mutation> mutations = new LinkedList<Mutation>();

    MethodInsnNode node = (MethodInsnNode) instruction.getASMNode();
    Type returnType = Type.getReturnType(node.desc);

    // insert mutation into bytecode with conditional
    InsnList mutation = new InsnList();
    logger.info("Mutation deletestatement for statement " + node.name + node.desc);
    for (Type argType : Type.getArgumentTypes(node.desc)) {
        if (argType.getSize() == 0)
            logger.info("Ignoring parameter of type " + argType);
        else if (argType.getSize() == 2) {
            mutation.insert(new InsnNode(Opcodes.POP2));
            logger.debug("Deleting parameter of 2 type " + argType);
        } else {
            logger.debug("Deleting parameter of 1 type " + argType);
            mutation.insert(new InsnNode(Opcodes.POP));
        }
    }
    if (node.getOpcode() == Opcodes.INVOKEVIRTUAL) {
        logger.debug("Deleting callee of type " + node.owner);
        mutation.add(new InsnNode(Opcodes.POP));
    } else if (node.getOpcode() == Opcodes.INVOKEINTERFACE) {
        boolean isStatic = false;
        try {
            Class<?> clazz = Class.forName(node.owner.replace('/', '.'), false,
                    DeleteStatement.class.getClassLoader());
            for (java.lang.reflect.Method method : clazz.getMethods()) {
                if (method.getName().equals(node.name)) {
                    if (Type.getMethodDescriptor(method).equals(node.desc)) {
                        if (Modifier.isStatic(method.getModifiers()))
                            isStatic = true;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            logger.warn("Could not find class: " + node.owner + ", this is likely a severe problem");
        }
        if (!isStatic) {
            logger.info("Deleting callee of type " + node.owner);
            mutation.add(new InsnNode(Opcodes.POP));
        }
    }
    mutation.add(getDefault(returnType));

    // insert mutation into pool
    Mutation mutationObject = MutationPool.addMutation(className, methodName,
            NAME + " " + node.name + node.desc, instruction, mutation, Mutation.getDefaultInfectionDistance());

    mutations.add(mutationObject);
    return mutations;
}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*from ww  w .j ava2 s.  co m*/
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {
    JumpInsnNode node = (JumpInsnNode) instruction.getASMNode();
    List<Mutation> mutations = new LinkedList<Mutation>();
    LabelNode target = node.label;

    boolean isBoolean = frame.getStack(frame.getStackSize() - 1) == BooleanValueInterpreter.BOOLEAN_VALUE;

    for (Integer op : getOperators(node.getOpcode(), isBoolean)) {
        logger.debug("Adding replacement " + op);
        if (op >= 0) {
            // insert mutation into bytecode with conditional
            JumpInsnNode mutation = new JumpInsnNode(op, target);
            // insert mutation into pool
            Mutation mutationObject = MutationPool.addMutation(className, methodName,
                    NAME + " " + getOp(node.getOpcode()) + " -> " + getOp(op), instruction, mutation,
                    getInfectionDistance(node.getOpcode(), op));
            mutations.add(mutationObject);
        } else {
            // Replace relational operator with TRUE/FALSE

            InsnList mutation = new InsnList();
            if (opcodesInt.contains(node.getOpcode()))
                mutation.add(new InsnNode(Opcodes.POP));
            else
                mutation.add(new InsnNode(Opcodes.POP2));
            if (op == TRUE) {
                mutation.add(new LdcInsnNode(1));
            } else {
                mutation.add(new LdcInsnNode(0));
            }
            mutation.add(new JumpInsnNode(Opcodes.IFNE, target));
            Mutation mutationObject = MutationPool.addMutation(className, methodName,
                    NAME + " " + getOp(node.getOpcode()) + " -> " + op, instruction, mutation,
                    getInfectionDistance(node.getOpcode(), op));
            mutations.add(mutationObject);
        }
    }

    return mutations;
}

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

License:Open Source License

/**
 * <p>//from   w w w .  java  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

private Map<String, InsnList> getLocalReplacements(MethodNode mn, String desc, AbstractInsnNode node,
        Frame frame) {//from w  w w . j  a  va  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) {//from   ww  w  . j  a va2s  .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.RemoveFinalMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override//from w  ww .  j  a  v a  2s .c o m
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if ((opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) && owner.equals(className)) {

        if (!finalFields.contains(name)) {
            //System.out.println("Keeping non-final field " + name + " in class "
            //        + owner);
            super.visitFieldInsn(opcode, owner, name, desc);
        } else {
            //System.out.println("Omitting final field " + name + " in class " + owner);
            Type type = Type.getType(desc);
            if (type.getSize() == 1)
                super.visitInsn(Opcodes.POP);
            else if (type.getSize() == 2)
                super.visitInsn(Opcodes.POP2);
            if (opcode == Opcodes.PUTFIELD)
                super.visitInsn(Opcodes.POP);
        }
    } else {
        //if (!owner.equals(className))
        //   System.out.println("Mismatch: " + className + " / " + owner);
        super.visitFieldInsn(opcode, owner, name, desc);
    }
}

From source file:org.evosuite.runtime.instrumentation.RemoveFinalMethodAdapter.java

License:Open Source License

/**
 * Calls to cobertura methods are removed to avoid that code coverage
 * data is deleted//ww  w.j  a v  a  2 s . c o m
 */
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
    if (opcode == Opcodes.INVOKESTATIC && name.startsWith("__cobertura")) {
        for (Type parameterType : Type.getArgumentTypes(desc)) {
            if (parameterType.getSize() == 1) {
                super.visitInsn(Opcodes.POP);
            } else if (parameterType.getSize() == 2) {
                super.visitInsn(Opcodes.POP2);
            }
        }
    } else {
        super.visitMethodInsn(opcode, owner, name, desc, itf);
    }
}