Example usage for org.objectweb.asm MethodVisitor visitLineNumber

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

Introduction

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

Prototype

public void visitLineNumber(final int line, final Label start) 

Source Link

Document

Visits a line number declaration.

Usage

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

License:Open Source License

public static void visitLineNumber(Strand strand, ObjectValue oMv, long line, ObjectValue oLabel) {
    MethodVisitor mv = ASMUtil.getRefArgumentNativeData(oMv);
    Label label = ASMUtil.getRefArgumentNativeData(oLabel);
    mv.visitLineNumber((int) line, label);
}

From source file:org.codehaus.groovy.classgen.asm.StatementWriter.java

License:Apache License

public void writeBlockStatement(BlockStatement block) {
    writeStatementLabel(block);/*ww  w.  ja v  a 2  s . co  m*/

    int mark = controller.getOperandStack().getStackLength();
    CompileStack compileStack = controller.getCompileStack();
    compileStack.pushVariableScope(block.getVariableScope());
    List<Statement> statementList = block.getStatements();
    for (Statement statement : statementList) {
        statement.visit(controller.getAcg());
    }
    compileStack.pop();

    // GROOVY-7647
    if (block.getLastLineNumber() > 0 && !isMethodOrConstructorNonEmptyBlock(block) // GROOVY-9126
    ) {
        MethodVisitor mv = controller.getMethodVisitor();
        Label blockEnd = new Label();
        mv.visitLabel(blockEnd);
        mv.visitLineNumber(block.getLastLineNumber(), blockEnd);
    }

    controller.getOperandStack().popDownTo(mark);
}

From source file:org.codehaus.groovy.classgen.asm.StatementWriter.java

License:Apache License

public void writeTryCatchFinally(TryCatchStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitTryCatchFinally");
    writeStatementLabel(statement);/*from w  ww.ja  va 2 s.c  om*/

    MethodVisitor mv = controller.getMethodVisitor();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();

    Statement tryStatement = statement.getTryStatement();
    Statement finallyStatement = statement.getFinallyStatement();

    // start try block, label needed for exception table
    Label tryStart = new Label();
    mv.visitLabel(tryStart);
    BlockRecorder tryBlock = makeBlockRecorder(finallyStatement);
    tryBlock.startRange(tryStart);

    tryStatement.visit(controller.getAcg());

    // goto finally part
    Label finallyStart = new Label();
    mv.visitJumpInsn(GOTO, finallyStart);

    Label tryEnd = new Label();
    mv.visitLabel(tryEnd);
    tryBlock.closeRange(tryEnd);
    // pop for "makeBlockRecorder(finallyStatement)"
    controller.getCompileStack().pop();

    BlockRecorder catches = makeBlockRecorder(finallyStatement);
    for (CatchStatement catchStatement : statement.getCatchStatements()) {
        ClassNode exceptionType = catchStatement.getExceptionType();
        String exceptionTypeInternalName = BytecodeHelper.getClassInternalName(exceptionType);

        // start catch block, label needed for exception table
        Label catchStart = new Label();
        mv.visitLabel(catchStart);
        catches.startRange(catchStart);

        // create exception variable and store the exception
        Parameter exceptionVariable = catchStatement.getVariable();
        compileStack.pushState();
        compileStack.defineVariable(exceptionVariable, true);
        // handle catch body
        catchStatement.visit(controller.getAcg());
        // place holder to avoid problems with empty catch blocks
        mv.visitInsn(NOP);
        // pop for the variable
        controller.getCompileStack().pop();

        // end of catch
        Label catchEnd = new Label();
        mv.visitLabel(catchEnd);
        catches.closeRange(catchEnd);

        // goto finally start
        mv.visitJumpInsn(GOTO, finallyStart);
        compileStack.writeExceptionTable(tryBlock, catchStart, exceptionTypeInternalName);
    }

    // used to handle exceptions in catches and regularly visited finals
    Label catchAny = new Label();

    // add "catch any" block to exception table for try part we do this
    // after the exception blocks, because else this one would supersede
    // any of those otherwise
    compileStack.writeExceptionTable(tryBlock, catchAny, null);
    // same for the catch parts
    compileStack.writeExceptionTable(catches, catchAny, null);

    // pop for "makeBlockRecorder(catches)"
    compileStack.pop();

    // start finally
    mv.visitLabel(finallyStart);
    finallyStatement.visit(controller.getAcg());

    // goto after all-catching block
    Label skipCatchAll = new Label();
    mv.visitJumpInsn(GOTO, skipCatchAll);

    // start a block catching any Exception
    mv.visitLabel(catchAny);
    // store exception
    // TODO: maybe define a Throwable and use it here instead of Object
    operandStack.push(ClassHelper.OBJECT_TYPE);
    int anyExceptionIndex = compileStack.defineTemporaryVariable("exception", true);

    // GROOVY-9199
    controller.resetLineNumber();
    int line = finallyStatement.getLineNumber();
    if (line > 0)
        mv.visitLineNumber(line, catchAny);

    finallyStatement.visit(controller.getAcg());

    // load the exception and rethrow it
    mv.visitVarInsn(ALOAD, anyExceptionIndex);
    mv.visitInsn(ATHROW);

    mv.visitLabel(skipCatchAll);
    compileStack.removeVar(anyExceptionIndex);
}

From source file:org.codehaus.groovy.classgen.AsmClassGenerator.java

License:Apache License

public void onLineNumber(final ASTNode statement, final String message) {
    if (statement == null || statement instanceof BlockStatement)
        return;/*from www  . j a v  a2 s . c  om*/

    currentASTNode = statement;
    int line = statement.getLineNumber();
    if (line < 0 || (!ASM_DEBUG && line == controller.getLineNumber()))
        return;

    controller.setLineNumber(line);
    MethodVisitor mv = controller.getMethodVisitor();
    if (mv != null) {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitLineNumber(line, l);
    }
}

From source file:org.diorite.inject.impl.utils.AsmUtils.java

License:Open Source License

public static int printLineNumber(MethodVisitor mv, int lineNumber) {
    if (lineNumber == -1) {
        return -1;
    }//  w  w  w  . j ava 2 s .  com
    Label label = new Label();
    mv.visitLabel(label);
    mv.visitLineNumber(lineNumber, label);
    return lineNumber + 1;
}

From source file:org.eclipse.golo.compiler.JavaBytecodeUtils.java

License:Open Source License

static Label visitLine(GoloElement<?> element, MethodVisitor visitor) {
    Label label = new Label();
    visitor.visitLabel(label);//from w  w w.j  a  v a 2s.c  o  m
    if (element.hasPosition()) {
        visitor.visitLineNumber(element.positionInSourceCode().getStartLine(), label);
    }
    return label;
}

From source file:org.fabric3.implementation.bytecode.reflection.BytecodeConsumerInvokerFactory.java

License:Open Source License

private void writeTargetInvoke(Method method, String internalTargetName, ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "invoke",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS);
    mv.visitCode();//from   w  w  w  . j  ava 2 s. c o  m
    Label label1 = new Label();
    mv.visitLabel(label1);
    mv.visitLineNumber(9, label1);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, internalTargetName);

    if (method.getParameterTypes().length == 1) {
        // single argument method, load the parameter passes on to the stack
        Class<?> paramType = method.getParameterTypes()[0];
        mv.visitVarInsn(Opcodes.ALOAD, 2);

        writeParam(paramType, mv);

    } else if (method.getParameterTypes().length > 1) {
        // multi-argument method: cast the parameter to an object array and then load each element on the stack to be passed as params
        mv.visitVarInsn(Opcodes.ALOAD, 2);

        mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
        mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
        mv.visitVarInsn(Opcodes.ASTORE, 3);

        int pos = 0;
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        for (Class<?> paramType : method.getParameterTypes()) {
            mv.visitInsn(Opcodes.ICONST_0 + pos);
            mv.visitInsn(Opcodes.AALOAD);

            writeParam(paramType, mv);

            if (pos < method.getParameterTypes().length - 1) {
                mv.visitVarInsn(Opcodes.ALOAD, 3);
            }
            pos++;
        }
    }

    // invoke the instance
    String methodName = method.getName();
    String methodDescriptor = Type.getMethodDescriptor(method);

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internalTargetName, methodName, methodDescriptor);

    Class<?> returnType = method.getReturnType();
    writeReturn(returnType, mv);

    Label label2 = new Label();
    mv.visitLabel(label2);
    String descriptor = Type.getDescriptor(ServiceInvoker.class);

    mv.visitLocalVariable("this", descriptor, null, label1, label2, 0);
    mv.visitLocalVariable("instance", "Ljava/lang/Object;", null, label1, label2, 1);
    mv.visitLocalVariable("arg", "Ljava/lang/Object;", null, label1, label2, 2);
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:org.fabric3.implementation.bytecode.reflection.BytecodeHelper.java

License:Open Source License

/**
 * Creates a no-args constructor.//from  www .j av  a2  s  .  c om
 *
 * @param cw the class writer
 */
public static void writeConstructor(ClassWriter cw, Class<?> superType) {
    String descriptor = Type.getDescriptor(ServiceInvoker.class);

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label label = new Label();
    mv.visitLabel(label);
    mv.visitLineNumber(6, label);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superType), "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", descriptor, null, label, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.fabric3.monitor.impl.proxy.BytecodeMonitorProxyService.java

License:Open Source License

/**
 * Implements a monitor interface method.
 *
 * @param cw                       the class writer
 * @param index                    the method index
 * @param proxyClassNameInternal   the parameter signature
 * @param writeParametersSignature the parameter types
 *//*  ww w  . j av  a 2 s.co  m*/
private void generateMethod(ClassWriter cw, Method method, int index, String proxyClassNameInternal,
        String writeParametersSignature) {
    String methodSignature = Type.getMethodDescriptor(method);

    // calculate the position of local variables. Per the JVM spec, pos 0 is reserved for a reference to "this"
    Class<?>[] paramTypes = method.getParameterTypes();
    int numParams = paramTypes.length;

    int offset = calculateParameterSpace(paramTypes);

    // calculate position of local variables
    int varIndexPosition = offset + 1; // pos of the index variable used for looking up the DispatchInfo
    int varCurrentLevelPosition = varIndexPosition + 1;
    int varCurrentMessagePosition = varCurrentLevelPosition + 1;
    int varTimestampPosition = varCurrentMessagePosition + 1;
    int varDispatchInfoPosition = varCurrentMessagePosition + 1; // Note this is the same as varTimestampPos since there is an if

    int varEntryPosition = varTimestampPosition + 2; // Note +2
    int varArgsPosition = varTimestampPosition + 2; // Note +2 and the same as varEntryPosition since there is an if

    int varStartPosition = varEntryPosition + 1;
    int varBufferPosition = varStartPosition + 2;

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), methodSignature, null, null);

    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, null);
    Label l3 = new Label();
    mv.visitTryCatchBlock(l2, l3, l2, null);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLineNumber(62, l4);

    // set the index var used to lookup the DispatchInfo. The array of DispatchInfo objects correspond to the ordering of Methods in the proxy interface.
    pushInteger(index, mv);
    mv.visitVarInsn(ISTORE, varIndexPosition);

    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitLineNumber(65, l5);

    // lookup the DispatchInfo based on the index for the method
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "infos", "[L" + DISPATCH_INFO + ";");
    mv.visitVarInsn(ILOAD, varIndexPosition);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, varDispatchInfoPosition);
    Label l11 = new Label();
    mv.visitLabel(l11);
    mv.visitLineNumber(70, l11);
    mv.visitVarInsn(ALOAD, varDispatchInfoPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, DISPATCH_INFO, "getLevel", "()L" + MONITOR_LEVEL + ";");
    mv.visitVarInsn(ASTORE, varCurrentLevelPosition);
    Label l12 = new Label();
    mv.visitLabel(l12);
    mv.visitLineNumber(71, l12);
    mv.visitVarInsn(ALOAD, varDispatchInfoPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, DISPATCH_INFO, "getMessage", "()Ljava/lang/String;");
    mv.visitVarInsn(ASTORE, varCurrentMessagePosition);

    mv.visitVarInsn(ALOAD, varCurrentLevelPosition);
    Label l13 = new Label();
    mv.visitJumpInsn(IFNULL, l13);
    mv.visitVarInsn(ALOAD, varCurrentLevelPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_LEVEL, "intValue", "()I");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "monitorable",
            "Lorg/fabric3/api/host/monitor/Monitorable;");
    mv.visitMethodInsn(INVOKEINTERFACE, "org/fabric3/api/host/monitor/Monitorable", "getLevel",
            "()L" + MONITOR_LEVEL + ";");
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_LEVEL, "intValue", "()I");
    Label l14 = new Label();
    mv.visitJumpInsn(IF_ICMPGE, l14);
    mv.visitLabel(l13);
    mv.visitLineNumber(75, l13);
    mv.visitInsn(RETURN);
    mv.visitLabel(l14);
    mv.visitLineNumber(77, l14);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J");
    mv.visitVarInsn(LSTORE, varTimestampPosition);
    Label l15 = new Label();
    mv.visitLabel(l15);
    mv.visitLineNumber(78, l15);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "asyncEnabled", "Z");
    Label l16 = new Label();
    mv.visitJumpInsn(IFEQ, l16);
    Label l17 = new Label();
    mv.visitLabel(l17);
    mv.visitLineNumber(79, l17);
    mv.visitInsn(ACONST_NULL);
    mv.visitVarInsn(ASTORE, varArgsPosition);
    mv.visitLabel(l0);
    mv.visitLineNumber(81, l0);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J");
    mv.visitVarInsn(LSTORE, varStartPosition);
    Label l18 = new Label();
    mv.visitLabel(l18);
    mv.visitLineNumber(82, l18);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "router", "L" + DESTINATION_ROUTER + ";");
    mv.visitMethodInsn(INVOKEINTERFACE, DESTINATION_ROUTER, "get", "()L" + MONITOR_EVENT_ENTRY + ";");
    mv.visitVarInsn(ASTORE, varEntryPosition);
    Label l19 = new Label();
    mv.visitLabel(l19);
    mv.visitLineNumber(83, l19);
    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "destinationIndex", "I");
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "setDestinationIndex", "(I)V");
    Label l20 = new Label();
    mv.visitLabel(l20);
    mv.visitLineNumber(84, l20);
    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitVarInsn(LLOAD, varStartPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "setTimestampNanos", "(J)V");
    Label l21 = new Label();
    mv.visitLabel(l21);
    mv.visitLineNumber(85, l21);
    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "getBuffer",
            "()Lorg/fabric3/monitor/spi/buffer/ResizableByteBuffer;");
    mv.visitVarInsn(ASTORE, varBufferPosition);

    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitVarInsn(ALOAD, varCurrentMessagePosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "setTemplate", "(Ljava/lang/String;)V");

    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J");

    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "setEntryTimestamp", "(J)V");

    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitVarInsn(ALOAD, varCurrentLevelPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "setLevel", "(L" + MONITOR_LEVEL + ";)V");

    Label l22 = new Label();
    mv.visitLabel(l22);
    mv.visitLineNumber(87, l22);

    mv.visitVarInsn(ALOAD, 0);

    // Load the method arguments onto the stack. Note that we access the method arguments using i+1 since the 0 position is used by "this" (params begin
    // at 1).
    for (int i = 0; i < paramTypes.length; i++) {
        Class<?> paramType = paramTypes[i];
        if (paramType.isPrimitive()) {
            if (Integer.TYPE.equals(paramType)) {
                mv.visitVarInsn(ILOAD, i + 1);
            } else if (Long.TYPE.equals(paramType)) {
                mv.visitVarInsn(LLOAD, i + 1);
            } else if (Double.TYPE.equals(paramType)) {
                mv.visitVarInsn(DLOAD, i + 1);
            } else if (Boolean.TYPE.equals(paramType)) {
                mv.visitVarInsn(ILOAD, i + 1);
            } else if (Float.TYPE.equals(paramType)) {
                mv.visitVarInsn(FLOAD, i + 1);
            } else if (Short.TYPE.equals(paramType)) {
                mv.visitVarInsn(ILOAD, i + 1);
            } else if (Byte.TYPE.equals(paramType)) {
                mv.visitVarInsn(ILOAD, i + 1);
            } else if (Character.TYPE.equals(paramType)) {
                mv.visitVarInsn(ILOAD, i + 1);
            } else {
                throw new AssertionError("Unhandled type: " + paramType);
            }

        } else {
            mv.visitVarInsn(ALOAD, i + 1);
        }
    }

    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitMethodInsn(INVOKESPECIAL, proxyClassNameInternal, "writeParameters" + index,
            writeParametersSignature);

    Label l24 = new Label();
    mv.visitLabel(l24);
    mv.visitLineNumber(90, l24);

    mv.visitLabel(l1);
    mv.visitLineNumber(95, l1);
    mv.visitVarInsn(ALOAD, varEntryPosition);
    Label l27 = new Label();
    mv.visitJumpInsn(IFNULL, l27);
    Label l28 = new Label();
    mv.visitLabel(l28);
    mv.visitLineNumber(96, l28);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "router", "L" + DESTINATION_ROUTER + ";");
    mv.visitVarInsn(ALOAD, varEntryPosition);
    mv.visitMethodInsn(INVOKEINTERFACE, DESTINATION_ROUTER, "publish", "(L" + MONITOR_EVENT_ENTRY + ";)V");
    mv.visitJumpInsn(GOTO, l27);
    mv.visitLabel(l2);
    mv.visitLineNumber(95, l2);
    mv.visitVarInsn(ASTORE, 13);
    mv.visitLabel(l3);
    mv.visitVarInsn(ALOAD, varEntryPosition);
    Label l29 = new Label();
    mv.visitJumpInsn(IFNULL, l29);
    Label l30 = new Label();
    mv.visitLabel(l30);
    mv.visitLineNumber(96, l30);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "router", "L" + DESTINATION_ROUTER + ";");
    mv.visitVarInsn(ALOAD, varArgsPosition);
    mv.visitMethodInsn(INVOKEINTERFACE, DESTINATION_ROUTER, "publish", "(L" + MONITOR_EVENT_ENTRY + ";)V");
    mv.visitLabel(l29);
    mv.visitVarInsn(ALOAD, 13);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l27);
    mv.visitLineNumber(99, l27);
    Label l31 = new Label();
    mv.visitJumpInsn(GOTO, l31);
    mv.visitLabel(l16);
    mv.visitLineNumber(100, l16);
    pushInteger(numParams, mv);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitVarInsn(ASTORE, varArgsPosition);
    Label l32 = new Label();
    mv.visitLabel(l32);
    mv.visitLineNumber(101, l32);

    for (int i = 0; i < paramTypes.length; i++) {
        mv.visitVarInsn(ALOAD, varArgsPosition);
        pushInteger(i, mv);

        if (paramTypes[i].isPrimitive()) {
            // i+1 since that is the position of the method argument (position 0 is reserved for "this")
            if (Integer.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(ILOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
            } else if (Long.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(LLOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
            } else if (Double.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(DLOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
            } else if (Float.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(FLOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
            } else if (Boolean.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(ILOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(Z)Ljava/lang/Boolean;");
            } else if (Short.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(ILOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
            } else if (Byte.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(ILOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
            } else if (Character.TYPE.equals(paramTypes[i])) {
                mv.visitVarInsn(ILOAD, i + 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
            }
        } else {
            mv.visitVarInsn(ALOAD, i + 1); // i+1 since that is the position of the method argument (position 0 is reserved for "this")
        }
        mv.visitInsn(AASTORE);
    }

    Label l34 = new Label();
    mv.visitLabel(l34);
    mv.visitLineNumber(103, l34);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "router", "L" + DESTINATION_ROUTER + ";");
    mv.visitVarInsn(ALOAD, varCurrentLevelPosition);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "destinationIndex", "I");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "runtimeName", "Ljava/lang/String;");
    mv.visitVarInsn(LLOAD, varTimestampPosition);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, ABSTRACT_MONITOR_HANDLER, "source", "Ljava/lang/String;");
    mv.visitVarInsn(ALOAD, varCurrentMessagePosition);
    mv.visitVarInsn(ALOAD, varArgsPosition);
    mv.visitMethodInsn(INVOKEINTERFACE, DESTINATION_ROUTER, "send",
            "(Lorg/fabric3/api/annotation/monitor/MonitorLevel;ILjava/lang/String;JLjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V");
    mv.visitLabel(l31);
    mv.visitLineNumber(106, l31);
    mv.visitInsn(RETURN);

    Label methodEnd = new Label();
    mv.visitLabel(methodEnd);

    mv.visitLocalVariable("this", "Lorg/fabric3/monitor/impl/proxy/AbstractMonitorHandler;", null, l4,
            methodEnd, 0);

    // Load the method params as local variables. Note the index starts at 1 since 0 is reserved for "this".
    for (int i = 1; i <= numParams; i++) {
        Class<?> paramType = paramTypes[i - 1];
        if (String.class.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "Ljava/lang/String;", null, l4, methodEnd, i);
        } else if (Integer.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "I", null, l4, methodEnd, i);
        } else if (Long.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "J", null, l4, methodEnd, i);
        } else if (Double.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "D", null, l4, methodEnd, i);
        } else if (Boolean.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "Z", null, l4, methodEnd, i);
        } else if (Float.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "F", null, l4, methodEnd, i);
        } else if (Short.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "S", null, l4, methodEnd, i);
        } else if (Byte.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "B", null, l4, methodEnd, i);
        } else if (Character.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "C", null, l4, methodEnd, i);
        } else if (paramType.isPrimitive()) {
            throw new AssertionError("Unhandled type: " + paramType);
        } else {
            mv.visitLocalVariable("arg" + i, "Ljava/lang/Object;", null, l4, methodEnd, i);
        }

    }

    mv.visitLocalVariable("index", "I", null, l5, methodEnd, varIndexPosition);

    mv.visitLocalVariable("currentLevel", "L" + MONITOR_LEVEL + ";", null, l12, methodEnd,
            varCurrentLevelPosition);
    mv.visitLocalVariable("currentMessage", "Ljava/lang/String;", null, l12, methodEnd,
            varCurrentMessagePosition);
    mv.visitLocalVariable("timestamp", "J", null, l15, methodEnd, varTimestampPosition);

    mv.visitLocalVariable("info", "L" + DISPATCH_INFO + ";", null, l11, l12, varDispatchInfoPosition);

    mv.visitLocalVariable("entry", "L" + MONITOR_EVENT_ENTRY + ";", null, l0, l27, varEntryPosition);
    mv.visitLocalVariable("args", "[Ljava/lang/Object;", null, l32, l31, varArgsPosition);

    mv.visitLocalVariable("start", "J", null, l18, l1, varStartPosition);
    mv.visitLocalVariable("buffer", "Lorg/fabric3/monitor/spi/buffer/ResizableByteBuffer;", null, l22, l1,
            varBufferPosition);

    mv.visitMaxs(9, 14);
    mv.visitEnd();
}

From source file:org.fabric3.monitor.impl.proxy.BytecodeMonitorProxyService.java

License:Open Source License

/**
 * Creates the writeParameters method. The method signature will take the same arguments as the proxy interface method that it is to be invoked from.
 *
 * @param cw         the class writer/*from ww w.  j  a v a2s  . co  m*/
 * @param index      the method index
 * @param signature  the parameter signature
 * @param paramTypes the parameter types
 */
private void writeGenerateParametersMethod(ClassWriter cw, int index, String signature, Class<?>[] paramTypes) {
    int varMethodArgOffset = 1;

    int offset = calculateParameterSpace(paramTypes);

    int varEntryPosition = offset + 1;
    int varNumberArgsPosition = varEntryPosition + 1;
    int varParamEntryPosition = varNumberArgsPosition + 1;
    int varIPosition = varParamEntryPosition + 1;

    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "writeParameters" + index, signature, null, null);

    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(103, l0);

    // set the number of arguments for this method
    pushInteger(paramTypes.length, mv);
    mv.visitVarInsn(ISTORE, varNumberArgsPosition);

    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(104, l1);

    mv.visitVarInsn(ALOAD, varEntryPosition); //set the param entry limit
    mv.visitVarInsn(ILOAD, varNumberArgsPosition);
    mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "setLimit", "(I)V");

    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(112, l2);

    // Setup i variable for the for loop and then iterate until the number of arguments is reached
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, varIPosition);

    Label l3 = new Label();
    mv.visitLabel(l3);

    // jump if i (specified by the for loop) is greater than the number of arguments
    mv.visitVarInsn(ILOAD, varIPosition);
    mv.visitVarInsn(ILOAD, varNumberArgsPosition);
    Label l4 = new Label();
    mv.visitJumpInsn(IF_ICMPGE, l4);

    Label endIf = new Label();
    for (int i = 0; i < paramTypes.length; i++) {
        // load ring buffer entry
        mv.visitVarInsn(ALOAD, varEntryPosition);
        mv.visitMethodInsn(INVOKEVIRTUAL, MONITOR_EVENT_ENTRY, "getEntries", "()[L" + PARAM_ENTRY + ";");
        pushInteger(i, mv);
        mv.visitInsn(AALOAD);
        mv.visitVarInsn(ASTORE, varParamEntryPosition);
        mv.visitVarInsn(ALOAD, varParamEntryPosition);

        Class<?> paramType = paramTypes[i];
        if (Character.TYPE.equals(paramType)) {
            // load method parameter
            mv.visitVarInsn(ILOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setCharValue", "(C)V");
        } else if (Integer.TYPE.equals(paramType)) {
            mv.visitVarInsn(ILOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setIntValue", "(I)V");
        } else if (Long.TYPE.equals(paramType)) {
            mv.visitVarInsn(LLOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setLongValue", "(J)V");
        } else if (Double.TYPE.equals(paramType)) {
            mv.visitVarInsn(DLOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setDoubleValue", "(D)V");
        } else if (Boolean.TYPE.equals(paramType)) {
            mv.visitVarInsn(ILOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setBooleanValue", "(Z)V");
        } else if (Float.TYPE.equals(paramType)) {
            mv.visitVarInsn(FLOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setFloatValue", "(F)V");
        } else if (Short.TYPE.equals(paramType)) {
            mv.visitVarInsn(ILOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setShortValue", "(S)V");
        } else if (Byte.TYPE.equals(paramType)) {
            mv.visitVarInsn(ILOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setByteValue", "(B)V");
        } else if (Object.class.isAssignableFrom(paramType)) {
            mv.visitVarInsn(ALOAD, varMethodArgOffset + i);
            mv.visitMethodInsn(INVOKEVIRTUAL, PARAM_ENTRY, "setObjectValue", "(Ljava/lang/Object;)V");
        } else {
            throw new AssertionError("Unhandled type: " + paramType);
        }
    }
    mv.visitLabel(endIf);
    mv.visitLineNumber(121, endIf);

    // increment i and counter, then loop
    mv.visitIincInsn(varIPosition, 1);
    mv.visitJumpInsn(GOTO, l3);
    // end of for loop

    mv.visitLabel(l4);

    mv.visitInsn(RETURN);
    Label endMethod = new Label();
    mv.visitLabel(endMethod);

    mv.visitLocalVariable("this", "L" + ABSTRACT_MONITOR_HANDLER + ";", null, l0, endMethod, 0);

    for (int i = 1; i <= paramTypes.length; i++) {
        Class<?> paramType = paramTypes[i - 1];
        if (Integer.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "I", null, l0, endMethod, i + 1);
        } else if (Long.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "J", null, l0, endMethod, i + 1);
        } else if (Double.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "D", null, l0, endMethod, i + 1);
        } else if (Boolean.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "Z", null, l0, endMethod, i + 1);
        } else if (Float.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "F", null, l0, endMethod, i + 1);
        } else if (Short.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "S", null, l0, endMethod, i + 1);
        } else if (Byte.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "B", null, l0, endMethod, i + 1);
        } else if (Character.TYPE.equals(paramType)) {
            mv.visitLocalVariable("arg" + i, "C", null, l0, endMethod, i + 1);
        } else if (paramType.isPrimitive()) {
            throw new AssertionError("Unhandled type");
        } else {
            mv.visitLocalVariable("arg" + i, "Ljava/lang/Object;", null, l0, endMethod, i + 1);
        }
    }

    mv.visitLocalVariable("entry", "L" + MONITOR_EVENT_ENTRY + ";", null, l0, endMethod, varEntryPosition);
    mv.visitLocalVariable("numberArgs", "I", null, l1, endMethod, varNumberArgsPosition);
    mv.visitLocalVariable("current", "L" + PARAM_ENTRY + ";", null, l2, endMethod, varParamEntryPosition);
    mv.visitLocalVariable("i", "I", null, l2, endMethod, varIPosition);

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

}