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.mutation.ReplaceComparisonOperator.java

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int./*from w  ww.  ja  v  a  2  s.  c om*/
 * @param opcodeNew a int.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(int opcodeOrig, int opcodeNew) {
    InsnList distance = new InsnList();
    switch (opcodeOrig) {
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceComparisonOperator.class), "getInfectionDistance",
                "(IIII)D", false));
        break;

    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
        distance.add(new InsnNode(Opcodes.DUP));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceComparisonOperator.class), "getInfectionDistance", "(III)D",
                false));
        break;

    default:
        distance.add(new LdcInsnNode(0.0));
    }

    return distance;
}

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

License:Open Source License

/**
 * <p>//from w  ww .j a  v  a2 s  . co m
 * addPrimitiveDistanceCheck
 * </p>
 *
 * @param distance
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @param type
 *            a {@link org.objectweb.asm.Type} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 */
public static void addPrimitiveDistanceCheck(InsnList distance, Type type, InsnList mutant) {
    distance.add(cast(type, Type.DOUBLE_TYPE));
    distance.add(copy(mutant));
    distance.add(cast(type, Type.DOUBLE_TYPE));
    distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ReplaceVariable.class),
            "getDistance", "(DD)D", false));
}

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

License:Open Source License

/**
 * <p>/* w w w  .jav  a  2  s. co  m*/
 * addReferenceDistanceCheck
 * </p>
 *
 * @param distance
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @param type
 *            a {@link org.objectweb.asm.Type} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 */
public static void addReferenceDistanceCheck(InsnList distance, Type type, InsnList mutant) {
    distance.add(copy(mutant));
    distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ReplaceVariable.class),
            "getDistance", "(Ljava/lang/Object;Ljava/lang/Object;)D", false));
}

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

License:Open Source License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {

    String targetClassName = owner.replace('/', '.');
    if (targetClassName.equals(org.evosuite.runtime.Random.class.getCanonicalName())
            || !BytecodeInstrumentation.checkIfEvoSuitePackage(targetClassName)) {
        //Only ignore EvoSuite callbacks
        if (opcode == Opcodes.INVOKESTATIC) {
            this.purityAnalyzer.addStaticCall(classNameWithDots, methodName, descriptor, targetClassName, name,
                    desc);//ww  w.  ja va2  s  .  co m
        } else if (opcode == Opcodes.INVOKEVIRTUAL) {
            this.purityAnalyzer.addVirtualCall(classNameWithDots, methodName, descriptor, targetClassName, name,
                    desc);

        } else if (opcode == Opcodes.INVOKEINTERFACE) {
            this.purityAnalyzer.addInterfaceCall(classNameWithDots, methodName, descriptor, targetClassName,
                    name, desc);

        } else if (opcode == Opcodes.INVOKESPECIAL) {
            this.purityAnalyzer.addSpecialCall(classNameWithDots, methodName, descriptor, targetClassName, name,
                    desc);
        }
    }
    super.visitMethodInsn(opcode, owner, name, desc, itf);
}

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);
        }//from w  ww. j  a v a  2  s  .c  om
    } 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.StringBooleanInterpreter.java

License:Open Source License

/** {@inheritDoc} */
@Override//ww w  .j  a  v a  2s.co  m
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);
}

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

License:Open Source License

/**
 * Replace boolean-returning method calls on String classes
 * //from www .j a va2 s  .c  o m
 * @param mn
 */
@SuppressWarnings("unchecked")
private boolean transformStrings(MethodNode mn) {
    logger.info("Current method: {}", mn.name);
    boolean changed = false;
    ListIterator<AbstractInsnNode> iterator = mn.instructions.iterator();
    while (iterator.hasNext()) {
        AbstractInsnNode node = iterator.next();
        if (node instanceof MethodInsnNode) {
            MethodInsnNode min = (MethodInsnNode) node;
            if (min.owner.equals("java/lang/String")) {
                if (min.name.equals("equals")) {
                    changed = true;

                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringEquals",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(Object.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    /*
                            MethodInsnNode equalCheck = new MethodInsnNode(
                                    Opcodes.INVOKESTATIC,
                                    Type.getInternalName(BooleanHelper.class),
                                    "StringEqualsCharacterDistance",
                                    Type.getMethodDescriptor(Type.DOUBLE_TYPE,
                                                             new Type[] {
                                                                     Type.getType(String.class),
                                                                     Type.getType(Object.class) }));
                            mn.instructions.insertBefore(node, equalCheck);
                            mn.instructions.insertBefore(node, new LdcInsnNode(0.0));
                            mn.instructions.insertBefore(node, new InsnNode(Opcodes.DCMPG));
                            mn.instructions.remove(node);
                            */
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("equalsIgnoreCase")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringEqualsIgnoreCase",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("startsWith")) {
                    changed = true;
                    if (min.desc.equals("(Ljava/lang/String;)Z")) {
                        mn.instructions.insertBefore(node, new InsnNode(Opcodes.ICONST_0));
                    }
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringStartsWith",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class),
                                    Type.getType(String.class), Type.INT_TYPE }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("endsWith")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringEndsWith",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();

                } else if (min.name.equals("isEmpty")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringIsEmpty",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();
                } else if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringMatches",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(String.class), Type.getType(String.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                    TransformationStatistics.transformedStringComparison();
                } else if (min.name.equals("regionMatches")) {
                    Type[] argumentTypes = Type.getArgumentTypes(min.desc);
                    if (argumentTypes.length == 4) {
                        changed = true;
                        MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "StringRegionMatches",
                                Type.getMethodDescriptor(Type.INT_TYPE,
                                        new Type[] { Type.getType(String.class), Type.INT_TYPE,
                                                Type.getType(String.class), Type.INT_TYPE, Type.INT_TYPE }));
                        mn.instructions.insertBefore(node, equalCheck);
                        mn.instructions.remove(node);
                        TransformationStatistics.transformedStringComparison();

                    } else if (argumentTypes.length == 5) {
                        changed = true;
                        MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "StringRegionMatches",
                                Type.getMethodDescriptor(Type.INT_TYPE,
                                        new Type[] { Type.getType(String.class), Type.BOOLEAN_TYPE,
                                                Type.INT_TYPE, Type.getType(String.class), Type.INT_TYPE,
                                                Type.INT_TYPE }));
                        mn.instructions.insertBefore(node, equalCheck);
                        mn.instructions.remove(node);
                        TransformationStatistics.transformedStringComparison();
                    }
                }

            } else if (min.owner.equals("java/util/regex/Pattern")) {
                if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringMatchRegex",
                            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(String.class),
                                    Type.getType(CharSequence.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                }
            } else if (min.owner.equals("java/util/regex/Matcher")) {
                if (min.name.equals("matches")) {
                    changed = true;
                    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
                            Type.getInternalName(BooleanHelper.class), "StringMatchRegex",
                            Type.getMethodDescriptor(Type.INT_TYPE,
                                    new Type[] { Type.getType(Matcher.class) }));
                    mn.instructions.insertBefore(node, equalCheck);
                    mn.instructions.remove(node);
                }
            }
        }
    }
    return changed;
}

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

License:Open Source License

private static boolean isStringMethod(AbstractInsnNode node) {
    if (node.getOpcode() == Opcodes.INVOKESTATIC) {
        MethodInsnNode methodInsnNode = (MethodInsnNode) node;
        return methodInsnNode.owner.equals(Type.getInternalName(BooleanHelper.class))
                && methodInsnNode.name.startsWith("String");
    }/*from ww  w .  j a  v  a 2 s.  c  o m*/
    return false;
}

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

License:Open Source License

/**
 * <p>//from  w  w  w .  java 2 s  .com
 * transformMethod
 * </p>
 * 
 * @param mn
 *            a {@link org.objectweb.asm.tree.MethodNode} object.
 * @return a boolean.
 */
public boolean transformMethod(MethodNode mn) {
    boolean changed = transformStrings(mn);
    if (changed) {
        try {
            mn.maxStack++;
            Analyzer a = new Analyzer(new StringBooleanInterpreter());
            a.analyze(cn.name, mn);
            Frame[] frames = a.getFrames();
            AbstractInsnNode node = mn.instructions.getFirst();
            boolean done = false;
            while (!done) {
                if (node == mn.instructions.getLast())
                    done = true;
                AbstractInsnNode next = node.getNext();
                int index = mn.instructions.indexOf(node);
                if (index >= frames.length)
                    break;
                Frame current = frames[index];
                if (current == null)
                    break;
                int size = current.getStackSize();
                if (node.getOpcode() == Opcodes.IFNE) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious())) {
                        logger.info("IFNE -> IFGT");
                        branch.setOpcode(Opcodes.IFGT);
                        // branch.setOpcode(Opcodes.IFGE);
                    }
                } else if (node.getOpcode() == Opcodes.IFEQ) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious())) {
                        logger.info("IFEQ -> IFLE");
                        branch.setOpcode(Opcodes.IFLE);
                        // branch.setOpcode(Opcodes.IFNE);
                    }
                } else if (node.getOpcode() == Opcodes.IF_ICMPEQ) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious().getPrevious())) {
                        if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) {
                            branch.setOpcode(Opcodes.IFLE);
                            mn.instructions.remove(node.getPrevious());
                        } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) {
                            branch.setOpcode(Opcodes.IFGT);
                            mn.instructions.remove(node.getPrevious());
                        }
                    }
                } else if (node.getOpcode() == Opcodes.IF_ICMPNE) {
                    JumpInsnNode branch = (JumpInsnNode) node;
                    if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious().getPrevious())) {
                        if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) {
                            branch.setOpcode(Opcodes.IFGT);
                            mn.instructions.remove(node.getPrevious());
                        } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) {
                            branch.setOpcode(Opcodes.IFLE);
                            mn.instructions.remove(node.getPrevious());
                        }
                    }
                } else if (node.getOpcode() == Opcodes.IRETURN) {
                    if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN
                            || isStringMethod(node.getPrevious())) {
                        logger.info("IFEQ -> IFLE");
                        MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC,
                                Type.getInternalName(BooleanHelper.class), "intToBoolean",
                                Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }));

                        mn.instructions.insertBefore(node, n);
                    }
                }
                node = next;
            }
        } catch (Exception e) {
            logger.warn("EXCEPTION DURING STRING TRANSFORMATION: {}", e);
            e.printStackTrace();
            return changed;
        }
    }
    return changed;
}