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:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java

License:Apache License

private void dispatch_getDelegate() {
    if (isStatic) {
        delegate.visitInsn(Opcodes.ACONST_NULL);
    } else {// w ww  .java 2 s.  c  om
        delegate.visitVarInsn(Opcodes.ALOAD, 0); // this
    }
    delegate.visitLdcInsn(className);
    delegate.visitLdcInsn(methodName);
    delegate.visitLdcInsn(descriptor.getMethodDesc());
    delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, GwtNativeDispatch, "getDelegate",
            GwtNativeDispatch_getDelegate);
}

From source file:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java

License:Apache License

private void nativeDelegate_invokeReturnObject() {
    String methodReturn = descriptor.getReturnDesc();
    if (methodReturn == null) {
        throw new NullPointerException(descriptor.getMethodDesc());
    }/*from w  w  w .ja  va2 s  .  c  o m*/
    switch (methodReturn.charAt(0)) {
    case 'L':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturnObject",
                "()Ljava/lang/Object;");
        delegate.visitTypeInsn(Opcodes.CHECKCAST, methodReturn.substring(1, methodReturn.length() - 1));
        delegate.visitInsn(Opcodes.ARETURN);
        break;
    case 'Z':
    case 'B':
    case 'S':
    case 'C':
    case 'I':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.IRETURN);
        break;
    case 'J':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.LRETURN);
        break;
    case 'F':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.FRETURN);
        break;
    case 'D':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.DRETURN);
        break;
    case 'V':
        delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "invokeReturn" + methodReturn,
                "()" + methodReturn);
        delegate.visitInsn(Opcodes.RETURN);
        break;
    default:
        throw new IllegalStateException(methodReturn);
    }
}

From source file:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java

License:Apache License

private int nativeDelegate_addArgument(String parameter, int index) {
    int size = 1;
    delegate.visitInsn(Opcodes.DUP);// ww  w .jav a 2s. c  om
    switch (parameter.charAt(0)) {
    case 'L':
    case '[':
        parameter = "Ljava/lang/Object;";
        delegate.visitVarInsn(Opcodes.ALOAD, index);
        break;
    case 'Z':
    case 'B':
    case 'C':
    case 'S':
    case 'I':
        delegate.visitVarInsn(Opcodes.ILOAD, index);
        break;
    case 'J':
        delegate.visitVarInsn(Opcodes.LLOAD, index);
        size = 2;
        break;
    case 'F':
        delegate.visitVarInsn(Opcodes.FLOAD, index);
        break;
    case 'D':
        delegate.visitVarInsn(Opcodes.DLOAD, index);
        size = 2;
        break;
    default:
        throw new IllegalStateException(parameter);
    }
    delegate.visitMethodInsn(Opcodes.INVOKEVIRTUAL, InvocationDelegate, "addArg", "(" + parameter + ")V");
    return size;
}

From source file:com.google.gwtorm.schema.sql.SqlBooleanTypeInfo.java

License:Apache License

@Override
public void generateResultSetGet(final CodeGenSupport cgs) {
    cgs.fieldSetBegin();//from  w  w  w . j  a  v a 2 s .co  m
    cgs.mv.visitLdcInsn(getTrueValue());
    cgs.pushSqlHandle();
    cgs.pushColumnIndex();
    cgs.invokeResultSetGet(getJavaSqlTypeAlias());
    cgs.mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(String.class), "equals",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.getType(Object.class) }));
    cgs.fieldSetEnd();
}

From source file:com.google.gwtorm.schema.sql.SqlCharTypeInfo.java

License:Apache License

@Override
public void generateResultSetGet(final CodeGenSupport cgs) {
    cgs.fieldSetBegin();// w  w w.j ava2 s. co  m
    cgs.pushSqlHandle();
    cgs.pushColumnIndex();
    cgs.invokeResultSetGet(getJavaSqlTypeAlias());
    cgs.push(0);
    cgs.mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(String.class), "charAt",
            Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { Type.INT_TYPE }));
    cgs.fieldSetEnd();
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

/**
 * Reflection-based allocation (@see java.lang.reflect.Array#newInstance) is
 * triggered with a static method call (INVOKESTATIC), so we hook it here.
 * Class initialization is triggered with a constructor call (INVOKESPECIAL)
 * so we hook that here too as a proxy for the new bytecode which leaves an
 * uninitialized object on the stack that we're not allowed to touch.
 * {@link java.lang.Object#clone} is also a call to INVOKESPECIAL,
 * and is hooked here.  {@link java.lang.Class#newInstance} and
 * {@link java.lang.reflect.Constructor#newInstance} are both
 * INVOKEVIRTUAL calls, so they are hooked here, as well.
 *///  w ww.  j  a  v  a 2 s  . co m
@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String signature,
        final boolean itf) {
    if (opcode == Opcodes.INVOKESTATIC &&
    // Array does its own native allocation.  Grr.
            owner.equals("java/lang/reflect/Array") && name.equals("newInstance")) {
        if (signature.equals("(Ljava/lang/Class;I)Ljava/lang/Object;")) {

            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            // stack: ... class count
            final int countIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, countIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... class count
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj
            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... newobj newobj count
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj count newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        } else if (signature.equals("(Ljava/lang/Class;[I)Ljava/lang/Object;")) {
            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
            // stack: ... class dimsArray
            pushProductOfIntArrayOnStack();
            // -> stack: ... class dimsArray product
            final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, productIndex);
            // -> stack: ... class dimsArray

            super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
            // -> stack: ... class dimsArray
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj

            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, productIndex);
            // -> stack: ... newobj newobj product
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj product newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        }
    }

    if (opcode == Opcodes.INVOKEVIRTUAL) {
        if ("clone".equals(name) && owner.startsWith("[")) {
            super.visitMethodInsn(opcode, owner, name, signature, itf);

            int i = 0;
            while (i < owner.length()) {
                if (owner.charAt(i) != '[') {
                    break;
                }
                i++;
            }
            if (i > 1) {
                // -> stack: ... newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... arrayref
                calculateArrayLengthAndDispatch(owner.substring(i), i);
            } else {
                // -> stack: ... newobj
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... newobj newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... newobj arrayref
                super.visitInsn(Opcodes.ARRAYLENGTH);
                // -> stack: ... newobj length
                super.visitInsn(Opcodes.SWAP);
                // -> stack: ... length newobj
                invokeRecordAllocation(owner.substring(i));
            }
            return;
        } else if ("newInstance".equals(name)) {
            if ("java/lang/Class".equals(owner) && "()Ljava/lang/Object;".equals(signature)) {
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... Class Class
                super.visitMethodInsn(opcode, owner, name, signature, itf);
                // -> stack: ... Class newobj
                super.visitInsn(Opcodes.DUP_X1);
                // -> stack: ... newobj Class newobj
                super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, CLASS_RECORDER_SIG,
                        false);
                // -> stack: ... newobj
                return;
            } else if ("java/lang/reflect/Constructor".equals(owner)
                    && "([Ljava/lang/Object;)Ljava/lang/Object;".equals(signature)) {
                buildRecorderFromObject(opcode, owner, name, signature, itf);
                return;
            }
        }
    }

    if (opcode == Opcodes.INVOKESPECIAL) {
        if ("clone".equals(name) && "java/lang/Object".equals(owner)) {
            buildRecorderFromObject(opcode, owner, name, signature, itf);
            return;
        } else if ("<init>".equals(name) && outstandingAllocs > 0) {
            // Tricky because superclass initializers mean there can be more calls
            // to <init> than calls to NEW; hence outstandingAllocs.
            --outstandingAllocs;

            // Most of the time (i.e. in bytecode generated by javac) it is the case
            // that following an <init> call the top of the stack has a reference ot
            // the newly-initialized object.  But nothing in the JVM Spec requires
            // this, so we need to play games with the stack to make an explicit
            // extra copy (and then discard it).

            dupStackElementBeforeSignatureArgs(signature);
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            super.visitLdcInsn(-1);
            super.visitInsn(Opcodes.SWAP);
            invokeRecordAllocation(owner);
            super.visitInsn(Opcodes.POP);
            return;
        }
    }

    super.visitMethodInsn(opcode, owner, name, signature, itf);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void buildRecorderFromObject(final int opcode, final String owner, final String name,
        final String signature, final boolean itf) {
    super.visitMethodInsn(opcode, owner, name, signature, itf);
    // -> stack: ... newobj
    super.visitInsn(Opcodes.DUP);
    // -> stack: ... newobj newobj
    super.visitInsn(Opcodes.DUP);
    // -> stack: ... newobj newobj newobj
    // We could be instantiating this class or a subclass, so we
    // have to get the class the hard way.
    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    // -> stack: ... newobj newobj Class
    super.visitInsn(Opcodes.SWAP);
    // -> stack: ... newobj Class newobj
    super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, CLASS_RECORDER_SIG, false);
    // -> stack: ... newobj
}

From source file:com.google.template.soy.jbcsrc.MethodRef.java

License:Apache License

static MethodRef create(java.lang.reflect.Method method) {
    Class<?> clazz = method.getDeclaringClass();
    TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass());
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    ImmutableList<Type> argTypes;
    if (isStatic) {
        argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method));
    } else {// ww w. jav a2 s  . c  o m
        // for instance methods the first 'argument' is always an instance of the class.
        argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method))
                .build();
    }
    return new AutoValue_MethodRef(
            clazz.isInterface() ? Opcodes.INVOKEINTERFACE
                    : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
            ownerType, org.objectweb.asm.commons.Method.getMethod(method), Type.getType(method.getReturnType()),
            argTypes, Features.of());
}

From source file:com.google.template.soy.jbcsrc.MethodRef.java

License:Apache License

static MethodRef createInstanceMethod(TypeInfo owner, Method method) {
    return new AutoValue_MethodRef(Opcodes.INVOKEVIRTUAL, owner, method, method.getReturnType(),
            ImmutableList.<Type>builder().add(owner.type()).add(method.getArgumentTypes()).build(),
            Features.of());//from  ww w .jav a  2s .  c o  m
}

From source file:com.google.template.soy.jbcsrc.restricted.MethodRef.java

License:Apache License

public static MethodRef create(java.lang.reflect.Method method) {
    Class<?> clazz = method.getDeclaringClass();
    TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass());
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    ImmutableList<Type> argTypes;
    if (isStatic) {
        argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method));
    } else {//w  w w .  jav a2s  .c o m
        // for instance methods the first 'argument' is always an instance of the class.
        argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method))
                .build();
    }
    return new AutoValue_MethodRef(
            clazz.isInterface() ? Opcodes.INVOKEINTERFACE
                    : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
            ownerType, Method.getMethod(method), Type.getType(method.getReturnType()), argTypes, Features.of());
}