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

License:Apache License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    if (overlayTypePredicate.isOverlayDesc(owner)) {
        //System.out.println(owner + "." + name);
        if (isConstructor(name)) {
            super.visitMethodInsn(opcode, owner + "$", name, desc);
        } else {/*from   w w  w .  j a v  a  2s  . c o  m*/
            boolean isStatic = isOpcode(opcode, Opcodes.ACC_STATIC);

            String newDescriptor = desc;
            if (!isStatic) {
                String overlayClass = overlayTypePredicate.getImplementingClass(owner, name, desc);
                newDescriptor = new Descriptor(desc).toDescPrefix(overlayClass);
            }
            super.visitMethodInsn(Opcodes.INVOKESTATIC, owner + "$", name, newDescriptor);
        }
    } else {
        super.visitMethodInsn(opcode, owner, name, desc);
    }
}

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

License:Apache License

@Override
public void generatePreparedStatementSet(final CodeGenSupport cgs) {
    cgs.pushSqlHandle();/*from w  ww  .  j a v a2  s .  c o  m*/
    cgs.pushColumnIndex();
    cgs.pushFieldValue();
    cgs.mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(SqlByteArrayTypeInfo.class),
            "toPreparedStatement", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {
                    Type.getType(PreparedStatement.class), Type.INT_TYPE, Type.getType(byte[].class) }));
}

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

License:Apache License

@Override
public void generateResultSetGet(final CodeGenSupport cgs) {
    cgs.fieldSetBegin();//from w ww.  ja  v a 2  s  . c o  m
    cgs.pushSqlHandle();
    cgs.pushColumnIndex();
    cgs.mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(SqlByteArrayTypeInfo.class),
            "fromResultSet", Type.getMethodDescriptor(Type.getType(byte[].class),
                    new Type[] { Type.getType(ResultSet.class), Type.INT_TYPE }));
    cgs.fieldSetEnd();
}

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

License:Apache License

@Override
public void generatePreparedStatementSet(final CodeGenSupport cgs) {
    cgs.pushSqlHandle();//from  ww  w  . j  a  va  2s  .  c  o m
    cgs.pushColumnIndex();
    cgs.pushFieldValue();
    cgs.mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Character.class), "toString",
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] { Type.CHAR_TYPE }));
    cgs.invokePreparedStatementSet(getJavaSqlTypeAlias());
}

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

License:Apache License

@Override
public void generatePreparedStatementSet(final CodeGenSupport cgs) {
    if (cgs.getFieldReference().getColumnAnnotation().length() <= 255) {
        super.generatePreparedStatementSet(cgs);
    } else {/*  www .  j  ava2  s  .com*/
        cgs.pushSqlHandle();
        cgs.pushColumnIndex();
        cgs.pushFieldValue();
        cgs.mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(SqlStringTypeInfo.class),
                "toPreparedStatement", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {
                        Type.getType(PreparedStatement.class), Type.INT_TYPE, Type.getType(String.class) }));
    }
}

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

License:Apache License

@Override
public void generateResultSetGet(final CodeGenSupport cgs) {
    if (cgs.getFieldReference().getColumnAnnotation().length() <= 255) {
        super.generateResultSetGet(cgs);
    } else {// w  w w .jav a  2 s .  c om
        cgs.fieldSetBegin();
        cgs.pushSqlHandle();
        cgs.pushColumnIndex();
        cgs.mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(SqlStringTypeInfo.class),
                "fromResultSet", Type.getMethodDescriptor(Type.getType(String.class),
                        new Type[] { Type.getType(ResultSet.class), 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 w  w.  j a va  2s.c o  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.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void invokeRecordAllocation(String typeName) {
    final Matcher matcher = namePattern.matcher(typeName);
    if (matcher.find()) {
        typeName = matcher.group(1);/*from   w  w  w.j  av  a 2s  . c om*/
    }
    // stack: ... count newobj
    super.visitInsn(Opcodes.DUP_X1);
    // -> stack: ... newobj count newobj
    super.visitLdcInsn(typeName);
    // -> stack: ... newobj count newobj typename
    super.visitInsn(Opcodes.SWAP);
    // -> stack: ... newobj count typename newobj
    super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE, false);
    // -> stack: ... newobj
}

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

License:Apache License

private void invokeStart() {
    super.visitMethodInsn(Opcodes.INVOKESTATIC, CLASS_PATH, "start", START_SIGNATURE, false);
}