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:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxInt() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "intValue", "()I"));
    return il;/*w ww  .  j  ava2s.  c o  m*/
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxLong() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "longValue", "()J"));
    return il;// w w w . java2  s .  c  o  m
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxFloat() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "floatValue", "()F"));
    return il;/*from   ww w.jav a 2 s  .com*/
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxDouble() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "doubleValue", "()D"));
    return il;//from   www.  ja  v a  2 s  . co  m
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void castAndUnboxIfNeeded(MethodVisitor mv, Field field) {
    if (!field.getType().isPrimitive()) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(field.getType()));
        return;/*from  w  w  w.  j a  v a 2 s.  c o  m*/
    }

    Type type = Type.getType(field.getType());
    switch (type.getSort()) {
    case Type.BOOLEAN:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
        break;
    case Type.INT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
        break;
    case Type.SHORT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Short");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
        break;
    case Type.LONG:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Long");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
        break;
    case Type.FLOAT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Float");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
        break;
    case Type.DOUBLE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Double");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
        break;
    case Type.BYTE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Byte");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
        break;
    case Type.CHAR:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Character");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
        break;
    }
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Creates the method invoking wrapper method.
 *///from w w w  . ja va 2  s . c  om
private static void createMethod(Class clazz, String name, Method refMethod, ClassWriter cw, String methodName,
        String desc, boolean argsParams, boolean returnValue, Class... parameterTypes) {

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS, methodName, desc, null, null);

    boolean isStatic = Modifier.isStatic(refMethod.getModifiers());
    boolean isInteface = Modifier.isInterface(refMethod.getDeclaringClass().getModifiers());

    final int invokeCode;
    if (isStatic) {
        invokeCode = Opcodes.INVOKESTATIC;
    } else {
        invokeCode = isInteface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(clazz));
    }

    if (argsParams) {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitIntInsn(Opcodes.BIPUSH, i);
            mv.visitInsn(Opcodes.AALOAD);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    } else {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    }

    mv.visitMethodInsn(invokeCode, Type.getInternalName(clazz), name, Type.getMethodDescriptor(refMethod));

    if (returnValue) {
        prepareResult(mv, refMethod);
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitMaxs(1, 1); // ignored since ClassWriter set as ClassWriter.COMPUTE_MAXS
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Unbox the input parameters//from www. j  ava 2  s  . c  om
 */
private static void prepareParameter(MethodVisitor mv, Type type) {

    switch (type.getSort()) {
    case Type.BOOLEAN:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
        break;
    case Type.BYTE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Byte");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
        break;
    case Type.CHAR:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Character");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
        break;
    case Type.SHORT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Short");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
        break;
    case Type.INT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
        break;
    case Type.LONG:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Long");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
        break;
    case Type.FLOAT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Float");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
        break;
    case Type.DOUBLE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Double");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
        break;
    default:
        mv.visitTypeInsn(Opcodes.CHECKCAST, type.getInternalName());
        break;
    }
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitInvokeDynamicInsn(final String name, final String desc, final Handle bsm,
        final Object... bsmArgs) {
    assert name != null && desc != null && bsm != null && bsmArgs != null;
    /**********//from  w  w  w . ja v a 2s.  c  o  m
     * INVOKE *
     **********/
    final M m = getDu().getDynamicM(name, desc);
    final M bsM = handle2m(bsm);
    final Object[] bsArgs = new Object[bsmArgs.length];
    for (int i = 0; i < bsArgs.length; ++i) {
        // don't leak ASM types
        Object arg = bsmArgs[i];
        if (arg instanceof Handle) {
            arg = handle2m((Handle) arg);
        }
        bsArgs[i] = arg;
    }
    add(new INVOKE(this.ops.size(), Opcodes.INVOKEVIRTUAL, this.line, m, bsM, bsArgs));
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String desc,
        final boolean itf) {
    if (owner == null || name == null || desc == null) {
        log.warn(getM() + ": Cannot read invoke operation with method name '" + owner + "." + name
                + "' and descriptor '" + desc + "'!");
        return;//from ww w .  j ava2  s  .c om
    }
    switch (opcode) {
    /**********
     * INVOKE *
     **********/
    case Opcodes.INVOKEINTERFACE:
    case Opcodes.INVOKESPECIAL:
        // Constructor or supermethod (any super) or private method callout.
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEVIRTUAL: {
        final T ownerT = getDu().getT(owner);
        if (opcode == Opcodes.INVOKEINTERFACE) {
            ownerT.setInterface(true); // static also possible in interface since JVM 8
        }
        assert opcode != Opcodes.INVOKEINTERFACE || itf;

        final M refM = ownerT.getM(name, desc);
        refM.setStatic(opcode == Opcodes.INVOKESTATIC);
        add(new INVOKE(this.ops.size(), opcode, this.line, refM, opcode == Opcodes.INVOKESPECIAL));
        break;
    }
    default:
        log.warn(getM() + ": Unknown method insn opcode '" + opcode + "'!");
    }
}

From source file:org.devinprogress.YAIF.Transformer.ASMTransformer.java

public static void hookPostTabComplete(MethodNode mn) {
    AbstractInsnNode n = ASMHelper.getNthInsnNode(mn, Opcodes.INVOKEVIRTUAL, 2);
    ASMHelper.InsertInvokeStaticAfter(mn, n, "org.devinprogress.YAIF.YetAnotherInputFix", "onTabCompletePacket",
            "()V");
}