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:jerl.bcm.inj.impl.InjectCollection.java

License:Apache License

public void injectGC(MethodVisitor mv) {
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "gc", "()V");
}

From source file:jerl.bcm.inj.impl.InjectCollection.java

License:Apache License

public void injectCrash(MethodVisitor mv) {
    // TODO: change to correct call
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "crash", "()V");
}

From source file:jerl.bcm.inj.impl.InjectCollection.java

License:Apache License

public void injectRegister(MethodVisitor mv, int id) {
    mv.visitLdcInsn(new Integer(id));
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "jerl/bcm/util/Register", "register", "(I)V");
}

From source file:jerl.bcm.inj.impl.InjectCollection.java

License:Apache License

public void injectPrintRegs(MethodVisitor mv) {
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "jerl/bcm/util/Register", "printRegistrations", "()V");
}

From source file:jerl.bcm.inj.impl.MethodCallRemove.java

License:Apache License

@Override
public void inject(MethodVisitor mv) {
    // TODO Auto-generated method stub
    // pop arguments
    Type[] args = Type.getArgumentTypes(desc);
    for (int i = 0; i < args.length; i++) {
        if (args[i].getSort() == Type.DOUBLE || args[i].getSort() == Type.LONG) {
            mv.visitInsn(Opcodes.POP2);/*from ww  w. j a  va  2 s .c  om*/
        } else {
            mv.visitInsn(Opcodes.POP);
        }
    }
    if (opcode == Opcodes.INVOKESTATIC) {
    } else {
        mv.visitInsn(Opcodes.POP);
    }
}

From source file:jerl.bcm.inj.impl.MethodCallReplace.java

License:Apache License

@Override
public void inject(MethodVisitor mv) {
    // TODO Auto-generated method stub
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, new_owner, new_name, new_desc);
}

From source file:jerl.semc.MethodEntrySEMCInitLog.java

License:Apache License

@Override
public void inject(MethodVisitor mv) {
    mv.visitLdcInsn(Type.getType("L" + className + ";"));
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, FieldAddSEMCLog.LOG_CLASS_NAME, "getLogger",
            "(Ljava/lang/Class;)L" + FieldAddSEMCLog.LOG_CLASS_NAME + ";");
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, FieldAddSEMCLog.LOG_FIELD_NAME,
            "L" + FieldAddSEMCLog.LOG_CLASS_NAME + ";");
}

From source file:jp.co.dgic.testing.common.virtualmock.asm.AbstractAsmMethodVisitor.java

License:Open Source License

public void createMethodCall(int opcode, String owner, String name, String desc) {

    boolean isStaticMethod = (opcode == Opcodes.INVOKESTATIC);
    boolean isInterface = (opcode == Opcodes.INVOKEINTERFACE);

    InternalMockObjectManager.printConsole(
            "[INVOKE METHOD] : " + (isStaticMethod ? " static " : " ") + owner + "#" + name + " " + desc);

    if (!canReplace(owner, isInterface)) {
        // call real method
        mv.visitMethodInsn(opcode, owner, name, desc);
        return;//  ww  w.ja  v  a  2  s  .  co  m
    }

    // createCreateArgsArray
    Label hasMockReturnValue = new Label();
    Type[] argTypes = Type.getArgumentTypes(desc);
    if (argTypes == null) {
        argTypes = new Type[0];
    }

    // before method call
    createCopyStackArgsToLocalVariables(isStaticMethod, argTypes);
    createCreateArgsArray(isStaticMethod, argTypes, _maxLocals + 1);

    // call MockObjecManager.indicateCalledAndGetReturnValue
    mv.visitLdcInsn(makeKey(owner.replace('/', '.'), name));
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitMethodInsn(INVOKESTATIC, MANAGER_CLASS_NAME, "indicateCalledAndGetReturnValue",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitVarInsn(ASTORE, _maxLocals);

    // if (mock value != null) GOTO LABEL : mock value is NOT null
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitJumpInsn(IFNONNULL, hasMockReturnValue);

    createPutArgsIntoStackFromLocalValriables(isStaticMethod, argTypes);

    // call real method
    mv.visitMethodInsn(opcode, owner, name, desc);

    // after method call
    // GOTO LABEL : next statement
    Label toNextStatement = new Label();
    mv.visitJumpInsn(GOTO, toNextStatement);

    // LABEL : mock value is NOT null
    mv.visitLabel(hasMockReturnValue);

    // throw exception
    String[] exceptions = getExceptions(owner, name, desc);
    createThrowExceptions(exceptions);
    createInvoleThrowException(owner, name);

    Type returnType = Type.getReturnType(desc);
    if (isVoid(returnType, name)) {
        // check return value
        mv.visitVarInsn(ALOAD, _maxLocals);
        mv.visitLdcInsn(makeName(owner, name));
        mv.visitMethodInsn(INVOKESTATIC, MANAGER_CLASS_NAME, "checkReturnTypeIsIgnoreOrNullReturnValue",
                "(Ljava/lang/Object;Ljava/lang/String;)V");
    } else {
        String returnTypeClassName = getReturnTypeClassName(returnType, owner, name);

        // check return value
        // instanceof NullReturnValue
        mv.visitVarInsn(ALOAD, _maxLocals);
        mv.visitTypeInsn(INSTANCEOF, NULL_RETURN_VALUE_CLASS_NAME);
        Label toSetNullValue = new Label();
        mv.visitJumpInsn(IFNE, toSetNullValue);
        // instanceof return type
        mv.visitVarInsn(ALOAD, _maxLocals);
        mv.visitTypeInsn(INSTANCEOF, returnTypeClassName);
        Label toMockValueLoad = new Label();
        mv.visitJumpInsn(IFNE, toMockValueLoad);

        // checkReturnTypeIsNullReturnValue
        mv.visitVarInsn(ALOAD, _maxLocals);
        mv.visitLdcInsn(makeName(owner, name));
        mv.visitMethodInsn(INVOKESTATIC, MANAGER_CLASS_NAME, "checkReturnTypeIsNullReturnValue",
                "(Ljava/lang/Object;Ljava/lang/String;)V");

        // set null value
        mv.visitLabel(toSetNullValue);
        mv.visitInsn(getZeroOpcodeByType(returnType));

        mv.visitJumpInsn(GOTO, toNextStatement);

        // load mock return value
        mv.visitLabel(toMockValueLoad);
        mv.visitVarInsn(ALOAD, _maxLocals);
        mv.visitTypeInsn(CHECKCAST, returnTypeClassName);

        if (isPrimitive(returnType)) {
            String methodNameOfToValue = getToValueMethodName(returnType);
            String descriptorOfToValue = getToValueDescriptor(returnType);
            mv.visitMethodInsn(INVOKEVIRTUAL, returnTypeClassName, methodNameOfToValue, descriptorOfToValue);
        }
    }

    // LABEL : next statement
    mv.visitLabel(toNextStatement);

}

From source file:jp.co.dgic.testing.common.virtualmock.asm.AbstractAsmMethodVisitor.java

License:Open Source License

public void createConstructorCall(int opcode, String owner, String name, String desc) {

    if (isSuperOrThis(owner)) {
        // call real method
        mv.visitMethodInsn(opcode, owner, name, desc);
        return;/*from   w ww.  ja v  a  2s .c  om*/
    }

    boolean isStaticMethod = (opcode == Opcodes.INVOKESTATIC);
    InternalMockObjectManager.printConsole(
            "[INVOKE CONSTRUCTOR] : " + (isStaticMethod ? " static " : " ") + owner + "#" + name + " " + desc);

    if (!canNewExprReplace(owner)) {
        // call real method
        mv.visitMethodInsn(opcode, owner, name, desc);
        return;
    }

    // createCreateArgsArray
    Label hasMockReturnValue = new Label();
    Type[] argTypes = Type.getArgumentTypes(desc);
    if (argTypes == null) {
        argTypes = new Type[0];
    }

    // before method call
    createCopyStackArgsToLocalVariables(isStaticMethod, argTypes);
    createCreateArgsArray(isStaticMethod, argTypes, _maxLocals + 1);

    // call MockObjecManager.indicateCalledAndGetReturnValueForNewExpr
    mv.visitLdcInsn(makeKey(owner.replace('/', '.'), name));
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitLdcInsn(new Boolean(isOwnSource(owner)));
    mv.visitMethodInsn(INVOKESTATIC, MANAGER_CLASS_NAME, "indicateCalledAndGetReturnValueForNewExpr",
            "(Ljava/lang/String;[Ljava/lang/Object;Z)Ljava/lang/Object;");
    mv.visitVarInsn(ASTORE, _maxLocals);

    // if (mock value != null) GOTO LABEL : mock value is NOT null
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitJumpInsn(IFNONNULL, hasMockReturnValue);

    createPutArgsIntoStackFromLocalValriables(isStaticMethod, argTypes);

    // call real method
    mv.visitMethodInsn(opcode, owner, name, desc);

    // after method call
    // GOTO LABEL : next statement
    Label toNextStatement = new Label();
    mv.visitJumpInsn(GOTO, toNextStatement);

    // LABEL : mock value is NOT null
    mv.visitLabel(hasMockReturnValue);

    if (isOwnSource(owner)) {
        mv.visitVarInsn(ALOAD, _maxLocals);
        createCreateArgsArray(isStaticMethod, argTypes, _maxLocals + 1);
        mv.visitLdcInsn(makeKey(owner.replace('/', '.'), name));
        mv.visitVarInsn(ALOAD, _maxLocals);
        mv.visitMethodInsn(INVOKESTATIC, MANAGER_CLASS_NAME, "indicateCalled",
                "(Ljava/lang/String;[Ljava/lang/Object;)V");
        mv.visitVarInsn(ASTORE, _maxLocals);
    }

    // throw exception
    String[] exceptions = getExceptions(owner, name, desc);
    createThrowExceptions(exceptions);
    createInvoleThrowException(owner, name);

    Type returnType = Type.getReturnType(desc);

    if (!isAfterDup) {
        mv.visitLabel(toNextStatement);
        return;
    }

    mv.visitInsn(POP);

    String returnTypeClassName = getReturnTypeClassName(returnType, owner, name);

    // check return value
    // instanceof NullReturnValue
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitTypeInsn(INSTANCEOF, NULL_RETURN_VALUE_CLASS_NAME);
    Label toSetNullValue = new Label();
    mv.visitJumpInsn(IFNE, toSetNullValue);
    // instanceof return type
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitTypeInsn(INSTANCEOF, returnTypeClassName);
    Label toMockValueLoad = new Label();
    mv.visitJumpInsn(IFNE, toMockValueLoad);

    // checkReturnTypeIsNullReturnValue
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitLdcInsn(makeName(owner, name));
    mv.visitMethodInsn(INVOKESTATIC, MANAGER_CLASS_NAME, "checkReturnTypeForNewExpr",
            "(Ljava/lang/Object;Ljava/lang/String;)V");

    // set null value
    mv.visitLabel(toSetNullValue);
    mv.visitInsn(ACONST_NULL);

    mv.visitJumpInsn(GOTO, toNextStatement);

    // load mock return value
    mv.visitLabel(toMockValueLoad);
    mv.visitVarInsn(ALOAD, _maxLocals);
    mv.visitTypeInsn(CHECKCAST, returnTypeClassName);

    if (isPrimitive(returnType)) {
        String methodNameOfToValue = getToValueMethodName(returnType);
        String descriptorOfToValue = getToValueDescriptor(returnType);
        mv.visitMethodInsn(INVOKEVIRTUAL, returnTypeClassName, methodNameOfToValue, descriptorOfToValue);
    }

    // LABEL : next statement
    mv.visitLabel(toNextStatement);

}

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private void addNonStaticMethods(CompilerContext context, ClassVisitor cv) {
    MethodVisitor mv;/*from ww w  .j a v a2s. c om*/

    // public int exec(int returnAddress, int alternativeReturnAddress, boolean isJump) throws Exception;
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getExecMethodName(), context.getExecMethodDesc(), null,
            exceptions);
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassName(), context.getStaticExecMethodName(),
            context.getStaticExecMethodDesc());
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // private static IExecutable e;
    FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, context.getReplaceFieldName(),
            executableDescriptor, null, null);
    fv.visitEnd();

    // public void setExecutable(IExecutable e);
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getReplaceMethodName(), context.getReplaceMethodDesc(),
            null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();

    // public IExecutable getExecutable();
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getGetMethodName(), context.getGetMethodDesc(), null,
            exceptions);
    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}