Example usage for org.objectweb.asm MethodVisitor visitIincInsn

List of usage examples for org.objectweb.asm MethodVisitor visitIincInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitIincInsn.

Prototype

public void visitIincInsn(final int var, final int increment) 

Source Link

Document

Visits an IINC instruction.

Usage

From source file:mt.swift.Deserializer.java

License:Apache License

private void generateReadList(MethodVisitor methodVisitor, FrameRegisterManager context, ListType listType) {
    // protocol.readListBegin()
    int tlistSizeLocal = context.newAnonymousSlot();
    int loopCounterLocal = context.newAnonymousSlot();

    methodVisitor.visitVarInsn(ALOAD, context.getSlot("protocol"));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "com/facebook/thrift/protocol/TProtocol", "readListBegin",
            "()Lcom/facebook/thrift/protocol/TList;");
    methodVisitor.visitFieldInsn(GETFIELD, "com/facebook/thrift/protocol/TList", "size", "I");
    methodVisitor.visitVarInsn(ISTORE, tlistSizeLocal);

    // result = new ArrayList(tlist.size)
    methodVisitor.visitTypeInsn(NEW, "java/util/ArrayList");
    methodVisitor.visitInsn(DUP);//from www  .j a  v  a2s.  c  o m
    methodVisitor.visitVarInsn(ILOAD, tlistSizeLocal);
    methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V");

    // i = 0
    methodVisitor.visitInsn(ICONST_0);
    methodVisitor.visitVarInsn(ISTORE, loopCounterLocal); // #4 = loop counter

    Label done = new Label();
    Label loop = new Label();
    methodVisitor.visitLabel(loop);
    methodVisitor.visitVarInsn(ILOAD, loopCounterLocal);
    methodVisitor.visitVarInsn(ILOAD, tlistSizeLocal);
    methodVisitor.visitJumpInsn(IF_ICMPGE, done);

    methodVisitor.visitInsn(DUP); // ArrayList

    generateReadElement(methodVisitor, context, listType.getValueType());
    generateConvertToObject(methodVisitor, listType.getValueType());

    // entry is left on stack(0). Add to list
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/util/ArrayList", "add", "(Ljava/lang/Object;)Z");
    methodVisitor.visitInsn(POP);

    methodVisitor.visitIincInsn(loopCounterLocal, 1);
    methodVisitor.visitJumpInsn(GOTO, loop);

    methodVisitor.visitLabel(done);
    methodVisitor.visitVarInsn(ALOAD, context.getSlot("protocol"));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "com/facebook/thrift/protocol/TProtocol", "readListEnd",
            "()V");

    context.release(tlistSizeLocal);
    context.release(loopCounterLocal);
}

From source file:mt.swift.Deserializer.java

License:Apache License

private void generateReadSet(MethodVisitor methodVisitor, FrameRegisterManager context, SetType type) {
    // protocol.readListBegin()
    int tsetSizeLocal = context.newAnonymousSlot();
    int loopCounterLocal = context.newAnonymousSlot();

    methodVisitor.visitVarInsn(ALOAD, context.getSlot("protocol"));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "com/facebook/thrift/protocol/TProtocol", "readSetBegin",
            "()Lcom/facebook/thrift/protocol/TSet;");
    methodVisitor.visitFieldInsn(GETFIELD, "com/facebook/thrift/protocol/TSet", "size", "I");
    methodVisitor.visitVarInsn(ISTORE, tsetSizeLocal);

    // result = new ArrayList(tlist.size)
    methodVisitor.visitTypeInsn(NEW, "java/util/HashSet");
    methodVisitor.visitInsn(DUP);/*from w w  w.  j  a va2 s.com*/
    methodVisitor.visitVarInsn(ILOAD, tsetSizeLocal);
    methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "(I)V");

    // i = 0
    methodVisitor.visitInsn(ICONST_0);
    methodVisitor.visitVarInsn(ISTORE, loopCounterLocal); // #4 = loop counter

    Label done = new Label();
    Label loop = new Label();
    methodVisitor.visitLabel(loop);
    methodVisitor.visitVarInsn(ILOAD, loopCounterLocal);
    methodVisitor.visitVarInsn(ILOAD, tsetSizeLocal);
    methodVisitor.visitJumpInsn(IF_ICMPGE, done);

    methodVisitor.visitInsn(DUP); // ArrayList

    generateReadElement(methodVisitor, context, type.getValueType());
    generateConvertToObject(methodVisitor, type.getValueType());

    // entry is left on stack(0). Add to list
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashSet", "add", "(Ljava/lang/Object;)Z");
    methodVisitor.visitInsn(POP);

    methodVisitor.visitIincInsn(loopCounterLocal, 1);
    methodVisitor.visitJumpInsn(GOTO, loop);

    methodVisitor.visitLabel(done);
    methodVisitor.visitVarInsn(ALOAD, context.getSlot("protocol"));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "com/facebook/thrift/protocol/TProtocol", "readSetEnd", "()V");

    context.release(tsetSizeLocal);
    context.release(loopCounterLocal);
}

From source file:mt.swift.Deserializer.java

License:Apache License

private void generateReadMap(MethodVisitor methodVisitor, FrameRegisterManager context, MapType type) {
    // protocol.readListBegin()
    int tmapSizeLocal = context.newAnonymousSlot();
    int loopCounterLocal = context.newAnonymousSlot();

    methodVisitor.visitVarInsn(ALOAD, context.getSlot("protocol"));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "com/facebook/thrift/protocol/TProtocol", "readMapBegin",
            "()Lcom/facebook/thrift/protocol/TMap;");
    methodVisitor.visitFieldInsn(GETFIELD, "com/facebook/thrift/protocol/TMap", "size", "I");
    methodVisitor.visitVarInsn(ISTORE, tmapSizeLocal);

    // result = new ArrayList(tlist.size)
    methodVisitor.visitTypeInsn(NEW, "java/util/LinkedHashMap");
    methodVisitor.visitInsn(DUP);//from  w w w. j  a v  a 2s  .c  o m
    methodVisitor.visitVarInsn(ILOAD, tmapSizeLocal);
    methodVisitor.visitInsn(ICONST_2); // allocate 2 * tmap.size to avoid rehashing while building map
    methodVisitor.visitInsn(IMUL);
    methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/util/LinkedHashMap", "<init>", "(I)V");

    // i = 0
    methodVisitor.visitInsn(ICONST_0);
    methodVisitor.visitVarInsn(ISTORE, loopCounterLocal); // #4 = loop counter

    Label done = new Label();
    Label loop = new Label();
    methodVisitor.visitLabel(loop);
    methodVisitor.visitVarInsn(ILOAD, loopCounterLocal);
    methodVisitor.visitVarInsn(ILOAD, tmapSizeLocal);
    methodVisitor.visitJumpInsn(IF_ICMPGE, done);

    methodVisitor.visitInsn(DUP); // Map

    generateReadElement(methodVisitor, context, type.getKeyType());
    generateConvertToObject(methodVisitor, type.getKeyType());
    generateReadElement(methodVisitor, context, type.getValueType());
    generateConvertToObject(methodVisitor, type.getValueType());

    // entry is left on stack(0). Add to list
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/util/LinkedHashMap", "put",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
    methodVisitor.visitInsn(POP);

    methodVisitor.visitIincInsn(loopCounterLocal, 1);
    methodVisitor.visitJumpInsn(GOTO, loop);

    methodVisitor.visitLabel(done);
    methodVisitor.visitVarInsn(ALOAD, context.getSlot("protocol"));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "com/facebook/thrift/protocol/TProtocol", "readMapEnd", "()V");

    context.release(tmapSizeLocal);
    context.release(loopCounterLocal);
}

From source file:net.sourceforge.cobertura.instrument.pass3.AtomicArrayCodeProvider.java

License:GNU General Public License

/**
 * <pre>/*from  www  .  j a v a 2s  .  c  o  m*/
 * int[] __cobertura_get_and_reset_counters() {
 * int[] res = new int[counters.length()];
 * for(int i=0; i<counters.length(); i++){
 * res[i]=counters.getAndSet(i, 0);
 * }
 * return res;
 * }
 * </pre>
 */
public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()[I", null, null);

    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length",
            "()I");
    mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, 1);
    Label l3 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l3);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "getAndSet",
            "(II)I");
    mv.visitInsn(Opcodes.IASTORE);
    mv.visitIincInsn(1, 1);
    mv.visitLabel(l3);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length",
            "()I");
    mv.visitJumpInsn(Opcodes.IF_ICMPLT, l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.TestUnitCodeProvider.java

License:GNU General Public License

/**
 * Generates://from w  ww  . j a  va2 s  .  c  o m
 *   public static void __cobertura_init()
 *   {
 *     if (__cobertura_counters == null)
 *       {
 *         __cobertura_counters = new TestUnitInformationHolder[class.length];
 *       }
 *       TouchCollector.registerClass("mypackage/HelloWorld");
 *   }
 */
public void generateCINITmethod(MethodVisitor mv, String className, int counters_cnt) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
    mv.visitLdcInsn(counters_cnt);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(TestUnitInformationHolder.class));
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);

    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 1);
    Label l11 = new Label();
    mv.visitLabel(l11);
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLineNumber(24, l3);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
    mv.visitFieldInsn(GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitTypeInsn(NEW, "net/sourceforge/cobertura/coveragedata/TestUnitInformationHolder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "net/sourceforge/cobertura/coveragedata/TestUnitInformationHolder",
            "<init>", "()V");
    mv.visitInsn(AASTORE);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLineNumber(23, l4);
    mv.visitIincInsn(1, 1);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitFieldInsn(GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitInsn(ARRAYLENGTH);
    mv.visitJumpInsn(IF_ICMPLT, l3);

    mv.visitLabel(l1);

    generateRegisterClass(mv, className);
}

From source file:org.apache.directmemory.lightning.internal.generator.BytecodeMarshallerGenerator.java

License:Apache License

private void visitObjectArrayPropertyAccessorRead(MethodVisitor mv, String className,
        PropertyDescriptor propertyDescriptor) {
    Class<?> propertyType = propertyDescriptor.getType();

    // Load this to method stack
    mv.visitVarInsn(ALOAD, 0);// ww  w  . ja  va2  s  .co m

    // Read PropertyAccessor from field
    mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("accessor", propertyDescriptor),
            PROPERTYACCESSOR_CLASS_DESCRIPTOR);
    mv.visitTypeInsn(CHECKCAST, ARRAYPROPERTYACCESSOR_CLASS_INTERNAL_TYPE);
    mv.visitVarInsn(ASTORE, 8);

    // Load property type
    mv.visitVarInsn(ALOAD, 8);
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKEINTERFACE, PROPERTYACCESSOR_CLASS_INTERNAL_TYPE, "getType",
            OBJECT_GET_CLASS_SIGNATURE);
    mv.visitVarInsn(ASTORE, 5);

    // Load value to method stack
    mv.visitVarInsn(ALOAD, 1);

    // Save array to stack position 6
    visitPropertyAccessorValueRead(propertyType, mv);
    mv.visitTypeInsn(CHECKCAST, Type.getType(propertyType).getInternalName());
    mv.visitVarInsn(ASTORE, 6);

    // Save length to stream
    mv.visitVarInsn(ALOAD, 3);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitInsn(ARRAYLENGTH);
    mv.visitMethodInsn(INVOKEINTERFACE, DATAOUTPUT_CLASS_INTERNAL_TYPE, "writeInt", "(I)V");

    // Loop over every element in array
    Label forLoopEnd = new Label();
    Label forLoopStart = new Label();
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 7);
    mv.visitJumpInsn(GOTO, forLoopEnd);
    mv.visitLabel(forLoopStart);

    // Load this to method stack
    mv.visitVarInsn(ALOAD, 0);

    // Load property marshaller on stack
    mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("marshaller", propertyDescriptor),
            MARSHALLER_CLASS_DESCRIPTOR);

    // Load PropertyAccessor to method stack
    mv.visitVarInsn(ALOAD, 8);

    // Load array to method stack
    mv.visitVarInsn(ALOAD, 6);

    // Load index to method stack
    mv.visitVarInsn(ILOAD, 7);

    // Get value from array
    mv.visitMethodInsn(INVOKEINTERFACE, ARRAYPROPERTYACCESSOR_CLASS_INTERNAL_TYPE, "readObject",
            PROPERTY_ACCESSOR_ARRAY_READ_OBJECT_SIGNATURE);
    mv.visitTypeInsn(CHECKCAST, Type.getType(propertyType.getComponentType()).getInternalName());

    // If type is primitive add some "autoboxing" magic
    if (propertyType.getComponentType().isPrimitive()) {
        visitWrapperAutoboxing(propertyType.getComponentType(), mv);
    }

    // Load type to method stack
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("component", propertyDescriptor),
            CHEATINGPROPERTYDESCRIPTOR_CLASS_DESCRIPTOR);

    // Load DataOutput to method stack
    mv.visitVarInsn(ALOAD, 3);

    // Load SerializationContext to method stack
    mv.visitVarInsn(ALOAD, 4);

    // Call Marshaller#marshall on properties marshaller
    mv.visitMethodInsn(INVOKEINTERFACE, MARSHALLER_CLASS_INTERNAL_TYPE, "marshall",
            MARSHALLER_MARSHALL_SIGNATURE);

    // Test if loop ends
    mv.visitIincInsn(7, 1);
    mv.visitLabel(forLoopEnd);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitInsn(ARRAYLENGTH);
    mv.visitJumpInsn(IF_ICMPLT, forLoopStart);
}

From source file:org.apache.directmemory.lightning.internal.generator.BytecodeMarshallerGenerator.java

License:Apache License

private void visitObjectArrayPropertyAccessorWrite(MethodVisitor mv, String className,
        PropertyDescriptor propertyDescriptor) {
    Class<?> propertyType = propertyDescriptor.getType();
    Class<?> componentType = propertyType.getComponentType();

    // Read size//from  w ww .  j a v  a  2s  . com
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEINTERFACE, DATAINPUT_CLASS_INTERNAL_TYPE, "readInt", "()I");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ISTORE, 5);

    // Instantiate array
    mv.visitTypeInsn(ANEWARRAY, Type.getType(componentType).getInternalName());
    mv.visitVarInsn(ASTORE, 6);

    // Read PropertyAccessor from field
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("accessor", propertyDescriptor),
            PROPERTYACCESSOR_CLASS_DESCRIPTOR);
    mv.visitVarInsn(ASTORE, 9);

    // Get component type
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("component", propertyDescriptor),
            CHEATINGPROPERTYDESCRIPTOR_CLASS_DESCRIPTOR);
    mv.visitVarInsn(ASTORE, 8);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("marshaller", propertyDescriptor),
            MARSHALLER_CLASS_DESCRIPTOR);
    mv.visitVarInsn(ASTORE, 10);

    // For loop
    Label forLoopStart = new Label();
    Label forLoopEnd = new Label();
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 7);
    mv.visitJumpInsn(GOTO, forLoopEnd);
    mv.visitLabel(forLoopStart);

    // Write value to array
    mv.visitVarInsn(ALOAD, 9);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitVarInsn(ILOAD, 7);

    // Read value from stream to 4th stack position
    mv.visitVarInsn(ALOAD, 10);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKEINTERFACE, MARSHALLER_CLASS_INTERNAL_TYPE, "unmarshall",
            MARSHALLER_BASE_UNMARSHALL_SIGNATURE);

    mv.visitMethodInsn(INVOKEINTERFACE, ARRAYPROPERTYACCESSOR_CLASS_INTERNAL_TYPE, "writeObject",
            PROPERTY_ACCESSOR_ARRAY_WRITE_OBJECT_SIGNATURE);

    // Increment counter
    mv.visitIincInsn(7, 1);

    // Test for loop end
    mv.visitLabel(forLoopEnd);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitVarInsn(ILOAD, 5);
    mv.visitJumpInsn(IF_ICMPLT, forLoopStart);

    // Write array to object
    mv.visitVarInsn(ALOAD, 9);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 6);
    visitPropertyAccessorValueWrite(propertyType.getComponentType(), mv);
}

From source file:org.apache.felix.ipojo.composite.service.provides.POJOWriter.java

License:Apache License

/**
 * Generate on method./*from   w  w w. ja  v  a  2 s .c o  m*/
 * @param cw : class writer
 * @param className : the current class name
 * @param method : the method to generate
 * @param sign : method signature to generate
 * @param delegator : the field on which delegate
 * @param handler : the handler (used to acess the logger)
 */
private static void generateMethod(ClassWriter cw, String className, MethodMetadata method, Method sign,
        FieldMetadata delegator, Handler handler) {
    String desc = Type.getMethodDescriptor(sign);
    String name = sign.getName();
    String[] exc = new String[sign.getExceptionTypes().length];
    for (int i = 0; i < sign.getExceptionTypes().length; i++) {
        exc[i] = Type.getType(sign.getExceptionTypes()[i]).getInternalName();
    }

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, name, desc, null, exc);

    if (delegator.isOptional()) {
        if (!delegator.isAggregate()) {
            generateOptionalCase(mv, delegator, className);
        }
        if (delegator.isAggregate() /*&& method.getPolicy() == MethodMetadata.ONE_POLICY*/) {
            generateOptionalAggregateCase(mv, delegator, className);
        }
    }

    if (delegator.isAggregate()) {
        if (method.getPolicy() == MethodMetadata.ONE_POLICY) {
            // Aggregate and One Policy
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, className, delegator.getName(),
                    "[L" + delegator.getSpecification().getName().replace('.', '/') + ";");
            mv.visitInsn(ICONST_0); // Use the first one
            mv.visitInsn(AALOAD);

            loadArgs(mv, ACC_PUBLIC, Type.getArgumentTypes(desc));

            // Invoke
            mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name,
                    desc);

            // Return
            mv.visitInsn(Type.getReturnType(desc).getOpcode(Opcodes.IRETURN));

        } else { // All policy
            if (Type.getReturnType(desc).getSort() != Type.VOID) {
                handler.error("All policy cannot be used on method which does not return void");
            }

            Type[] args = Type.getArgumentTypes(desc);
            int index = args.length + 1;

            // Init
            mv.visitInsn(ICONST_0);
            mv.visitVarInsn(ISTORE, index);
            Label l1b = new Label();
            mv.visitLabel(l1b);
            Label l2b = new Label();
            mv.visitJumpInsn(GOTO, l2b);

            // Loop
            Label l3b = new Label();
            mv.visitLabel(l3b);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, className, delegator.getName(),
                    "[L" + delegator.getSpecification().getName().replace('.', '/') + ";");
            mv.visitVarInsn(ILOAD, index);
            mv.visitInsn(AALOAD);

            loadArgs(mv, ACC_PUBLIC, Type.getArgumentTypes(desc));

            mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name,
                    desc);

            Label l4b = new Label();
            mv.visitLabel(l4b);
            mv.visitIincInsn(index, 1); // i++;

            // Condition
            mv.visitLabel(l2b);
            mv.visitVarInsn(ILOAD, index);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, className, delegator.getName(),
                    "[L" + delegator.getSpecification().getName().replace('.', '/') + ";");
            mv.visitInsn(ARRAYLENGTH);
            mv.visitJumpInsn(IF_ICMPLT, l3b);

            Label l5b = new Label();
            mv.visitLabel(l5b);
            mv.visitInsn(RETURN);
        }
    } else {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, className, delegator.getName(),
                "L" + delegator.getSpecification().getName().replace('.', '/') + ";");

        loadArgs(mv, ACC_PUBLIC, Type.getArgumentTypes(desc));

        // Invoke
        if (delegator.getSpecification().isInterface()) {
            mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name,
                    desc);
        } else {
            mv.visitMethodInsn(INVOKEVIRTUAL, delegator.getSpecification().getName().replace('.', '/'), name,
                    desc);
        }

        // Return
        mv.visitInsn(Type.getReturnType(desc).getOpcode(IRETURN));
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.ballerinalang.nativeimpl.jvm.methodvisitor.VisitIincInsn.java

License:Open Source License

public static void visitIincInsn(Strand strand, ObjectValue oMv, long variable, long amount) {
    MethodVisitor mv = ASMUtil.getRefArgumentNativeData(oMv);
    mv.visitIincInsn((int) variable, (int) amount);
}

From source file:org.codehaus.aspectwerkz.transform.inlining.compiler.AbstractJoinPointCompiler.java

License:Open Source License

/**
 * Creates invocations fo the before interceptors.
 *
 * @param cv/* w ww .j a  va 2s .  c  om*/
 * @param joinPointInstanceIndex
 * @param registerDepth
 */
private void createBeforeInterceptorInvocations(final MethodVisitor cv, final int joinPointInstanceIndex,
        final int registerDepth) {
    final int loopIndex = registerDepth + 1;
    cv.visitInsn(ICONST_0);
    cv.visitVarInsn(ISTORE, loopIndex);
    Label loopStartLabel = new Label();
    cv.visitLabel(loopStartLabel);
    cv.visitVarInsn(ILOAD, loopIndex);
    cv.visitVarInsn(ALOAD, joinPointInstanceIndex);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, NR_OF_BEFORE_INTERCEPTORS_FIELD_NAME, I);
    Label loopCheckCondLabel = new Label();
    cv.visitJumpInsn(IF_ICMPGE, loopCheckCondLabel);
    cv.visitVarInsn(ALOAD, joinPointInstanceIndex);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, BEFORE_INTERCEPTORS_FIELD_NAME,
            BEFORE_ADVICE_ARRAY_CLASS_SIGNATURE);
    cv.visitVarInsn(ILOAD, loopIndex);
    cv.visitInsn(AALOAD);
    cv.visitVarInsn(ALOAD, joinPointInstanceIndex);
    cv.visitMethodInsn(INVOKEINTERFACE, BEFORE_ADVICE_CLASS_NAME, INTERCEPT_INVOKE_METHOD_NAME,
            BEFORE_ADVICE_INVOKE_METHOD_SIGNATURE);
    cv.visitIincInsn(loopIndex, 1);
    cv.visitJumpInsn(GOTO, loopStartLabel);
    cv.visitLabel(loopCheckCondLabel);
}