Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

List of usage examples for org.objectweb.asm MethodVisitor visitMethodInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitMethodInsn.

Prototype

@Deprecated
public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor) 

Source Link

Document

Visits a method instruction.

Usage

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.absoluteValues.AbsoluteValueMethodAdapter.java

License:Open Source License

@Override
protected void handleMutation(Mutation mutation, Integer typeOpcode) {
    MutationCode unMutated = new MutationCode(null) {
        @Override//from   ww w. ja  va  2  s .co  m
        public void insertCodeBlock(MethodVisitor mv) {
        }

    };

    List<MutationCode> mutated = new ArrayList<MutationCode>();
    mutation.setOperatorAddInfo(ABSOLUTE);
    if (mutationManager.shouldApplyMutation(mutation)) {
        Mutation dbMutation = QueryManager.getMutation(mutation);
        final String signature = getSignature(typeOpcode);
        MutationCode mutatedAbsolute = new MutationCode(dbMutation) {
            @Override
            public void insertCodeBlock(MethodVisitor mv) {
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Math", "abs", signature);
            }
        };
        mutated.add(mutatedAbsolute);
    }
    mutation.setOperatorAddInfo(ABSOLUTE_NEGATIVE);
    if (mutationManager.shouldApplyMutation(mutation)) {
        Mutation dbMutation = QueryManager.getMutation(mutation);
        final String signature = getSignature(typeOpcode);
        MutationCode mutatedMinusAbsolut = new MutationCode(dbMutation) {
            @Override
            public void insertCodeBlock(MethodVisitor mv) {
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/CoverageDataRuntime", "absMinus1", signature);
            }
        };
        mutated.add(mutatedMinusAbsolut);
    }
    mutation.setOperatorAddInfo(FAIL_ON_ZERO);
    if (mutationManager.shouldApplyMutation(mutation)) {
        Mutation dbMutation = QueryManager.getMutation(mutation);
        final String signature = getSignature(typeOpcode);
        MutationCode mutatedFailOnZero = new MutationCode(dbMutation) {
            @Override
            public void insertCodeBlock(MethodVisitor mv) {
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/CoverageDataRuntime", "failOnZero", signature);
            }
        };
        mutated.add(mutatedFailOnZero);
    }

    if (mutated.size() > 0) {
        BytecodeTasks.insertIfElse(mv, unMutated, mutated.toArray(new MutationCode[0]));
    } else {
        logger.debug("Not applying mutations for base mutation " + mutation);
    }
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.BytecodeTasks.java

License:Open Source License

/**
 * Inserts a mutation. The inserted code is like this:
 * <code>if(System.getProperty(mutationID)){
 *          execute mutated code//from   w  w w  .j a va  2 s  . co  m
 *       }
 *       else{
 *          execute unmutated code
 *       }
 * 
 * @param mv
 *            MethodVisitor where the code is inserted.
 * @param unMutated
 *            code that should be used when no mutation is applied.
 * @param mutations
 *            code that should be used when one of the mutations is applied.
 */
public static void insertIfElse(MethodVisitor mv, MutationCode unMutated, MutationCode[] mutations) {
    Label endLabel = new Label();
    Label mutationStartLabel = new Label();
    mutationStartLabel.info = new MutationMarker(true);
    mv.visitLabel(mutationStartLabel);
    for (MutationCode mutationCode : mutations) {
        Mutation mutation = mutationCode.getMutation();
        mv.visitLdcInsn(mutation.getMutationVariable());
        // mv.visitLdcInsn(mutation.getMutationType() + "");
        // mv.visitInsn(Opcodes.POP);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "getProperty",
                "(Ljava/lang/String;)Ljava/lang/String;");
        Label l1 = new Label();
        mv.visitJumpInsn(Opcodes.IFNULL, l1);

        Label l2 = new Label();
        mv.visitLabel(l2);
        // insertPrintStatements(mv, "Mutation touched: " +
        // mutation.getId());
        insertMutationTouchedCode(mv, mutation);
        if (!DebugProperties.INSERT_ORIGINAL_INSTEAD_OF_MUTATION) {
            mutationCode.insertCodeBlock(mv);
        } else {
            logger.warn("Debug mode: not inserting mutated statement");
            unMutated.insertCodeBlock(mv);
        }
        mv.visitJumpInsn(Opcodes.GOTO, endLabel);
        mv.visitLabel(l1);
    }
    Label mutationEndLabel = new Label();
    mutationEndLabel.info = new MutationMarker(false);
    mv.visitLabel(mutationEndLabel);
    unMutated.insertCodeBlock(mv);
    mv.visitLabel(endLabel);

}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.BytecodeTasks.java

License:Open Source License

/**
 * Insert calls that signal whether the mutated code was executed.
 * /*from   w  ww . j a v  a  2s.  c  om*/
 * @param mv
 *            the method visitor to add the statements
 * @param mutation
 *            the mutation that is covered or not
 */
private static void insertMutationTouchedCode(MethodVisitor mv, Mutation mutation) {
    if (DebugProperties.MUTATION_PRINT_STATEMENTS_ENABLED) {
        BytecodeTasks.insertPrintStatements(mv, "Mutation " + mutation.getMutationVariable() + " - "
                + mutation.getMutationType() + " is enabled");
    }
    mv.visitLdcInsn(mutation.getId());
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "de/unisb/cs/st/javalanche/mutation/runtime/MutationObserver",
            "touch", "(J)V");
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.BytecodeTasks.java

License:Open Source License

/**
 * Inserts bytecode that prints the given message.
 * /*from w  ww  .ja va 2 s .c o m*/
 * @param mv
 *            The MethodVisitor for which the code is added.
 * @param message
 *            The text to be printed to System.out .
 */
public static void insertPrintStatements(MethodVisitor mv, String message) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
    mv.visitLdcInsn("[MUTATION] " + message);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.removeCalls.RemoveMethodCallsMethodAdapter.java

License:Open Source License

@Override
protected void handleMutation(Mutation mutation, final int opcode, final String owner, final String name,
        final String desc) {
    if (mutationManager.shouldApplyMutation(mutation)) {
        Mutation dbMutation = QueryManager.getMutation(mutation);
        MutationCode unMutated = new MutationCode(null) {
            @Override//from  w ww.ja  va  2 s.c o m
            public void insertCodeBlock(MethodVisitor mv) {
                mv.visitMethodInsn(opcode, owner, name, desc);
            }

        };
        MutationCode mutated = new RemoveCall(dbMutation, name, desc, opcode);
        BytecodeTasks.insertIfElse(mv, unMutated, new MutationCode[] { mutated });
    } else {
        logger.debug("Not applying mutation");
        mv.visitMethodInsn(opcode, owner, name, desc);
    }
}

From source file:de.unisb.cs.st.javalanche.mutation.runtime.CoverageDataUtil.java

License:Open Source License

public static void insertCoverageCalls(MethodVisitor mv, Mutation mutation) {
    Label endLabel = new Label();
    endLabel.info = new MutationMarker(false);
    Label mutationStartLabel = new Label();
    mutationStartLabel.info = new MutationMarker(true);
    mv.visitLabel(mutationStartLabel);/*from  ww w. j av  a  2 s. co  m*/
    Long id = mutation.getId();
    if (id == null) {
        Mutation mutationFromDb = QueryManager.getMutationOrNull(mutation);
        if (mutationFromDb != null) {
            id = mutationFromDb.getId();
        } else {
            QueryManager.saveMutation(mutation);
            id = mutation.getId();
        }
    }
    logger.debug("Inserting Coverage calls for:  " + id + " " + mutation);
    mv.visitLdcInsn(id);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/CoverageDataRuntime", "touch", "(J)V");
    mv.visitLabel(endLabel);
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*from  w ww  . j av a 2 s .c  o  m*/
    this.name = name;
    this.isInterface = (Opcodes.ACC_INTERFACE & access) != 0;
    this.superName = getHologramSuperclassName(isInterface, name, superName);
    interfaces = getHologramInterfaces(name, isInterface, interfaces);

    // Force everything to be public, since HologramClassLoader has to reflectively
    // construct holograms. Again, not a problem because the VM will see the original flags on the ClassMirror instead.
    // Also remove enum flags.
    int hologramAccess = forcePublic(access);
    // Also remove abstract flag. Shouldn't be necessary, but the VM (OpenJDK at least)
    // creates objects that claim to be an instance of VirtualMachineError, which is abstract.
    if (name.equals("hologram/java/lang/VirtualMachineError")) {
        hologramAccess = ~Opcodes.ACC_ABSTRACT & access;
    }

    // We need at least 1.5 to use class literal constants
    // TODO-RS: Work out a better way to interpret 45.X numbers correctly
    if (version == Opcodes.V1_1 || version < Opcodes.V1_5) {
        version = 49;
    }

    super.visit(version, hologramAccess, name, signature, this.superName, interfaces);

    if (this.name.equals(hologramThrowableType.getInternalName())) {
        // Generate aliases for the original superclass' fillInStackTrace and getStackTrace methods,
        // so we can call them in stubs without hitting hologram code.
        MethodVisitor v = super.visitMethod(Opcodes.ACC_PUBLIC, "superFillInStackTrace",
                Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
        v.visitCode();
        v.visitVarInsn(Opcodes.ALOAD, 0);
        v.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Throwable.class), "fillInStackTrace",
                Type.getMethodDescriptor(Type.getType(Throwable.class)));
        v.visitInsn(Opcodes.RETURN);
        v.visitMaxs(1, 1);
        v.visitEnd();

        v = super.visitMethod(Opcodes.ACC_PUBLIC, "superGetStackTrace",
                Type.getMethodDescriptor(Type.getType(StackTraceElement[].class)), null, null);
        v.visitCode();
        v.visitVarInsn(Opcodes.ALOAD, 0);
        v.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Throwable.class), "getStackTrace",
                Type.getMethodDescriptor(Type.getType(StackTraceElement[].class)));
        v.visitInsn(Opcodes.ARETURN);
        v.visitMaxs(1, 1);
        v.visitEnd();
    }
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

@Override
public void visitEnd() {
    // Generate the static field used to store the corresponding ClassMirror
    int staticAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC;
    super.visitField(staticAccess, "classMirror", classMirrorType.getDescriptor(), null, null);

    // Generate the constructor that takes a mirror instance as an Object parameter
    String constructorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
    if (name.equals(getHologramType(Type.getType(Throwable.class), true).getInternalName())) {
        // This doesn't extend ObjectHologram so we have to set the field directly
        super.visitField(Opcodes.ACC_PUBLIC, "mirror", objectMirrorType.getDescriptor(), null, null);

        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);/*from   ww w  . jav  a2s. co m*/
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE));
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, objectMirrorType.getInternalName());
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, name, "mirror", Type.getDescriptor(ObjectMirror.class));
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitEnd();

        methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "getMirror",
                Type.getMethodDescriptor(objectMirrorType), null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, name, "mirror", Type.getDescriptor(ObjectMirror.class));
        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(1, 1);
        methodVisitor.visitEnd();
    } else if (!isInterface) {
        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", constructorDesc);
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitEnd();
    }

    // Add a class initialization method to initialize the static fields,
    // if one doesn't exist already.
    if (!hasClinit) {
        InstructionAdapter mv = new InstructionAdapter(
                super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null));
        mv.visitCode();
        HologramMethodGenerator.initializeStaticFields(Type.getObjectType(this.name), mv);
        mv.areturn(Type.VOID_TYPE);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }

    super.visitEnd();
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

public static void generateArray(ClassVisitor visitor, HologramClassLoader loader,
        HologramClassMirror hologramClassMirror) {
    boolean isInterface = !hologramClassMirror.isImplementationClass();
    ClassMirror classMirror = hologramClassMirror.getOriginal();

    Type originalType = Reflection.typeForClassMirror(classMirror);
    Type originalElementType = originalType.getElementType();
    int dims = originalType.getDimensions();

    String internalName = getHologramType(originalType, !isInterface).getInternalName();

    ClassMirror superClassMirror = null;
    String superName = isInterface ? Type.getInternalName(Object.class)
            : Type.getInternalName(ObjectArrayHologram.class);
    Set<String> interfaces = new HashSet<String>();
    int access = Opcodes.ACC_PUBLIC | (isInterface ? Opcodes.ACC_INTERFACE : 0);

    if (originalElementType.getSort() == Type.OBJECT || originalElementType.getSort() == Type.ARRAY) {
        ClassMirror elementClass = loader.loadOriginalClassMirror(originalElementType.getClassName());
        superClassMirror = elementClass.getSuperClassMirror();

        if (isInterface) {
            if (superClassMirror != null) {
                Type superType = Reflection.makeArrayType(dims,
                        Type.getObjectType(superClassMirror.getClassName().replace('.', '/')));
                String superInterfaceName = getHologramType(superType).getInternalName();
                interfaces.add(superInterfaceName);
            }//from   ww w .  j  a v a  2  s . com

            for (ClassMirror interfaceMirror : elementClass.getInterfaceMirrors()) {
                Type superType = Reflection.makeArrayType(dims,
                        Type.getObjectType(interfaceMirror.getClassName().replace('.', '/')));
                String interfaceName = getHologramType(superType).getInternalName();
                interfaces.add(interfaceName);
            }

            interfaces.add(hologramType.getInternalName());

            Type nMinus1Type = Reflection.makeArrayType(dims - 1, Type.getType(Object.class));
            interfaces.add(getHologramType(nMinus1Type).getInternalName());
        }
    }
    if (!isInterface) {
        interfaces.add(getHologramType(originalType, false).getInternalName());
    }

    visitor.visit(Opcodes.V1_5, access, internalName, null, superName, interfaces.toArray(new String[0]));

    if (isInterface) {
        // Generate clone()
        String cloneDesc = Type.getMethodDescriptor(objectType);
        MethodVisitor mv = visitor.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "clone", cloneDesc,
                null, null);
        mv.visitEnd();
    } else {
        // Generate thunk constructors
        String initDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(ObjectArrayMirror.class));
        MethodVisitor mv = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", initDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", initDesc);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();

        initDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.INT_TYPE);
        mv = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", initDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ILOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", initDesc);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }

    // Generate the static field used to store the corresponding ClassMirror and the static initializer to set it
    int staticAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC;
    visitor.visitField(staticAccess, "classMirror", classMirrorType.getDescriptor(), null, null);

    InstructionAdapter mv = new InstructionAdapter(
            visitor.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null));
    mv.visitCode();
    HologramMethodGenerator.initializeStaticFields(Type.getObjectType(internalName), mv);
    mv.areturn(Type.VOID_TYPE);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    visitor.visitEnd();
}

From source file:edu.ubc.mirrors.holograms.MainEntryAdaptor.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {

    MethodVisitor superVisitor = super.visitMethod(access, name, desc, signature, exceptions);
    if (superVisitor != null && name.equals("main") && (Opcodes.ACC_STATIC & access) != 0
            && desc.equals(mainDesc)) {
        superVisitor.visitCode();/*w w  w .  j  a va  2s  .  c o m*/
        superVisitor.visitLdcInsn(Type.getObjectType(className));
        superVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        superVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(ObjectHologram.class),
                "invokeHologramMainMethod", invokeHologramMainMethodDesc);
        superVisitor.visitInsn(Opcodes.RETURN);
        superVisitor.visitMaxs(2, 1);
        superVisitor.visitEnd();
        return null;
    } else {
        return superVisitor;
    }
}