Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

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

Introduction

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

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddThreadNotificationAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String methodName, String desc, String signature,
        String[] exceptions) {/*from w  w  w  .j  a  v  a2s  .  c o  m*/
    if (INIT.equals(methodName)) {
        // into each constructor ...
        final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
        return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {
            @Override
            public void invokeConstructor(Type type, Method method) {
                super.invokeConstructor(type, method);
                // ... that contains a super(..) call (rather than this(..)):
                if (type.getInternalName().equals(clazz.getInternalSuperClassName())) {
                    // insert:
                    // this._OT$creationThread = Thread.currentThread();
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.THREAD_SLASH, CURRENT_THREAD,
                            CURRENT_THREAD_DESC, false);
                    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD,
                            THREAD_DESC);
                }
            }
        };
    } else if (RUN.equals(methodName) && RUN_DESC.equals(desc)) {
        final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
        return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {

            Label start = new Label(); // start of method (scope of new local)
            Label end = new Label(); // end of method
            int isThreadStartIdx; // new local: boolean _OT$isThreadStart

            @Override
            protected void onMethodEnter() {
                methodVisitor.visitLabel(start);
                isThreadStartIdx = newLocal(Type.BOOLEAN_TYPE);
                methodVisitor.visitLocalVariable("_OT$isThreadStart", "Z", null, start, end, isThreadStartIdx);
                // TeamThreadManager.newThreadStarted(false, this._OT$creationThread)
                methodVisitor.visitInsn(Opcodes.ICONST_0);
                methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                methodVisitor.visitFieldInsn(Opcodes.GETFIELD, clazz.getInternalName(), CREATION_THREAD,
                        THREAD_DESC);
                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH,
                        NEW_THREAD_STARTED, NEW_THREAD_STARTED_DESC, false);
                methodVisitor.visitIntInsn(Opcodes.ISTORE, isThreadStartIdx);
                // this._OT$creationThread = null; // avoid leak
                methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                methodVisitor.visitInsn(Opcodes.ACONST_NULL);
                methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD,
                        THREAD_DESC);
            }

            @Override
            protected void onMethodExit(int opcode) {
                insertThreadEndedNotification();
            }

            @Override
            public void endMethod() {
                methodVisitor.visitLabel(end);

                // insert another threadEnded notification as a handler for Throwable
                Label handler = new Label();
                methodVisitor.visitLabel(handler);
                insertThreadEndedNotification();
                methodVisitor.visitInsn(Opcodes.ATHROW); // rethrow caught exception

                methodVisitor.visitTryCatchBlock(start, end, handler, ClassNames.THROWABLE_SLASH);
                methodVisitor.visitMaxs(0, 0);
            }

            void insertThreadEndedNotification() {
                Label skip = new Label();
                // insert:
                // if (_OT$isThreadStart) TeamThreadManager.threadEnded();
                methodVisitor.visitIntInsn(Opcodes.ILOAD, isThreadStartIdx);
                methodVisitor.visitJumpInsn(Opcodes.IFEQ, skip);
                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH,
                        THREAD_ENDED, THREAD_ENDED_DESC, false);
                methodVisitor.visitLabel(skip);
            }
        };
    }
    return null;
}

From source file:org.eclipse.sisu.peaberry.internal.ImportGlue.java

License:Open Source License

@SuppressWarnings("PMD.ExcessiveMethodLength")
private static void wrap(final ClassWriter cw, final String proxyName, final Method method) {

    final String methodName = method.getName();

    final String descriptor = getMethodDescriptor(method);
    final String[] exceptions = getInternalNames(method.getExceptionTypes());

    // simple delegating proxy, so don't need synchronization on wrapper method
    final int modifiers = method.getModifiers() & ~(ABSTRACT | NATIVE | SYNCHRONIZED);

    final MethodVisitor v = cw.visitMethod(modifiers, methodName, descriptor, null, exceptions);

    final Label start = new Label();
    final Label invoke = new Label();
    final Label end = new Label();

    final Label ungetR = new Label();
    final Label finalR = new Label();
    final Label catchX = new Label();
    final Label ungetX = new Label();
    final Label finalX = new Label();

    v.visitCode();/*  w w w .j  a va  2  s.  com*/

    // support try{ get(); } finally { unget(); } model
    v.visitTryCatchBlock(start, ungetR, catchX, null);
    v.visitTryCatchBlock(ungetR, finalR, finalR, EXCEPTION_NAME);
    v.visitTryCatchBlock(ungetX, finalX, finalX, EXCEPTION_NAME);

    // store handle as "this"
    v.visitVarInsn(ALOAD, 0);
    v.visitFieldInsn(GETFIELD, proxyName, PROXY_HANDLE, IMPORT_DESC);
    v.visitInsn(DUP);
    v.visitVarInsn(ASTORE, 0);

    v.visitLabel(start);

    // dereference handle to get actual service instance
    v.visitMethodInsn(INVOKEINTERFACE, IMPORT_NAME, "get", "()" + OBJECT_DESC);
    v.visitInsn(DUP);

    // null => ServiceUnavailableException
    v.visitJumpInsn(IFNONNULL, invoke);
    v.visitTypeInsn(NEW, UNAVAILABLE_NAME);
    v.visitInsn(DUP);
    v.visitMethodInsn(INVOKESPECIAL, UNAVAILABLE_NAME, "<init>", "()V");
    v.visitInsn(ATHROW);

    v.visitLabel(invoke);

    final Class<?> clazz = method.getDeclaringClass();
    final String subjectName = getInternalName(clazz);

    if (!clazz.isInterface()) {
        v.visitTypeInsn(CHECKCAST, subjectName);
    }

    int i = 1;
    for (final Type t : getArgumentTypes(method)) {
        v.visitVarInsn(t.getOpcode(ILOAD), i);
        i = i + t.getSize();
    }

    // delegate to real method
    if (clazz.isInterface()) {
        v.visitMethodInsn(INVOKEINTERFACE, subjectName, methodName, descriptor);
    } else {
        v.visitMethodInsn(INVOKEVIRTUAL, subjectName, methodName, descriptor);
    }

    final Type returnType = getReturnType(method);

    if (VOID != returnType.getSort()) {
        v.visitVarInsn(returnType.getOpcode(ISTORE), 1);
    }

    v.visitLabel(ungetR);

    // unget on return
    v.visitVarInsn(ALOAD, 0);
    v.visitMethodInsn(INVOKEINTERFACE, IMPORT_NAME, "unget", VOID_DESC);
    v.visitInsn(ACONST_NULL);

    v.visitLabel(finalR);

    if (VOID != returnType.getSort()) {
        v.visitVarInsn(returnType.getOpcode(ILOAD), 1);
    }

    v.visitInsn(returnType.getOpcode(IRETURN));

    // cache initial exception
    v.visitLabel(catchX);
    v.visitVarInsn(ASTORE, 1);

    v.visitLabel(ungetX);

    // unget on exception
    v.visitVarInsn(ALOAD, 0);
    v.visitMethodInsn(INVOKEINTERFACE, IMPORT_NAME, "unget", VOID_DESC);
    v.visitInsn(ACONST_NULL);

    v.visitLabel(finalX);

    // restore initial exception
    v.visitVarInsn(ALOAD, 1);
    v.visitInsn(ATHROW);

    v.visitLabel(end);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:org.elasticsearch.plan.a.Adapter.java

License:Apache License

void checkWriteBranch(final MethodVisitor visitor, final ParserRuleContext source) {
    final Branch branch = getBranch(source);

    if (branch != null) {
        if (branch.tru != null) {
            visitor.visitJumpInsn(Opcodes.IFNE, branch.tru);
        } else if (branch.fals != null) {
            visitor.visitJumpInsn(Opcodes.IFEQ, branch.fals);
        }//from w  ww.  ja va2 s .c o m
    }
}

From source file:org.enerj.enhancer.ClassEnhancer.java

License:Open Source License

/**
 * Generate the getfield/putfield replacement methods (enerj_Get_* and enerj_Set_*)
 * for persistent fields./*w  ww  .j a  va 2 s  .  com*/
 * Parameters to the generated methods conveniently match the stack frame of
 * getfield and putfield.
 *
 * @param aField a Field for which the methods will be generated.
 */
private void emitPersistentFieldMediationMethods(Field aField) {
    int fieldScope = aField.getAccessModifiers() & (ACC_PRIVATE | ACC_PUBLIC | ACC_PROTECTED);
    String fieldName = aField.getName();
    String methodNameSuffix = getFieldMethodNameSuffix(mClassName, fieldName);
    String fieldType = aField.getDescriptor();
    int opcodeOffset = getOpcodeOffsetForDescriptor(fieldType);
    // TODO do we need to worry about fields with parameterized types? 
    //String genericSignature = Type.getDescriptor( aField.getGenericType() );

    MethodVisitor mv = cv.visitMethod(fieldScope | ACC_STATIC, FIELD_ACCESSOR_PREFIX + methodNameSuffix,
            '(' + mThisClassDescr + ')' + fieldType, null, null);
    Label label0 = new Label();
    mv.visitLabel(label0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, "enerj_mLoaded", "Z");
    Label label1 = new Label();
    mv.visitJumpInsn(IFNE, label1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, "enerj_mNew", "Z");
    mv.visitJumpInsn(IFNE, label1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_0);
    mv.visitMethodInsn(INVOKESTATIC, sPersistableHelperClassSlashed, "checkLoaded",
            "(Lorg/enerj/core/Persistable;Z)V");
    mv.visitLabel(label1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, fieldName, fieldType);
    mv.visitInsn(getReturnOpcodeForDescriptor(fieldType));
    Label label3 = new Label();
    mv.visitLabel(label3);
    mv.visitLocalVariable("anInstance", mThisClassDescr, null, label0, label3, 0);
    mv.visitMaxs(0, 0);

    ///--------------
    // Mutator (Setter). Because of the == comparison, the method is a little different
    // for primitives, Strings (final class that implements equals() properly), and regular objects.

    mv = cv.visitMethod(fieldScope | ACC_STATIC, FIELD_MUTATOR_PREFIX + methodNameSuffix,
            '(' + mThisClassDescr + fieldType + ")V", null, null);
    mv.visitCode();

    // Check if checkLoaded needs to be called. Call if (!enerj_mLoaded && !enerj_mNew)
    Label mutatorStartLabel = new Label();
    mv.visitLabel(mutatorStartLabel);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, "enerj_mLoaded", "Z");
    Label mutatorLabel1 = new Label();
    mv.visitJumpInsn(IFNE, mutatorLabel1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, "enerj_mNew", "Z");
    mv.visitJumpInsn(IFNE, mutatorLabel1);

    // Call checkLoaded
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_1);
    mv.visitMethodInsn(INVOKESTATIC, sPersistableHelperClassSlashed, "checkLoaded",
            "(Lorg/enerj/core/Persistable;Z)V");
    mv.visitLabel(mutatorLabel1);

    // Check if the field's value is actually changing. For primitives, we use ==;
    // for special classes declared final (e.g., String, Integer), we use equals(); 
    // and for other objects we use == (identity).

    Label notModifiedLabel = new Label();

    if (MetaData.isPrimitive(fieldType)) {
        // Push parameter 1 - the new value
        mv.visitVarInsn(ILOAD + opcodeOffset, 1);
        // This
        mv.visitVarInsn(ALOAD, 0);
        // Current value
        mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, fieldName, fieldType);

        char type = fieldType.charAt(0);
        switch (type) {
        case 'B':
        case 'Z':
        case 'C':
        case 'S':
        case 'I':
            mv.visitJumpInsn(IF_ICMPEQ, notModifiedLabel);
            break;

        case 'F':
            mv.visitInsn(FCMPL);
            mv.visitJumpInsn(IFEQ, notModifiedLabel);
            break;

        case 'J':
            mv.visitInsn(LCMP);
            mv.visitJumpInsn(IFEQ, notModifiedLabel);
            break;

        case 'D':
            mv.visitInsn(DCMPL);
            mv.visitJumpInsn(IFEQ, notModifiedLabel);
            break;

        default:
            throw new RuntimeException("Unknown primitive type: " + type);
        }
    } else if (fieldType.equals("Ljava/lang/String;") || fieldType.equals("Ljava/lang/Integer;")
            || fieldType.equals("Ljava/lang/Long;") || fieldType.equals("Ljava/lang/Byte;")
            || fieldType.equals("Ljava/lang/Boolean;") || fieldType.equals("Ljava/lang/Character;")
            || fieldType.equals("Ljava/lang/Short;") || fieldType.equals("Ljava/lang/Float;")
            || fieldType.equals("Ljava/lang/Double;")) {

        // One of the core final immutable types. Use equals() to compare values, like this:
        // "if ((aValue == null{1} && anInstance.mString != null{2}) || (aValue != null{3} && !aValue.equals(anInstance.mString){4})" ...

        // {1}: aValue == null
        mv.visitVarInsn(ALOAD, 1);
        Label imEqualsLabel = new Label();
        mv.visitJumpInsn(IFNONNULL, imEqualsLabel);

        // {2}: anInstance.mString != null
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, fieldName, fieldType);
        Label imStoreLabel = new Label();
        mv.visitJumpInsn(IFNONNULL, imStoreLabel);

        // {3}: aValue != null
        mv.visitLabel(imEqualsLabel);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitJumpInsn(IFNULL, notModifiedLabel);

        // {4}: equals()...
        // Push parameter 1 - the new value
        mv.visitVarInsn(ALOAD, 1);
        // Current Value
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, fieldName, fieldType);
        // Compare !equals()
        mv.visitMethodInsn(INVOKEVIRTUAL, aField.getInternalName(), "equals", "(Ljava/lang/Object;)Z");
        mv.visitJumpInsn(IFNE, notModifiedLabel);

        mv.visitLabel(imStoreLabel);
    } else {
        // Some other Object -- use identity ==
        // Push parameter 1 - the new value
        mv.visitVarInsn(ALOAD, 1);
        // This
        mv.visitVarInsn(ALOAD, 0);
        // Current value
        mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, fieldName, fieldType);
        mv.visitJumpInsn(IF_ACMPEQ, notModifiedLabel);
    }

    // Store the value
    // Mark owner object as modified - short circuit if already marked modified
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, mThisClassNameSlashed, "enerj_mModified", "Z");
    Label mutatorLabel2 = new Label();
    mv.visitJumpInsn(IFNE, mutatorLabel2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, sPersistableHelperClassSlashed, "addModified", sPersistableVoidSignature);
    mv.visitLabel(mutatorLabel2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ILOAD + opcodeOffset, 1);
    mv.visitFieldInsn(PUTFIELD, mThisClassNameSlashed, fieldName, fieldType);
    mv.visitLabel(notModifiedLabel);
    mv.visitInsn(RETURN);

    Label mutatorEndlabel = new Label();
    mv.visitLabel(mutatorEndlabel);
    mv.visitLocalVariable("anInstance", mThisClassDescr, null, mutatorStartLabel, mutatorEndlabel, 0);
    mv.visitLocalVariable("aValue", fieldType, null, mutatorStartLabel, mutatorEndlabel, 1);
    mv.visitMaxs(0, 0);
    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
 *///from  w  ww. java2 s  .com
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  .jav  a 2 s.c  om
 * @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();

}

From source file:org.glowroot.advicegen.AdviceGenerator.java

License:Apache License

@RequiresNonNull("methodMetaInternalName")
private void addOnBeforeMethod(ClassWriter cw) {
    MethodVisitor mv = visitOnBeforeMethod(cw, "Lorg/glowroot/api/TraceEntry;");
    mv.visitCode();//from www.j a v a 2  s .  c o  m
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "entryEnabled",
                "Lorg/glowroot/api/PluginServices$BooleanProperty;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/PluginServices$BooleanProperty", "value", "()Z",
                true);
        Label label = new Label();
        mv.visitJumpInsn(IFNE, label);
        // entryEnabled is false, collect timer only
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "pluginServices", "Lorg/glowroot/api/PluginServices;");
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "timerName", "Lorg/glowroot/api/TimerName;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/glowroot/api/PluginServices", "startTimer",
                "(Lorg/glowroot/api/TimerName;)Lorg/glowroot/api/Timer;", false);
        mv.visitInsn(ARETURN);
        mv.visitLabel(label);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    mv.visitFieldInsn(GETSTATIC, adviceInternalName, "pluginServices", "Lorg/glowroot/api/PluginServices;");
    if (config.isTransaction()) {
        String transactionType = config.transactionType();
        if (transactionType.isEmpty()) {
            mv.visitLdcInsn("<no transaction type provided>");
        } else {
            mv.visitLdcInsn(transactionType);
        }
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getTransactionNameTemplate",
                "()Lorg/glowroot/advicegen/MessageTemplate;", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/advicegen/GenericMessageSupplier", "create",
                "(Lorg/glowroot/advicegen/MessageTemplate;"
                        + "Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)"
                        + "Lorg/glowroot/advicegen/GenericMessageSupplier;",
                false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/glowroot/advicegen/GenericMessageSupplier", "getMessageText",
                "()Ljava/lang/String;", false);
    }
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getMessageTemplate",
            "()Lorg/glowroot/advicegen/MessageTemplate;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/advicegen/GenericMessageSupplier", "create",
            "(Lorg/glowroot/advicegen/MessageTemplate;"
                    + "Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)"
                    + "Lorg/glowroot/advicegen/GenericMessageSupplier;",
            false);
    mv.visitFieldInsn(GETSTATIC, adviceInternalName, "timerName", "Lorg/glowroot/api/TimerName;");
    if (config.isTransaction()) {
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/glowroot/api/PluginServices", "startTransaction",
                "(Ljava/lang/String;Ljava/lang/String;"
                        + "Lorg/glowroot/api/MessageSupplier;Lorg/glowroot/api/TimerName;)"
                        + "Lorg/glowroot/api/TraceEntry;",
                false);
    } else {
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/glowroot/api/PluginServices", "startTraceEntry",
                "(Lorg/glowroot/api/MessageSupplier;Lorg/glowroot/api/TimerName;)"
                        + "Lorg/glowroot/api/TraceEntry;",
                false);
    }
    addCodeForOptionalTraceAttributes(mv);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.advicegen.AdviceGenerator.java

License:Apache License

private void addOnReturnMethod(ClassWriter cw) {
    boolean entryOrTimer = !config.traceEntryEnabledProperty().isEmpty();
    String travelerType = entryOrTimer ? "Ljava/lang/Object;" : "Lorg/glowroot/api/TraceEntry;";
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "onReturn",
            "(Lorg/glowroot/api/OptionalReturn;" + travelerType + ")V", null, null);
    mv.visitParameterAnnotation(0, "Lorg/glowroot/api/weaving/BindOptionalReturn;", true).visitEnd();
    mv.visitParameterAnnotation(1, "Lorg/glowroot/api/weaving/BindTraveler;", true).visitEnd();
    int travelerParamIndex = 1;
    mv.visitAnnotation("Lorg/glowroot/api/weaving/OnReturn;", true).visitEnd();
    mv.visitCode();//  w w w .  j  a v  a  2  s.  c  o  m
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitVarInsn(ALOAD, travelerParamIndex);
        mv.visitTypeInsn(INSTANCEOF, "org/glowroot/api/Timer");
        Label label = new Label();
        mv.visitJumpInsn(IFEQ, label);
        mv.visitVarInsn(ALOAD, travelerParamIndex);
        mv.visitTypeInsn(CHECKCAST, "org/glowroot/api/Timer");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/Timer", "stop", "()V", true);
        mv.visitInsn(RETURN);
        mv.visitLabel(label);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/OptionalReturn", "isVoid", "()Z", true);
    Label notVoidLabel = new Label();
    Label endIfLabel = new Label();
    mv.visitJumpInsn(IFEQ, notVoidLabel);
    mv.visitLdcInsn("void");
    mv.visitJumpInsn(GOTO, endIfLabel);
    mv.visitLabel(notVoidLabel);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/OptionalReturn", "getValue", "()Ljava/lang/Object;",
            true);
    mv.visitLabel(endIfLabel);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/advicegen/GenericMessageSupplier", "updateWithReturnValue",
            "(Lorg/glowroot/api/TraceEntry;Ljava/lang/Object;)V", false);
    mv.visitVarInsn(ALOAD, travelerParamIndex);
    Long stackTraceThresholdMillis = config.traceEntryStackThresholdMillis();
    if (stackTraceThresholdMillis == null) {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/TraceEntry", "end", "()V", true);
    } else {
        mv.visitLdcInsn(stackTraceThresholdMillis);
        mv.visitFieldInsn(GETSTATIC, "java/util/concurrent/TimeUnit", "MILLISECONDS",
                "Ljava/util/concurrent/TimeUnit;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/TraceEntry", "endWithStackTrace",
                "(JLjava/util/concurrent/TimeUnit;)V", true);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.advicegen.AdviceGenerator.java

License:Apache License

private void addOnThrowMethod(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "onThrow",
            "(Ljava/lang/Throwable;Lorg/glowroot/api/TraceEntry;)V", null, null);
    mv.visitAnnotation("Lorg/glowroot/api/weaving/OnThrow;", true).visitEnd();
    mv.visitParameterAnnotation(0, "Lorg/glowroot/api/weaving/BindThrowable;", true).visitEnd();
    mv.visitParameterAnnotation(1, "Lorg/glowroot/api/weaving/BindTraveler;", true).visitEnd();
    mv.visitCode();/* ww  w. jav  a2s  . c o  m*/
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitVarInsn(ALOAD, 1);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitInsn(RETURN);
        mv.visitLabel(l0);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/api/ErrorMessage", "from",
            "(Ljava/lang/Throwable;)Lorg/glowroot/api/ErrorMessage;", false);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/api/TraceEntry", "endWithError",
            "(Lorg/glowroot/api/ErrorMessage;)V", true);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.agent.advicegen.AdviceGenerator.java

License:Apache License

@RequiresNonNull("methodMetaInternalName")
private void addOnBeforeMethod(ClassWriter cw) {
    MethodVisitor mv = visitOnBeforeMethod(cw, "Lorg/glowroot/agent/plugin/api/TraceEntry;");
    mv.visitCode();/*from ww  w  .  ja va 2  s.  com*/
    if (!config.traceEntryEnabledProperty().isEmpty() && pluginId != null) {
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "entryEnabled",
                "Lorg/glowroot/agent/plugin/api/config/BooleanProperty;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/config/BooleanProperty", "value",
                "()Z", true);
        Label label = new Label();
        mv.visitJumpInsn(IFNE, label);
        // entryEnabled is false, collect timer only
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "timerName",
                "Lorg/glowroot/agent/plugin/api/TimerName;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext", "startTimer",
                "(Lorg/glowroot/agent/plugin/api/TimerName;)" + "Lorg/glowroot/agent/plugin/api/Timer;", true);
        mv.visitInsn(ARETURN);
        mv.visitLabel(label);
    }
    mv.visitVarInsn(ALOAD, 0);
    if (config.isTransaction()) {
        String transactionType = config.transactionType();
        if (transactionType.isEmpty()) {
            mv.visitLdcInsn("<no transaction type provided>");
        } else {
            mv.visitLdcInsn(transactionType);
        }
        mv.visitVarInsn(ALOAD, 4);
        mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getTransactionNameTemplate",
                "()Lorg/glowroot/agent/advicegen/MessageTemplate;", false);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/GenericMessageSupplier", "create",
                "(Lorg/glowroot/agent/advicegen/MessageTemplate;"
                        + "Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)"
                        + "Lorg/glowroot/agent/advicegen/GenericMessageSupplier;",
                false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/glowroot/agent/advicegen/GenericMessageSupplier",
                "getMessageText", "()Ljava/lang/String;", false);
    }
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getMessageTemplate",
            "()Lorg/glowroot/agent/advicegen/MessageTemplate;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/GenericMessageSupplier", "create",
            "(Lorg/glowroot/agent/advicegen/MessageTemplate;"
                    + "Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)"
                    + "Lorg/glowroot/agent/advicegen/GenericMessageSupplier;",
            false);
    mv.visitFieldInsn(GETSTATIC, adviceInternalName, "timerName", "Lorg/glowroot/agent/plugin/api/TimerName;");
    if (config.isTransaction()) {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/OptionalThreadContext",
                "startTransaction",
                "(Ljava/lang/String;Ljava/lang/String;" + "Lorg/glowroot/agent/plugin/api/MessageSupplier;"
                        + "Lorg/glowroot/agent/plugin/api/TimerName;)"
                        + "Lorg/glowroot/agent/plugin/api/TraceEntry;",
                true);
    } else {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext", "startTraceEntry",
                "(Lorg/glowroot/agent/plugin/api/MessageSupplier;"
                        + "Lorg/glowroot/agent/plugin/api/TimerName;)"
                        + "Lorg/glowroot/agent/plugin/api/TraceEntry;",
                true);
    }
    addCodeForOptionalTransactionAttributes(mv);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}