Example usage for org.objectweb.asm.commons GeneratorAdapter loadArgs

List of usage examples for org.objectweb.asm.commons GeneratorAdapter loadArgs

Introduction

In this page you can find the example usage for org.objectweb.asm.commons GeneratorAdapter loadArgs.

Prototype

public void loadArgs(final int arg, final int count) 

Source Link

Document

Generates the instructions to load the given method arguments on the stack.

Usage

From source file:org.glowroot.agent.weaving.PointcutClassVisitor.java

License:Apache License

@Override
public void visitEnd() {
    if (onBeforeMethodVisitor != null) {
        int access = onBeforeMethodVisitor.access;
        String name = onBeforeMethodVisitor.name;
        String descriptor = "(Z" + onBeforeMethodVisitor.descriptor.substring(1);
        String signature = onBeforeMethodVisitor.signature;
        String[] exceptions = onBeforeMethodVisitor.exceptions;
        GeneratorAdapter mv = new GeneratorAdapter(
                cw.visitMethod(access, name, descriptor, signature, exceptions), access, name, descriptor);
        mv.visitCode();/*from w w w  .java 2 s .c o  m*/
        mv.visitVarInsn(ILOAD, 0);
        Label endWithDefaultLabel = new Label();
        mv.visitJumpInsn(IFEQ, endWithDefaultLabel);
        mv.loadArgs(1, mv.getArgumentTypes().length - 1);
        mv.visitMethodInsn(INVOKESTATIC, checkNotNull(className), name, onBeforeMethodVisitor.descriptor,
                false);
        mv.returnValue();

        mv.visitLabel(endWithDefaultLabel);
        mv.visitFrame(F_NEW, 0, new Object[0], 0, new Object[0]);
        if (mv.getReturnType().getSort() != Type.VOID) {
            // return value will be ignored when !enabled, but need to return something
            WeavingMethodVisitor.pushDefault(mv, mv.getReturnType());
        }
        mv.returnValue();
        mv.endMethod();
    }
    super.visitEnd();
}

From source file:org.jruby.javasupport.proxy.JavaProxyClassFactory.java

License:LGPL

private static Class[] generateConstructor(Type selfType, Constructor constructor, ClassVisitor cw) {

    Class[] superConstructorParameterTypes = constructor.getParameterTypes();
    Class[] newConstructorParameterTypes = new Class[superConstructorParameterTypes.length + 1];
    System.arraycopy(superConstructorParameterTypes, 0, newConstructorParameterTypes, 0,
            superConstructorParameterTypes.length);
    newConstructorParameterTypes[superConstructorParameterTypes.length] = JavaProxyInvocationHandler.class;

    int access = Opcodes.ACC_PUBLIC;
    String name1 = "<init>";
    String signature = null;/*from w w  w .  jav  a 2 s  . c om*/
    Class[] superConstructorExceptions = constructor.getExceptionTypes();

    org.objectweb.asm.commons.Method super_m = new org.objectweb.asm.commons.Method(name1, Type.VOID_TYPE,
            toType(superConstructorParameterTypes));
    org.objectweb.asm.commons.Method m = new org.objectweb.asm.commons.Method(name1, Type.VOID_TYPE,
            toType(newConstructorParameterTypes));

    GeneratorAdapter ga = new GeneratorAdapter(access, m, signature, toType(superConstructorExceptions), cw);

    ga.loadThis();
    ga.loadArgs(0, superConstructorParameterTypes.length);
    ga.invokeConstructor(Type.getType(constructor.getDeclaringClass()), super_m);

    ga.loadThis();
    ga.loadArg(superConstructorParameterTypes.length);
    ga.putField(selfType, INVOCATION_HANDLER_FIELD_NAME, INVOCATION_HANDLER_TYPE);

    // do a void return
    ga.returnValue();
    ga.endMethod();
    return newConstructorParameterTypes;
}