Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

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

License:Open Source License

/**
 * <p>/*from  w  w  w .  j  ava  2s. co  m*/
 * 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.ReturnValueAdapter.java

License:Open Source License

private void callLogPrototype(String traceMethod, PDType type) {
    if (type != PDType.LONG && type != PDType.DOUBLE) {
        this.visitInsn(Opcodes.DUP);
        if (type == PDType.FLOAT) {
            this.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "floatToRawIntBits", "(F)I", false);
        }// w ww  .j  a v  a2s.c o  m
    } else {
        this.visitInsn(Opcodes.DUP2);
        if (type == PDType.DOUBLE) {
            this.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "doubleToRawLongBits", "(D)J",
                    false);
        }
        this.visitInsn(Opcodes.DUP2);
        this.visitIntInsn(Opcodes.BIPUSH, 32);
        this.visitInsn(Opcodes.LSHR);
        this.visitInsn(Opcodes.LXOR);
        this.visitInsn(Opcodes.L2I);
    }

    this.visitLdcInsn(className);
    this.visitLdcInsn(fullMethodName);
    this.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
            "returnValue", "(ILjava/lang/String;Ljava/lang/String;)V", false);
}

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

License:Open Source License

private void callLogAReturn() {
    this.visitInsn(Opcodes.DUP);
    this.visitLdcInsn(className);
    this.visitLdcInsn(fullMethodName);
    this.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
            "returnValue", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V", false);
}

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

License:Open Source License

/**
 * Insert a call to the isNull helper function
 * /*w  w w . ja v  a2s  .  c  o m*/
 * @param opcode
 * @param position
 * @param list
 */
public void insertPushNull(int opcode, JumpInsnNode position, InsnList list) {
    int branchId = getBranchID(currentMethodNode, position);
    logger.info("Inserting instrumentation for NULL check at branch " + branchId + " in method "
            + currentMethodNode.name);

    MethodInsnNode nullCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
            Type.getInternalName(BooleanHelper.class), "isNull",
            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(Object.class), Type.INT_TYPE }),
            false);
    list.insertBefore(position, new InsnNode(Opcodes.DUP));
    list.insertBefore(position, new LdcInsnNode(opcode));
    list.insertBefore(position, nullCheck);
    //list.insertBefore(position,
    //                  new LdcInsnNode(getBranchID(currentMethodNode, position)));
    insertBranchIdPlaceholder(currentMethodNode, position, branchId);
    MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "pushPredicate",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false);
    list.insertBefore(position, push);

}

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

License:Open Source License

/**
 * Insert a call to the distance function for unary comparison
 * //from w  w  w  . j a va  2  s .co  m
 * @param opcode
 * @param position
 * @param list
 */
public void insertPush(int opcode, JumpInsnNode position, InsnList list) {
    list.insertBefore(position, new InsnNode(Opcodes.DUP));
    // TODO: We have to put a placeholder here instead of the actual branch ID
    // TODO: And then later add another transformation where we replace this with
    //       actual branch IDs
    //list.insertBefore(position,
    //                  new LdcInsnNode(getBranchID(currentMethodNode, position)));
    insertBranchIdPlaceholder(currentMethodNode, position);
    MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "pushPredicate",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false);
    list.insertBefore(position, push);

}

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

License:Open Source License

/** {@inheritDoc} */
@Override// w  w  w  . j  a  va 2s.  co  m
public void visitInsn(int opcode) {
    if (opcode == Opcodes.ATHROW) {
        super.visitInsn(Opcodes.DUP);
        this.visitLdcInsn(className);
        this.visitLdcInsn(methodName);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
                "exceptionThrown", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V", false);
    }
    super.visitInsn(opcode);
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/*from   w  ww .j a  va  2 s .c om*/
    FieldInsnNode fieldIns = null;

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
                final InsnList il = new InsnList();

                if (opcode == Opcodes.GETFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        il.add(new InsnNode(Opcodes.SWAP));
                    } else if (fieldType.getSize() == 2) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        // v
                        // GETFIELD
                        // v, w
                        il.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        il.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        // -> Call
                        // w
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class),
                        "notifyReadAccess",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/*  w  ww. j a  v  a  2 s.  com*/
    FieldInsnNode fieldIns = null;

    // needed get right receiver var in case of PUTFIELD

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
                // construction of  
                //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
                // call
                final InsnList il = new InsnList();

                if (opcode == Opcodes.PUTFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2));
                        il.add(new InsnNode(Opcodes.POP));
                    } else if (fieldType.getSize() == 2) {
                        InsnList uglyList = new InsnList();
                        // v, w
                        uglyList.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        uglyList.add(new InsnNode(Opcodes.DUP));
                        // w, v, v
                        uglyList.add(new InsnNode(Opcodes.DUP2_X2));
                        // v, v, w, v, v
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // v, v, w
                        instructions.insertBefore(fieldIns, uglyList);
                        // PUTFIELD
                        // v
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions
                this.captureId++;
                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private InsnList addCaptureCall(final boolean isStatic, final String internalClassName, final String methodName,
        final String methodDesc, final Type[] argTypes) {
    // construction of  
    //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
    // call//www  .  j  av  a  2s  .c o  m
    final InsnList il = new InsnList();

    il.add(new LdcInsnNode(this.captureId));

    // --- load receiver argument
    int varIndex;
    if (isStatic) {
        // static method invocation
        il.add(new LdcInsnNode(internalClassName));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class),
                "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));

        varIndex = 0;
    } else {
        // non-static method call
        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }

    // --- load method name argument

    il.add(new LdcInsnNode(methodName));

    // --- load method description argument

    il.add(new LdcInsnNode(methodDesc));

    // --- load methodParams arguments

    // load methodParams length
    // TODO ICONST_1 to ICONST_5 would be more efficient
    il.add(new IntInsnNode(Opcodes.BIPUSH, argTypes.length));

    // create array object
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));

    // fill the array

    for (int i = 0; i < argTypes.length; i++) {
        il.add(new InsnNode(Opcodes.DUP));

        // TODO ICONST_1 to ICONST_5 would be more efficient
        il.add(new IntInsnNode(Opcodes.BIPUSH, i));

        //check for primitives
        this.loadAndConvertToObject(il, argTypes[i], varIndex++);
        il.add(new InsnNode(Opcodes.AASTORE));

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            varIndex++;
        }
    }

    // --- construct Capture.capture() call

    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "capture",
            "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"));

    return il;
}

From source file:org.formulacompiler.compiler.internal.bytecode.ArrayAccessorForConstDataCompiler.java

License:Open Source License

@Override
protected void compileBody() throws CompilerException {
    final GeneratorAdapter mv = mv();
    final ArrayDescriptor dim = this.arrayNode.arrayDescriptor();
    final int n = dim.numberOfElements();
    final DataType eltDataType = this.arrayNode.getDataType();
    final TypeCompiler eltCompiler = section().engineCompiler().typeCompiler(eltDataType);
    final Type eltType = eltCompiler.type();

    // private double[] xy;
    final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, methodName(), arrayDescriptor(), null, null);
    fv.visitEnd();/*from  w ww .j  a  va  2  s.c o  m*/

    // if (this.xy == null) {
    final Label skipInit = mv.newLabel();
    mv.loadThis();
    mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor());
    mv.ifNonNull(skipInit);

    // ... new double[ n ]
    mv.loadThis();
    mv.push(n);
    mv.newArray(eltType);

    // ... { c1, c2, ... }
    int i = 0;
    for (ExpressionNode elt : this.arrayNode.arguments()) {
        if (elt instanceof ExpressionNodeForConstantValue) {
            mv.visitInsn(Opcodes.DUP);
            mv.push(i);
            final ExpressionNodeForConstantValue constElt = (ExpressionNodeForConstantValue) elt;
            eltCompiler.compileConst(mv, constElt.value());
            mv.arrayStore(eltType);
        }
        i++;
    }

    // this.xy *=* new double[] { ... }
    mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), methodName(), arrayDescriptor());

    // }
    // return this.xy;
    mv.mark(skipInit);
    mv.loadThis();
    mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor());
    mv.visitInsn(Opcodes.ARETURN);
}