Example usage for org.objectweb.asm MethodVisitor visitLabel

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

Introduction

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

Prototype

public void visitLabel(final Label label) 

Source Link

Document

Visits a label.

Usage

From source file:org.codehaus.aspectwerkz.cflow.CflowCompiler.java

License:Open Source License

/**
 * compile the jit cflow aspect//from  w w  w  . j a  v  a2 s.c om
 * @return bytecode for the concrete jit cflow aspect
 */
private byte[] compile() {
    m_cw = AsmHelper.newClassWriter(true);

    // class extends AbstractCflowsystemAspect
    m_cw.visit(AsmHelper.JAVA_VERSION, ACC_PUBLIC + ACC_SUPER + ACC_SYNTHETIC, m_className, null,
            ABSTRACT_CFLOW_CLASS, EMPTY_STRING_ARRAY);

    // static INSTANCE field
    m_cw.visitField(ACC_PRIVATE + ACC_STATIC, INSTANCE_CFLOW_FIELD_NAME, m_classSignature, null, null);

    // private ctor
    MethodVisitor ctor = m_cw.visitMethod(ACC_PRIVATE, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE, null,
            EMPTY_STRING_ARRAY);
    // invoke the constructor of abstract
    ctor.visitVarInsn(ALOAD, 0);
    ctor.visitMethodInsn(INVOKESPECIAL, ABSTRACT_CFLOW_CLASS, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
    ctor.visitInsn(RETURN);
    ctor.visitMaxs(0, 0);

    // static isInCflow() delegators
    MethodVisitor isInCflow = m_cw.visitMethod(ACC_PUBLIC + ACC_STATIC, IS_IN_CFLOW_METOD_NAME,
            IS_IN_CFLOW_METOD_SIGNATURE, null, EMPTY_STRING_ARRAY);
    isInCflow.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, m_classSignature);
    Label isNull = new Label();
    isInCflow.visitJumpInsn(IFNULL, isNull);
    isInCflow.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, m_classSignature);
    isInCflow.visitMethodInsn(INVOKEVIRTUAL, ABSTRACT_CFLOW_CLASS, IN_CFLOW_METOD_NAME,
            IN_CFLOW_METOD_SIGNATURE);
    isInCflow.visitInsn(IRETURN);
    isInCflow.visitLabel(isNull);
    isInCflow.visitInsn(ICONST_0);
    isInCflow.visitInsn(IRETURN);
    isInCflow.visitMaxs(0, 0);

    // static aspectOf()
    MethodVisitor aspectOf = m_cw.visitMethod(ACC_PUBLIC + ACC_STATIC, CFLOW_ASPECTOF_METHOD_NAME,
            "()" + m_classSignature, null, EMPTY_STRING_ARRAY);
    aspectOf.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, m_classSignature);
    Label isNotNull = new Label();
    aspectOf.visitJumpInsn(IFNONNULL, isNotNull);
    aspectOf.visitTypeInsn(NEW, m_className);
    aspectOf.visitInsn(DUP);
    aspectOf.visitMethodInsn(INVOKESPECIAL, m_className, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
    aspectOf.visitFieldInsn(PUTSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, m_classSignature);
    aspectOf.visitLabel(isNotNull);
    aspectOf.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, m_classSignature);
    aspectOf.visitInsn(ARETURN);
    aspectOf.visitMaxs(0, 0);

    m_cw.visitEnd();

    return m_cw.toByteArray();
}

From source file:org.codehaus.aspectwerkz.transform.inlining.AsmHelper.java

License:Open Source License

/**
 * Handles the unwrapping of a type, unboxing of primitives and casting to the correct object type.
 * Takes care of null value replaced by default primitive value.
 * <pre>(obj==null)?0L:((Long)obj).longValue();</pre>
 *
 * @param cv//from   w w  w  . ja v a 2s . c om
 * @param type
 */
public static void unwrapType(final MethodVisitor cv, final Type type) {
    // void, object and array type handling
    switch (type.getSort()) {
    case Type.OBJECT:
        String objectTypeName = type.getClassName().replace('.', '/');
        cv.visitTypeInsn(CHECKCAST, objectTypeName);
        return;
    case Type.ARRAY:
        cv.visitTypeInsn(CHECKCAST, type.getDescriptor());
        return;
    case Type.VOID:
        return;
    }
    // primitive type handling
    Label l0If = new Label();
    Label l1End = new Label();
    // if != null
    cv.visitInsn(DUP);
    cv.visitJumpInsn(IFNONNULL, l0If);
    // else, default value
    cv.visitInsn(POP);
    addDefaultValue(cv, type);
    // end
    cv.visitJumpInsn(GOTO, l1End);
    // if body
    cv.visitLabel(l0If);
    switch (type.getSort()) {
    case Type.SHORT:
        cv.visitTypeInsn(CHECKCAST, SHORT_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, SHORT_CLASS_NAME, SHORT_VALUE_METHOD_NAME,
                SHORT_VALUE_METHOD_SIGNATURE);
        break;
    case Type.INT:
        cv.visitTypeInsn(CHECKCAST, INTEGER_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, INTEGER_CLASS_NAME, INT_VALUE_METHOD_NAME,
                INT_VALUE_METHOD_SIGNATURE);
        break;
    case Type.LONG:
        cv.visitTypeInsn(CHECKCAST, LONG_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, LONG_CLASS_NAME, LONG_VALUE_METHOD_NAME, LONG_VALUE_METHOD_SIGNATURE);
        break;
    case Type.FLOAT:
        cv.visitTypeInsn(CHECKCAST, FLOAT_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, FLOAT_CLASS_NAME, FLOAT_VALUE_METHOD_NAME,
                FLOAT_VALUE_METHOD_SIGNATURE);
        break;
    case Type.DOUBLE:
        cv.visitTypeInsn(CHECKCAST, DOUBLE_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, DOUBLE_CLASS_NAME, DOUBLE_VALUE_METHOD_NAME,
                DOUBLE_VALUE_METHOD_SIGNATURE);
        break;
    case Type.BYTE:
        cv.visitTypeInsn(CHECKCAST, BYTE_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, BYTE_CLASS_NAME, BYTE_VALUE_METHOD_NAME, BYTE_VALUE_METHOD_SIGNATURE);
        break;
    case Type.BOOLEAN:
        cv.visitTypeInsn(CHECKCAST, BOOLEAN_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, BOOLEAN_CLASS_NAME, BOOLEAN_VALUE_METHOD_NAME,
                BOOLEAN_VALUE_METHOD_SIGNATURE);
        break;
    case Type.CHAR:
        cv.visitTypeInsn(CHECKCAST, CHARACTER_CLASS_NAME);
        cv.visitMethodInsn(INVOKEVIRTUAL, CHARACTER_CLASS_NAME, CHAR_VALUE_METHOD_NAME,
                CHAR_VALUE_METHOD_SIGNATURE);
        break;
    }
    cv.visitLabel(l1End);
}

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

License:Open Source License

/**
 * Creates the static initialization method (not clinit) for the join point.
 *///from w  ww.jav a2  s .c  om
private void createStaticInitializer() {
    MethodVisitor cv = m_cw.visitMethod(ACC_STATIC | ACC_PUBLIC, STATIC_INITIALIZATION_METHOD_NAME,
            NO_PARAM_RETURN_VOID_SIGNATURE, null, null);

    Label tryLabel = new Label();
    cv.visitLabel(tryLabel);
    cv.visitLdcInsn(m_calleeClassName.replace('/', '.'));
    cv.visitMethodInsn(INVOKESTATIC, CLASS_CLASS, FOR_NAME_METHOD_NAME, FOR_NAME_METHOD_SIGNATURE);
    cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, TARGET_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE);

    cv.visitLdcInsn(m_callerClassName.replace('/', '.'));
    cv.visitMethodInsn(INVOKESTATIC, CLASS_CLASS, FOR_NAME_METHOD_NAME, FOR_NAME_METHOD_SIGNATURE);
    cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, THIS_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE);

    Label finallyLabel = new Label();
    cv.visitLabel(finallyLabel);

    Label gotoFinallyLabel = new Label();
    cv.visitJumpInsn(GOTO, gotoFinallyLabel);

    Label catchLabel = new Label();
    cv.visitLabel(catchLabel);
    cv.visitVarInsn(ASTORE, 0);

    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V");

    cv.visitTypeInsn(NEW, RUNTIME_EXCEPTION_CLASS_NAME);
    cv.visitInsn(DUP);
    cv.visitLdcInsn("could not load target class using Class.forName() in generated join point base class "
            + m_joinPointClassName);

    cv.visitMethodInsn(INVOKESPECIAL, RUNTIME_EXCEPTION_CLASS_NAME, INIT_METHOD_NAME,
            RUNTIME_EXCEPTION_INIT_METHOD_SIGNATURE);

    cv.visitInsn(ATHROW);
    cv.visitLabel(gotoFinallyLabel);

    // create the enclosing static joinpoint
    createEnclosingStaticJoinPoint(cv);

    // create the metadata map
    cv.visitTypeInsn(NEW, HASH_MAP_CLASS_NAME);
    cv.visitInsn(DUP);
    cv.visitMethodInsn(INVOKESPECIAL, HASH_MAP_CLASS_NAME, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
    cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, META_DATA_FIELD_NAME, MAP_CLASS_SIGNATURE);

    // create the Signature instance
    createSignature(cv);

    // create the static JoinPoint instance
    cv.visitTypeInsn(NEW, m_joinPointClassName);
    cv.visitInsn(DUP);
    cv.visitMethodInsn(INVOKESPECIAL, m_joinPointClassName, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
    cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, OPTIMIZED_JOIN_POINT_INSTANCE_FIELD_NAME,
            L + m_joinPointClassName + SEMICOLON);

    // ensure aspect factories are all loaded
    for (int i = 0; i < m_aspectInfos.length; i++) {
        AspectInfo m_aspectInfo = m_aspectInfos[i];

        cv.visitLdcInsn(m_aspectInfo.getAspectFactoryClassName());
        cv.visitLdcInsn(m_aspectInfo.getAspectDefinition().getSystemDefinition().getUuid());
        cv.visitLdcInsn(m_aspectInfo.getAspectClassName());
        cv.visitLdcInsn(m_aspectInfo.getAspectQualifiedName());
        AsmHelper.loadStringConstant(cv, m_aspectInfo.getAspectDefinition().getContainerClassName());
        //TODO AVF do it once per aspect def
        StringBuffer sb = new StringBuffer();
        boolean hasOne = false;
        boolean isFirst = true;
        for (Iterator iterator = m_aspectInfo.getAspectDefinition().getParameters().entrySet()
                .iterator(); iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (!isFirst) {
                sb.append(DELIMITER);
            }
            isFirst = false;
            hasOne = true;
            sb.append(entry.getKey()).append(DELIMITER).append(entry.getValue());
        }
        if (hasOne) {
            cv.visitLdcInsn(sb.toString());
        } else {
            cv.visitInsn(ACONST_NULL);
        }
        cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, THIS_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE);
        cv.visitMethodInsn(INVOKEVIRTUAL, CLASS_CLASS, GETCLASSLOADER_METHOD_NAME,
                CLASS_CLASS_GETCLASSLOADER_METHOD_SIGNATURE);
        cv.visitLdcInsn(m_aspectInfo.getDeploymentModel().toString());
        cv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(AspectFactoryManager.class), "loadAspectFactory",
                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)V");
    }

    // create and initialize the aspect fields
    for (int i = 0; i < m_aspectInfos.length; i++) {
        m_aspectInfos[i].getAspectModel().createAndStoreStaticAspectInstantiation(m_cw, cv, m_aspectInfos[i],
                m_joinPointClassName);
    }

    cv.visitInsn(RETURN);
    cv.visitTryCatchBlock(tryLabel, finallyLabel, catchLabel, CLASS_NOT_FOUND_EXCEPTION_CLASS_NAME);
    cv.visitMaxs(0, 0);
}

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

License:Open Source License

/**
 * @param cv//from   w  w w  .j a v a 2 s  .  c  o m
 * @param input
 */
private void createPartOfInvokeMethodWithAllAdviceTypes(final MethodVisitor cv, final CompilerInput input) {
    final int returnValueIndex = (input.joinPointInstanceIndex != INDEX_NOTAVAILABLE)
            ? (input.joinPointInstanceIndex + 1)
            : input.callerIndex + 1;
    final int exceptionIndex1 = returnValueIndex + 1;
    final int exceptionIndex2 = returnValueIndex + 2;

    cv.visitInsn(ACONST_NULL);
    cv.visitVarInsn(ASTORE, returnValueIndex);

    Label tryLabel = new Label();
    cv.visitLabel(tryLabel);
    if (!m_requiresProceedMethod) {
        // if no around advice then optimize by invoking the target JP directly and no call to proceed()
        createInlinedJoinPointInvocation(cv, input);
        int stackIndex = returnValueIndex;//use another int since storeType will update it
        AsmHelper.storeType(cv, stackIndex, m_returnType);
        addReturnedValueToJoinPoint(cv, input, returnValueIndex, false);
    } else {
        createInvocationToProceedMethod(cv, input.joinPointInstanceIndex, returnValueIndex);
    }

    createAfterReturningAdviceInvocations(cv, input);

    Label finallyLabel1 = new Label();
    cv.visitLabel(finallyLabel1);

    if (m_isThisAdvisable) {
        final int registerDepth = input.callerIndex + 2; // caller is using last register + possible return value
        createAfterInterceptorInvocations(cv, input.joinPointInstanceIndex, registerDepth);
    }
    createAfterFinallyAdviceInvocations(cv, input);

    Label gotoFinallyLabel = new Label();
    cv.visitJumpInsn(GOTO, gotoFinallyLabel);

    Label catchLabel = new Label();
    cv.visitLabel(catchLabel);

    // store the exception
    cv.visitVarInsn(ASTORE, exceptionIndex1);

    if (m_isThisAdvisable) {
        createAfterThrowingInterceptorInvocations(cv, input.joinPointInstanceIndex, exceptionIndex1);
    }

    // loop over the after throwing advices
    for (int i = m_afterThrowingAdviceMethodInfos.length - 1; i >= 0; i--) {
        AdviceMethodInfo advice = m_afterThrowingAdviceMethodInfos[i];

        // set the exception argument index
        advice.setSpecialArgumentIndex(exceptionIndex1);

        // if (e instanceof TYPE) {...}
        cv.visitVarInsn(ALOAD, exceptionIndex1);

        final String specialArgTypeName = advice.getSpecialArgumentTypeName();
        if (specialArgTypeName != null) {
            // after throwing <TYPE>
            cv.visitTypeInsn(INSTANCEOF, specialArgTypeName);

            Label ifInstanceOfLabel = new Label();
            cv.visitJumpInsn(IFEQ, ifInstanceOfLabel);

            // after throwing advice invocation
            createAfterAdviceInvocation(cv, input, advice, exceptionIndex1);

            cv.visitLabel(ifInstanceOfLabel);
        } else {
            // after throwing
            createAfterAdviceInvocation(cv, input, advice, INDEX_NOTAVAILABLE);
        }
    }

    // rethrow exception
    cv.visitVarInsn(ALOAD, exceptionIndex1);
    cv.visitInsn(ATHROW);

    // store exception
    Label exceptionLabel = new Label();
    cv.visitLabel(exceptionLabel);
    cv.visitVarInsn(ASTORE, exceptionIndex2);

    // after finally advice invocation
    Label finallyLabel2 = new Label();
    cv.visitLabel(finallyLabel2);

    if (m_isThisAdvisable) {
        final int registerDepth = input.callerIndex + 2; // caller is using last register + possible return value
        createAfterInterceptorInvocations(cv, input.joinPointInstanceIndex, registerDepth);
    }
    createAfterFinallyAdviceInvocations(cv, input);

    // rethrow exception
    cv.visitVarInsn(ALOAD, exceptionIndex2);
    cv.visitInsn(ATHROW);
    cv.visitLabel(gotoFinallyLabel);

    // unwrap if around advice and return in all cases
    if (m_returnType.getSort() != Type.VOID) {
        if (m_requiresProceedMethod) {
            cv.visitVarInsn(ALOAD, returnValueIndex);
            AsmHelper.unwrapType(cv, m_returnType);
        } else {
            AsmHelper.loadType(cv, returnValueIndex, m_returnType);
        }
    }

    AsmHelper.addReturnStatement(cv, m_returnType);

    // build up the exception table
    cv.visitTryCatchBlock(tryLabel, finallyLabel1, catchLabel, THROWABLE_CLASS_NAME);
    cv.visitTryCatchBlock(tryLabel, finallyLabel1, exceptionLabel, null);
    cv.visitTryCatchBlock(catchLabel, finallyLabel2, exceptionLabel, null);
}

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

License:Open Source License

/**
 * @param cv/*from  www.j  a va  2  s. c  o  m*/
 * @param input
 */
private void createPartOfInvokeMethodWithoutAfterThrowingAdviceTypes(final MethodVisitor cv,
        final CompilerInput input) {
    final int returnValueIndex = (input.joinPointInstanceIndex != INDEX_NOTAVAILABLE)
            ? (input.joinPointInstanceIndex + 1)
            : input.callerIndex + 1;
    final int exceptionIndex = returnValueIndex + 1;

    cv.visitInsn(ACONST_NULL);
    cv.visitVarInsn(ASTORE, returnValueIndex);

    Label tryLabel = new Label();
    cv.visitLabel(tryLabel);
    if (!m_requiresProceedMethod) {
        // if no around advice then optimize by invoking the target JP directly and no call to proceed()
        createInlinedJoinPointInvocation(cv, input);
        int stackIndex = returnValueIndex;//use another int since storeType will update it
        AsmHelper.storeType(cv, stackIndex, m_returnType);
        addReturnedValueToJoinPoint(cv, input, returnValueIndex, false);
    } else {
        createInvocationToProceedMethod(cv, input.joinPointInstanceIndex, returnValueIndex);
    }

    createAfterReturningAdviceInvocations(cv, input);

    Label finallyLabel1 = new Label();
    cv.visitLabel(finallyLabel1);

    createAfterFinallyAdviceInvocations(cv, input);

    Label gotoFinallyLabel = new Label();
    cv.visitJumpInsn(GOTO, gotoFinallyLabel);

    Label exceptionLabel = new Label();
    cv.visitLabel(exceptionLabel);
    cv.visitVarInsn(ASTORE, exceptionIndex);

    Label finallyLabel2 = new Label();
    cv.visitLabel(finallyLabel2);

    createAfterFinallyAdviceInvocations(cv, input);

    cv.visitVarInsn(ALOAD, exceptionIndex);
    cv.visitInsn(ATHROW);

    cv.visitLabel(gotoFinallyLabel);

    // unwrap if around advice and return in all cases
    if (m_returnType.getSort() != Type.VOID) {
        if (m_requiresProceedMethod) {
            cv.visitVarInsn(ALOAD, returnValueIndex);
            AsmHelper.unwrapType(cv, m_returnType);
        } else {
            AsmHelper.loadType(cv, returnValueIndex, m_returnType);
        }
    }

    AsmHelper.addReturnStatement(cv, m_returnType);

    cv.visitTryCatchBlock(tryLabel, finallyLabel1, exceptionLabel, null);
    cv.visitTryCatchBlock(exceptionLabel, finallyLabel2, exceptionLabel, null);
}

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

License:Open Source License

/**
 * Create the proceed() method./*from w w  w .j av a 2  s. c  o m*/
 *
 * @param input a slightly different CompilerInput since jp index, is changed and caller and callee are meaningless
 *              in the proceed() method.
 */
private void createProceedMethod(CompilerInput input) {

    MethodVisitor cv = m_cw.visitMethod(ACC_PUBLIC | ACC_FINAL, PROCEED_METHOD_NAME, PROCEED_METHOD_SIGNATURE,
            null, new String[] { THROWABLE_CLASS_NAME });

    if (m_isThisAdvisable) {
        createAroundInterceptorInvocations(cv);
    }

    incrementStackFrameCounter(cv);

    // set up the labels
    Label tryLabel = new Label();
    Label defaultCaseLabel = new Label();
    Label gotoLabel = new Label();
    Label handlerLabel = new Label();
    Label endLabel = new Label();

    int nrOfCases = m_aroundAdviceMethodInfos.length;
    if (m_isThisAdvisable) {
        nrOfCases++;
    }

    Label[] caseLabels = new Label[nrOfCases];
    Label[] returnLabels = new Label[nrOfCases];
    int[] caseNumbers = new int[nrOfCases];
    for (int i = 0; i < caseLabels.length; i++) {
        caseLabels[i] = new Label();
        caseNumbers[i] = i;
    }
    for (int i = 0; i < returnLabels.length; i++) {
        returnLabels[i] = new Label();
    }

    // start try-catch block
    cv.visitLabel(tryLabel);

    // start the switch block and set the stackframe as the param to the switch
    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, STACK_FRAME_COUNTER_FIELD_NAME, I);
    cv.visitLookupSwitchInsn(defaultCaseLabel, caseNumbers, caseLabels);

    // add one case for each around advice invocation
    for (int i = 0; i < m_aroundAdviceMethodInfos.length; i++) {
        cv.visitLabel(caseLabels[i]);

        // gather advice info
        AdviceMethodInfo adviceInfo = m_aroundAdviceMethodInfos[i];

        Label endInstanceOflabel = beginRuntimeCheck(cv, input, adviceInfo.getAdviceInfo());

        // get the aspect instance
        adviceInfo.getAspectInfo().getAspectModel().loadAspect(cv, input, adviceInfo.getAspectInfo());

        // load the arguments to the advice
        adviceInfo.getAspectInfo().getAspectModel().createAroundAdviceArgumentHandling(cv, input,
                m_argumentTypes, adviceInfo);

        // invoke the advice method
        cv.visitMethodInsn(INVOKEVIRTUAL, adviceInfo.getAspectInfo().getAspectClassName(),
                adviceInfo.getAdviceInfo().getMethodName(), adviceInfo.getAdviceInfo().getMethodSignature());
        cv.visitVarInsn(ASTORE, 1);

        // we need to handle the case when the advice was skipped due to runtime check
        // that is : if (runtimeCheck) { ret = advice() } else { ret = proceed() }
        if (endInstanceOflabel != null) {
            Label elseInstanceOfLabel = new Label();
            cv.visitJumpInsn(GOTO, elseInstanceOfLabel);
            endRuntimeCheck(cv, adviceInfo.getAdviceInfo(), endInstanceOflabel);
            cv.visitVarInsn(ALOAD, 0);
            cv.visitMethodInsn(INVOKESPECIAL, m_joinPointClassName, PROCEED_METHOD_NAME,
                    PROCEED_METHOD_SIGNATURE);
            cv.visitVarInsn(ASTORE, 1);
            cv.visitLabel(elseInstanceOfLabel);
        }

        cv.visitLabel(returnLabels[i]);

        cv.visitVarInsn(ALOAD, 1);
        cv.visitInsn(ARETURN);
    }

    if (m_isThisAdvisable) {
        int delegationCaseIndex = caseLabels.length - 1;
        cv.visitLabel(caseLabels[delegationCaseIndex]);
        cv.visitVarInsn(ALOAD, 0);
        cv.visitInsn(ICONST_0);
        cv.visitFieldInsn(PUTFIELD, m_joinPointClassName, INTERCEPTOR_INDEX_FIELD_NAME, I);
        cv.visitVarInsn(ALOAD, 0);
        cv.visitMethodInsn(INVOKEVIRTUAL, m_joinPointClassName, PROCEED_METHOD_NAME, PROCEED_METHOD_SIGNATURE);

        cv.visitLabel(returnLabels[delegationCaseIndex]);

        cv.visitInsn(ARETURN);
    }

    // invoke the target join point in the default case
    cv.visitLabel(defaultCaseLabel);

    AsmHelper.prepareWrappingOfPrimitiveType(cv, Type.getReturnType(m_calleeMemberDesc));

    createJoinPointInvocation(cv);

    Type m_returnType = null;
    if (m_joinPointType != JoinPointType.CONSTRUCTOR_CALL_INT) {
        m_returnType = Type.getReturnType(m_calleeMemberDesc);
    } else {
        m_returnType = Type.getType(m_calleeClassSignature);
    }
    AsmHelper.wrapPrimitiveType(cv, m_returnType);
    cv.visitVarInsn(ASTORE, 1);

    // store it in Rtti return value
    addReturnedValueToJoinPoint(cv, input, 1, true);

    // set it as the CALLEE instance for ctor call - TODO refactor somewhere else
    if (m_joinPointType == JoinPointType.CONSTRUCTOR_CALL_INT) {
        cv.visitVarInsn(ALOAD, 0);
        cv.visitVarInsn(ALOAD, 1);
        cv.visitFieldInsn(PUTFIELD, m_joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, m_calleeClassSignature);
    }

    cv.visitLabel(gotoLabel);

    cv.visitVarInsn(ALOAD, 1);
    cv.visitInsn(ARETURN);

    // finally clause
    cv.visitLabel(handlerLabel);
    cv.visitVarInsn(ASTORE, 2);
    cv.visitLabel(endLabel);

    cv.visitVarInsn(ALOAD, 2);
    cv.visitInsn(ATHROW);

    // set up the label table
    cv.visitTryCatchBlock(tryLabel, returnLabels[0], handlerLabel, null);
    for (int i = 1; i < caseLabels.length; i++) {
        Label caseLabel = caseLabels[i];
        Label returnLabel = returnLabels[i];
        cv.visitTryCatchBlock(caseLabel, returnLabel, handlerLabel, null);
    }
    cv.visitTryCatchBlock(defaultCaseLabel, gotoLabel, handlerLabel, null);
    cv.visitTryCatchBlock(handlerLabel, endLabel, handlerLabel, null);
    cv.visitMaxs(0, 0);
}

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

License:Open Source License

/**
 * Adds after returning advice invocations.
 *
 * @param cv//  ww w  .  j a  v  a2  s.c om
 * @param input
 */
private void createAfterReturningAdviceInvocations(final MethodVisitor cv, final CompilerInput input) {
    final int returnValueIndex = (input.joinPointInstanceIndex != INDEX_NOTAVAILABLE)
            ? (input.joinPointInstanceIndex + 1)
            : input.callerIndex + 1;

    if (m_isThisAdvisable) {
        createAfterReturningInterceptorInvocations(cv, input.joinPointInstanceIndex, returnValueIndex);
    }

    boolean hasPoppedReturnValueFromStack = false;
    for (int i = m_afterReturningAdviceMethodInfos.length - 1; i >= 0; i--) {
        AdviceMethodInfo advice = m_afterReturningAdviceMethodInfos[i];

        // set the return value index that will be used as arg to advice
        advice.setSpecialArgumentIndex(returnValueIndex);

        String specialArgDesc = advice.getSpecialArgumentTypeDesc();
        if (specialArgDesc == null) {
            // after returning
            createAfterAdviceInvocation(cv, input, advice, INDEX_NOTAVAILABLE);
        } else {
            // after returning <TYPE>
            if (AsmHelper.isPrimitive(m_returnType)) {
                if (m_returnType.getDescriptor().equals(specialArgDesc)) {
                    createAfterAdviceInvocation(cv, input, advice, returnValueIndex);
                }
            } else {
                cv.visitVarInsn(ALOAD, returnValueIndex);

                cv.visitTypeInsn(INSTANCEOF, advice.getSpecialArgumentTypeName());

                Label label = new Label();
                cv.visitJumpInsn(IFEQ, label);

                createAfterAdviceInvocation(cv, input, advice, returnValueIndex);

                cv.visitLabel(label);
            }
        }
    }

    // need the return value in return operation
    if (!m_requiresProceedMethod && hasPoppedReturnValueFromStack) {
        cv.visitVarInsn(ALOAD, returnValueIndex);
    }
}

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

License:Open Source License

/**
 * Ends the ifLabel of a runtime check// www .j  a v a2 s .c  om
 *
 * @param cv
 * @param adviceInfo
 * @param label      if null, then do nothing (means we did not had a runtime check)
 */
private void endRuntimeCheck(final MethodVisitor cv, final AdviceInfo adviceInfo, final Label label) {
    DeploymentModel deployModel = adviceInfo.getAspectDeploymentModel();

    if (adviceInfo.hasTargetWithRuntimeCheck() || adviceInfo.getAdviceDefinition().hasCflowOrCflowBelow()
            || DeploymentModel.PER_THIS.equals(deployModel) || DeploymentModel.PER_TARGET.equals(deployModel)) {

        cv.visitLabel(label);
    }
}

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

License:Open Source License

/**
 * Handles the around interceptor invocations.
 *
 * @param cv/*  w  w w.j a  va  2s .  c  o m*/
 */
private void createAroundInterceptorInvocations(final MethodVisitor cv) {
    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, INTERCEPTOR_INDEX_FIELD_NAME, I);
    cv.visitInsn(ICONST_M1);
    Label ifStatementLabel = new Label();
    cv.visitJumpInsn(IF_ICMPEQ, ifStatementLabel);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, INTERCEPTOR_INDEX_FIELD_NAME, I);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, NR_OF_AROUND_INTERCEPTORS_FIELD_NAME, I);
    cv.visitJumpInsn(IF_ICMPGE, ifStatementLabel);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, AROUND_INTERCEPTORS_FIELD_NAME,
            AROUND_ADVICE_ARRAY_CLASS_SIGNATURE);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitInsn(DUP);
    cv.visitFieldInsn(GETFIELD, m_joinPointClassName, INTERCEPTOR_INDEX_FIELD_NAME, I);
    cv.visitInsn(DUP_X1);
    cv.visitInsn(ICONST_1);
    cv.visitInsn(IADD);
    cv.visitFieldInsn(PUTFIELD, m_joinPointClassName, INTERCEPTOR_INDEX_FIELD_NAME, I);
    cv.visitInsn(AALOAD);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKEINTERFACE, AROUND_ADVICE_CLASS_NAME, INTERCEPT_INVOKE_METHOD_NAME,
            AROUND_ADVICE_INVOKE_METHOD_SIGNATURE);
    cv.visitInsn(ARETURN);
    cv.visitLabel(ifStatementLabel);
}

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  v  a  2  s  .  c o  m
 * @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);
}