Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

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

Introduction

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

Prototype

int INVOKEVIRTUAL

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

Click Source Link

Usage

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitStoreState(MethodVisitor mv, int idx, FrameInfo fi, int numArgsToPreserve) {
    Frame f = frames[fi.endInstruction];

    if (fi.lBefore != null)
        fi.lBefore.accept(mv);//w  w w  .  j a va2  s.c o m

    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    emitConst(mv, idx);
    emitConst(mv, fi.numSlots);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "pushMethod", "(II)V");

    // store operand stack
    for (int i = f.getStackSize(); i-- > 0;) {
        BasicValue v = (BasicValue) f.getStack(i);
        if (!isOmitted(v)) {
            if (!isNullType(v)) {
                int slotIdx = fi.stackSlotIndices[i];
                assert slotIdx >= 0 && slotIdx < fi.numSlots;
                emitStoreValue(mv, v, lvarStack, slotIdx, -1);
            } else {
                db.log(LogLevel.DEBUG, "NULL stack entry: type=%s size=%d", v.getType(), v.getSize());
                mv.visitInsn(Opcodes.POP);
            }
        }
    }

    // store local vars
    for (int i = firstLocal; i < f.getLocals(); i++) {
        BasicValue v = (BasicValue) f.getLocal(i);
        if (!isNullType(v)) {
            mv.visitVarInsn(v.getType().getOpcode(Opcodes.ILOAD), i);
            int slotIdx = fi.localSlotIndices[i];
            assert slotIdx >= 0 && slotIdx < fi.numSlots;
            emitStoreValue(mv, v, lvarStack, slotIdx, i);
        }
    }

    // restore last numArgsToPreserve operands
    for (int i = f.getStackSize() - numArgsToPreserve; i < f.getStackSize(); i++) {
        BasicValue v = (BasicValue) f.getStack(i);
        if (!isOmitted(v)) {
            if (!isNullType(v)) {
                int slotIdx = fi.stackSlotIndices[i];
                assert slotIdx >= 0 && slotIdx < fi.numSlots;
                emitRestoreValue(mv, v, lvarStack, slotIdx, -1);
            } else {
                mv.visitInsn(Opcodes.ACONST_NULL);
            }
        }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPostRestore(MethodVisitor mv) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "postRestore", "()V");
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPreemptionPoint(MethodVisitor mv, int type) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    switch (type) {
    case 0://from  w  w  w. j  a va2s  .  c o  m
        mv.visitInsn(Opcodes.ICONST_0);
        break;
    case 1:
        mv.visitInsn(Opcodes.ICONST_1);
        break;
    case 2:
        mv.visitInsn(Opcodes.ICONST_2);
        break;
    case 3:
        mv.visitInsn(Opcodes.ICONST_3);
        break;
    default:
        throw new AssertionError("Unsupported type: " + type);
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "preemptionPoint", "(I)V");
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitRestoreValue(MethodVisitor mv, BasicValue v, int lvarStack, int idx, int lvar) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    emitConst(mv, idx);//from   w  w w  . ja v a  2s  . co  m

    switch (v.getType().getSort()) {
    case Type.OBJECT:
        String internalName = v.getType().getInternalName();
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getObject", "(I)Ljava/lang/Object;");
        if (!internalName.equals("java/lang/Object")) // don't cast to Object ;)
            mv.visitTypeInsn(Opcodes.CHECKCAST, internalName);
        //                println(mv, "RESTORE " + (lvar >= 0 ? ("VAR " + lvar + ": ") : "OPRND: "));
        break;
    case Type.ARRAY:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getObject", "(I)Ljava/lang/Object;");
        mv.visitTypeInsn(Opcodes.CHECKCAST, v.getType().getDescriptor());
        break;
    case Type.BYTE:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        mv.visitInsn(Opcodes.I2B);
        break;
    case Type.SHORT:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        mv.visitInsn(Opcodes.I2S);
        break;
    case Type.CHAR:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        mv.visitInsn(Opcodes.I2C);
        break;
    case Type.BOOLEAN:
    case Type.INT:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getInt", "(I)I");
        break;
    case Type.FLOAT:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getFloat", "(I)F");
        break;
    case Type.LONG:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getLong", "(I)J");
        break;
    case Type.DOUBLE:
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "getDouble", "(I)D");
        break;
    default:
        throw new InternalError("Unexpected type: " + v.getType());
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void println(MethodVisitor mv, String prefix, int refVar) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);/*from ww  w.j  a  va2 s .  c o m*/
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Thread", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" " + prefix);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" var " + refVar + ":");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");

    mv.visitVarInsn(Opcodes.ALOAD, refVar);

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/Object;)Ljava/lang/StringBuilder;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void println(MethodVisitor mv, String prefix) {
    mv.visitInsn(Opcodes.DUP); // S1 S1

    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); // PrintStream S1 S1

    mv.visitInsn(Opcodes.SWAP); // S1 PrintStream S1

    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder"); // StringBuilder S1 PrintStream S1
    mv.visitInsn(Opcodes.DUP); // StringBuilder StringBuilder S1 PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V"); // StringBuilder S1 PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Thread", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" " + prefix); // prefix StringBuilder S1 PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;"); // StringBuilder S1 PrintStream S1

    mv.visitInsn(Opcodes.SWAP); // S1 StringBuilder PrintStream S1

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/Object;)Ljava/lang/StringBuilder;"); // StringBuilder PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;"); // PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); // S1
}

From source file:co.paralleluniverse.fibers.instrument.MethodDatabase.java

License:Open Source License

private int isMethodSuspendable0(String className, String methodName, String methodDesc, int opcode) {
    if (methodName.charAt(0) == '<')
        return NONSUSPENDABLE; // special methods are never suspendable

    if (isYieldMethod(className, methodName))
        return SUSPENDABLE;

    ClassEntry entry = getClassEntry(className);
    if (entry == null) {
        entry = CLASS_NOT_FOUND;// w  ww.j av a2s .c  om

        if (cl != null) {
            log(LogLevel.INFO, "Trying to read class: %s", className);

            CheckInstrumentationVisitor civ = checkClass(className);
            if (civ == null)
                log(LogLevel.WARNING, "Class not found: %s", className);
            else
                entry = civ.getClassEntry();
        } else
            log(LogLevel.WARNING, "Can't check class: %s", className);

        recordSuspendableMethods(className, entry);
    }

    if (entry == CLASS_NOT_FOUND) {
        if (isJavaCore(className))
            return MAYBE_CORE;

        //            if (JavaAgent.isActive())
        //                throw new AssertionError();
        return UNKNOWN;
    }

    SuspendableType susp1 = entry.check(methodName, methodDesc);

    int suspendable = UNKNOWN;
    if (susp1 == null)
        suspendable = UNKNOWN;
    else if (susp1 == SuspendableType.SUSPENDABLE)
        suspendable = SUSPENDABLE;
    else if (susp1 == SuspendableType.SUSPENDABLE_SUPER)
        suspendable = SUSPENDABLE_ABSTRACT;
    else if (susp1 == SuspendableType.NON_SUSPENDABLE)
        suspendable = NONSUSPENDABLE;

    if (suspendable == UNKNOWN) {
        if (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKESTATIC
                || opcode == Opcodes.INVOKESPECIAL) {
            if (entry.getSuperName() != null)
                suspendable = isMethodSuspendable0(entry.getSuperName(), methodName, methodDesc, opcode);
        }
        if (opcode == Opcodes.INVOKEINTERFACE || opcode == Opcodes.INVOKEVIRTUAL) { // can be INVOKEVIRTUAL on an abstract class implementing the interface
            for (String iface : entry.getInterfaces()) {
                int s = isMethodSuspendable0(iface, methodName, methodDesc, opcode);
                if (s > suspendable)
                    suspendable = s;
                if (suspendable > MAYBE_CORE)
                    break;
            }
        }
    }

    return suspendable;
}

From source file:com.alibaba.hotswap.processor.jdk.lang.modifier.ClassNewInstanceModifier.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();

    mv.visitVarInsn(Opcodes.ALOAD, 0);//w ww.j a v a 2  s.  com
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
            "(Ljava/lang/String;)Z");
    Label old = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, old);

    // check it as a primary constructor
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "getClassMeta",
            "(Ljava/lang/String;)Lcom/alibaba/hotswap/meta/ClassMeta;");
    mv.visitLdcInsn(HotswapConstants.INIT);
    mv.visitLdcInsn("()V");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ClassMeta.class), "isPrimaryInitMethod",
            "(Ljava/lang/String;Ljava/lang/String;)Z");
    mv.visitJumpInsn(Opcodes.IFNE, old);

    // get uniform constructor
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapMethodUtil.class),
            "getConstructorParamTypes", "()[Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getConstructor",
            "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;");

    // index and objs
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitLdcInsn(HotswapConstants.INIT);
    mv.visitLdcInsn("()V");
    loadArgs();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapMethodUtil.class), "getMethodParams",
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)[Ljava/lang/Object;");

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Constructor", "newInstance",
            "([Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(old);
}

From source file:com.alibaba.hotswap.processor.jdk.lang.modifier.PrivateGetDeclaredConstructors.java

License:Open Source License

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

    if (opcode == Opcodes.INVOKESPECIAL && owner.equals("java/lang/Class")
            && name.equals("getDeclaredConstructors0") && desc.equals("(Z)[Ljava/lang/reflect/Constructor;")) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
                "(Ljava/lang/String;)Z");
        Label old = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, old);

        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(MethodReflectHelper.class),
                "getDeclaredConstructors0", "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Constructor;");
        Label end = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, end);
        mv.visitLabel(old);/*from  w w w  .j  av a2s.  c om*/
        super.visitMethodInsn(opcode, owner, name, desc);
        mv.visitLabel(end);
    } else {
        super.visitMethodInsn(opcode, owner, name, desc);
    }
}

From source file:com.alibaba.hotswap.processor.jdk.lang.modifier.PrivateGetDeclaredFieldsModifier.java

License:Open Source License

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

    if (opcode == Opcodes.INVOKESPECIAL && owner.equals("java/lang/Class") && name.equals("getDeclaredFields0")
            && desc.equals("(Z)[Ljava/lang/reflect/Field;")) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
                "(Ljava/lang/String;)Z");
        Label old = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, old);

        // mv.visitInsn(Opcodes.POP2);
        // mv.visitVarInsn(Opcodes.ALOAD, 0);
        // mv.visitVarInsn(Opcodes.ILOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(FieldReflectHelper.class),
                "privateGetDeclaredFields0", "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Field;");
        Label end = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, end);
        mv.visitLabel(old);/*from   w w  w.  ja  va  2 s .com*/
        super.visitMethodInsn(opcode, owner, name, desc);
        mv.visitLabel(end);
    } else {
        super.visitMethodInsn(opcode, owner, name, desc);
    }
}