Example usage for org.objectweb.asm Opcodes INVOKESTATIC

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

Introduction

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

Prototype

int INVOKESTATIC

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

Click Source Link

Usage

From source file:org.evosuite.instrumentation.coverage.MutationInstrumentation.java

License:Open Source License

/**
 * <p>/*from  w  ww . j  a  va 2 s  . co  m*/
 * addInstrumentation
 * </p>
 * 
 * @param mn
 *            a {@link org.objectweb.asm.tree.MethodNode} object.
 * @param original
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param mutations
 *            a {@link java.util.List} object.
 */
protected void addInstrumentation(MethodNode mn, AbstractInsnNode original, List<Mutation> mutations) {

    InsnList instructions = new InsnList();

    // call mutationTouched(mutationObject.getId());
    // TODO: All mutations in the id are touched, not just one!
    for (Mutation mutation : mutations) {
        instructions.add(mutation.getInfectionDistance());
        instructions.add(new LdcInsnNode(mutation.getId()));
        MethodInsnNode touched = new MethodInsnNode(Opcodes.INVOKESTATIC,
                Type.getInternalName(ExecutionTracer.class), "passedMutation",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.DOUBLE_TYPE, Type.INT_TYPE }),
                false);
        instructions.add(touched);
    }

    LabelNode endLabel = new LabelNode();
    for (Mutation mutation : mutations) {
        LabelNode nextLabel = new LabelNode();

        LdcInsnNode mutationId = new LdcInsnNode(mutation.getId());
        instructions.add(mutationId);
        FieldInsnNode activeId = new FieldInsnNode(Opcodes.GETSTATIC,
                Type.getInternalName(MutationObserver.class), "activeMutation", "I");
        instructions.add(activeId);
        instructions.add(new JumpInsnNode(Opcodes.IF_ICMPNE, nextLabel));
        instructions.add(mutation.getMutation());
        instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabel));
        instructions.add(nextLabel);
    }

    mn.instructions.insertBefore(original, instructions);
    mn.instructions.insert(original, endLabel);
}

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

License:Open Source License

@Override
public void visitInsn(int opcode) {
    // Overflow checks
    switch (opcode) {
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
        mv.visitInsn(Opcodes.DUP2);//ww w  .j  av a2s.  com
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "underflowDistance", "(III)I", false);

        insertBranchWithoutException(Opcodes.IFGT);

    case Opcodes.IDIV:
        mv.visitInsn(Opcodes.DUP2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "overflowDistance", "(III)I", false);
        insertBranchWithoutException(Opcodes.IFGT);

        break;

    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:

        mv.visitInsn(Opcodes.DUP2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "underflowDistance", "(FFI)I", false);
        insertBranchWithoutException(Opcodes.IFGE);

    case Opcodes.FDIV:
        mv.visitInsn(Opcodes.DUP2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "overflowDistance", "(FFI)I", false);
        insertBranchWithoutException(Opcodes.IFGE);
        break;

    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:

        int loc = mv.newLocal(Type.DOUBLE_TYPE);
        mv.storeLocal(loc);
        mv.visitInsn(Opcodes.DUP2);
        mv.loadLocal(loc);
        mv.visitInsn(Opcodes.DUP2_X2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "underflowDistance", "(DDI)I", false);

        insertBranchWithoutException(Opcodes.IFGE);

    case Opcodes.DDIV:
        loc = mv.newLocal(Type.DOUBLE_TYPE);

        mv.storeLocal(loc);
        mv.visitInsn(Opcodes.DUP2);
        mv.loadLocal(loc);
        mv.visitInsn(Opcodes.DUP2_X2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "overflowDistance", "(DDI)I", false);

        insertBranchWithoutException(Opcodes.IFGE);
        break;

    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
        int loc2 = mv.newLocal(Type.LONG_TYPE);
        mv.storeLocal(loc2);
        mv.visitInsn(Opcodes.DUP2);
        mv.loadLocal(loc2);
        mv.visitInsn(Opcodes.DUP2_X2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "underflowDistance", "(JJI)I", false);

        insertBranchWithoutException(Opcodes.IFGE);

    case Opcodes.LDIV:

        loc2 = mv.newLocal(Type.LONG_TYPE);
        mv.storeLocal(loc2);
        mv.visitInsn(Opcodes.DUP2);
        mv.loadLocal(loc2);
        mv.visitInsn(Opcodes.DUP2_X2);
        mv.visitLdcInsn(opcode);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CHECKER, "overflowDistance", "(JJI)I", false);

        insertBranchWithoutException(Opcodes.IFGE);
        break;
    }

}

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

License:Open Source License

/** {@inheritDoc} */
@Override/* w w w  .j  a v a  2 s .com*/
public void visitInsn(int opcode) {
    if (opcode == Opcodes.ATHROW && !inErrorBranch) {
        super.visitInsn(Opcodes.DUP);
        this.visitLdcInsn(className);
        this.visitLdcInsn(fullMethodName);
        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.instrumentation.JUnitCoverageMethodAdapter.java

License:Open Source License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
    if (!isJUnitTest) {
        super.visitMethodInsn(opcode, owner, name, desc, itf);
        return;//from  w ww. j  a v a 2s  . c  o m
    }

    String classNameWithDots = owner.replace('/', '.');
    String instrumentedOwner = owner;
    if (!DependencyAnalysis.shouldAnalyze(classNameWithDots)) {
        if (subClasses.contains(classNameWithDots)) {
            logger.info("Using target class instead of subclass");
            // The reason for this hack is that manually written tests often use dummy-subclasses and then we would miss coverage
            instrumentedOwner = Properties.TARGET_CLASS.replace('.', '/');
        } else if (superClasses.contains(classNameWithDots)) {
            // Can be a virtual call, so is ok
            instrumentedOwner = Properties.TARGET_CLASS.replace('.', '/');
        } else {
            super.visitMethodInsn(opcode, owner, name, desc, itf);
            return;
        }

    }
    Label startLabel = mark();
    Type[] argumentTypes = Type.getArgumentTypes(desc);
    int[] locals = new int[argumentTypes.length];

    for (int i = argumentTypes.length - 1; i >= 0; i--) {
        int local = newLocal(argumentTypes[i]);
        storeLocal(local, argumentTypes[i]);
        locals[i] = local;
    }
    if (opcode == Opcodes.INVOKESPECIAL) {
        // dup(); // for return value
        push((String) null);
    } else if (opcode == Opcodes.INVOKESTATIC) {
        push((String) null);
    } else {
        dup(); // Callee
    }
    push(opcode);
    push(instrumentedOwner);
    push(name);
    push(desc);
    push(argumentTypes.length);
    Type objectType = Type.getObjectType("java/lang/Object");
    newArray(objectType);
    for (int i = 0; i < argumentTypes.length; i++) {
        dup();
        push(i);
        loadLocal(locals[i]);
        box(argumentTypes[i]);
        arrayStore(objectType);
    }

    mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(JUnitObserver.class), "methodCalled",
            "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V",
            false);

    for (int i = 0; i < argumentTypes.length; i++) {
        loadLocal(locals[i]);
    }

    super.visitMethodInsn(opcode, owner, name, desc, itf);

    Label l = newLabel();
    goTo(l);
    Label endLabel = mark();

    TryCatchBlock block = new TryCatchBlock(startLabel, endLabel, endLabel,
            Type.getType(Throwable.class).getInternalName());
    instrumentedTryCatchBlocks.add(block);

    //        catchException(startLabel, endLabel, Type.getType(Throwable.class));

    dup(); // Exception
    push(instrumentedOwner);
    push(name);
    push(desc);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(JUnitObserver.class),
            "methodException", "(Ljava/lang/Throwable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
            false);

    throwException(); // re-throw
    mark(l);

    Type returnType = Type.getReturnType(desc);
    if (opcode == Opcodes.INVOKESPECIAL) {
        // dup();
        push((String) null);
    } else if (returnType == Type.VOID_TYPE) {
        push((String) null);
    } else {
        if (returnType.getSize() == 1)
            dup();
        else if (returnType.getSize() == 2)
            dup2();
        else
            assert (false); // Cannot happen
        box(Type.getReturnType(desc));
    }
    push(instrumentedOwner);
    push(name);
    push(desc);
    //        if ((opcode & Opcodes.INVOKESTATIC) > 0) {
    //            mv.visitInsn(Opcodes.ACONST_NULL);
    //        } else {
    //            mv.visitVarInsn(Opcodes.ALOAD, 0);
    //        }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(JUnitObserver.class),
            "methodReturned", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
            false);

}

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

License:Open Source License

private void addLineNumberInstrumentation(int line) {
    LinePool.addLine(className, fullMethodName, line);
    this.visitLdcInsn(className);
    this.visitLdcInsn(fullMethodName);
    this.visitLdcInsn(line);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class), "passedLine",
            "(Ljava/lang/String;Ljava/lang/String;I)V", false);
}

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

License:Open Source License

/** {@inheritDoc} */
@Override//from  w w  w.j a  v  a2  s  . c  om
public void visitMethodInsn(int opcode, String owner, String name, String desc) {

    boolean isReplaced = false;
    // static replacement methods
    for (MethodCallReplacement replacement : replacementCalls) {
        if (replacement.isTarget(owner, name, desc)) {
            isReplaced = true;
            replacement.insertMethodCall(this, Opcodes.INVOKESTATIC);
            break;
        }
    }

    // for constructors
    if (!isReplaced) {
        for (MethodCallReplacement replacement : specialReplacementCalls) {
            if (replacement.isTarget(owner, name, desc) && opcode == Opcodes.INVOKESPECIAL) {
                isReplaced = true;
                boolean isSelf = false;
                if (needToWaitForSuperConstructor) {
                    String originalClassNameWithDots = owner.replace('/', '.');
                    if (originalClassNameWithDots.equals(superClassName)) {
                        isSelf = true;
                    }
                }
                if (replacement.methodName.equals("<init>"))
                    replacement.insertConstructorCall(this, replacement, isSelf);
                else
                    replacement.insertMethodCall(this, Opcodes.INVOKESPECIAL);
                break;
            }
        }
    }

    // non-static replacement methods
    if (!isReplaced) {
        for (MethodCallReplacement replacement : virtualReplacementCalls) {
            if (replacement.isTarget(owner, name, desc)) {
                isReplaced = true;
                replacement.insertMethodCall(this, Opcodes.INVOKEVIRTUAL);
                break;
            }
        }
    }

    if (!isReplaced) {
        super.visitMethodInsn(opcode, owner, name, desc);
    }
    if (needToWaitForSuperConstructor) {
        if (opcode == Opcodes.INVOKESPECIAL) {
            String originalClassNameWithDots = owner.replace('/', '.');
            if (originalClassNameWithDots.equals(superClassName)) {
                needToWaitForSuperConstructor = false;
            }
        }
    }

}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*from w  ww  .  jav  a  2 s  .c  o  m*/
public void onMethodEnter() {

    if (methodName.equals("<clinit>"))
        return; // FIXXME: Should we call super.onMethodEnter() here?

    mv.visitLdcInsn(className);
    mv.visitLdcInsn(fullMethodName);
    if ((access & Opcodes.ACC_STATIC) > 0) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
    }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
            "enteredMethod", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V", false);

    super.onMethodEnter();
}

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

License:Open Source License

/** {@inheritDoc} */
@Override//from   w  ww .  ja v a2 s . com
public void onMethodExit(int opcode) {
    // TODO: Check for <clinit>

    if (opcode != Opcodes.ATHROW) {

        mv.visitLdcInsn(className);
        mv.visitLdcInsn(fullMethodName);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(org.evosuite.testcase.execution.ExecutionTracer.class),
                "leftMethod", "(Ljava/lang/String;Ljava/lang/String;)V", false);
    }
    super.onMethodExit(opcode);
}

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

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int.//from  w w  w.  jav a 2  s.co  m
 * @param opcodeNew a int.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(int opcodeOrig, int opcodeNew) {
    InsnList distance = new InsnList();

    if (opcodesInt.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceInt",
                "(IIII)D", false));
    } else if (opcodesLong.contains(opcodeOrig)) {
        distance.add(new VarInsnNode(Opcodes.LSTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.LLOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceLong",
                "(JJII)D", false));
        numVariable += 2;
    } else if (opcodesFloat.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceFloat",
                "(FFII)D", false));
    } else if (opcodesDouble.contains(opcodeOrig)) {
        distance.add(new VarInsnNode(Opcodes.DSTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.DLOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceDouble",
                "(DDII)D", false));
        numVariable += 2;
    }

    return distance;
}

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

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int./*from w  ww .  j  ava 2 s. c o  m*/
 * @param opcodeNew a int.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(int opcodeOrig, int opcodeNew) {
    InsnList distance = new InsnList();

    if (opcodesInt.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceInt",
                "(IIII)D", false));
    } else if (opcodesIntShift.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceInt",
                "(IIII)D", false));
    } else if (opcodesLong.contains(opcodeOrig)) {

        distance.add(new VarInsnNode(Opcodes.LSTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.LLOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceLong",
                "(JJII)D", false));
        numVariable += 2;

    } else if (opcodesLongShift.contains(opcodeOrig)) {
        distance.add(new VarInsnNode(Opcodes.ISTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.ILOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceLong",
                "(JIII)D", false));
        numVariable += 1;
    }

    return distance;
}