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.codehaus.aspectwerkz.transform.inlining.compiler.AspectWerkzAspectModel.java

License:Open Source License

/**
 * Generate a "if Aspects.hasAspect(qName, instance)"
 *
 * @param cv/*from   ww w . j av  a 2 s. c o m*/
 * @param perInstanceIndex
 * @param aspectInfo
 * @return
 */
private Label pushPerXCondition(final MethodVisitor cv, final int perInstanceIndex,
        final AspectInfo aspectInfo) {
    Label hasAspectCheck = new Label();

    cv.visitVarInsn(ALOAD, perInstanceIndex);
    cv.visitMethodInsn(INVOKESTATIC, aspectInfo.getAspectFactoryClassName(), FACTORY_HASASPECT_METHOD_NAME,
            FACTORY_HASASPECT_PEROBJECT_METHOD_SIGNATURE);
    cv.visitJumpInsn(IFEQ, hasAspectCheck);

    return hasAspectCheck;
}

From source file:org.codehaus.aspectwerkz.transform.inlining.weaver.InstanceLevelAspectVisitor.java

License:Open Source License

/**
 * Adds the getAspect(..) method to the target class.
 *
 * @param name the class name of the target class
 *///from  w  w  w .  j  av  a2s.  c o  m
private void addGetAspectMethod(final String name) {
    MethodVisitor cv = super.visitMethod(ACC_PUBLIC + ACC_SYNTHETIC, INSTANCE_LEVEL_GETASPECT_METHOD_NAME,
            INSTANCE_LEVEL_GETASPECT_METHOD_SIGNATURE, null, null);

    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, name, INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
            INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE);
    //--
    cv.visitInsn(DUP);
    Label ifMapNonNull = new Label();
    cv.visitJumpInsn(IFNONNULL, ifMapNonNull);
    cv.visitInsn(ACONST_NULL);
    cv.visitInsn(ARETURN);
    cv.visitLabel(ifMapNonNull);

    //        // if == null, field = new HashMap()
    //        Label ifFieldNullNotLabel = new Label();
    //        cv.visitJumpInsn(IFNONNULL, ifFieldNullNotLabel);
    //        cv.visitVarInsn(ALOAD, 0);
    //        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(
    //                PUTFIELD,
    //                name,
    //                INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
    //                INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE
    //        );
    //        cv.visitLabel(ifFieldNullNotLabel);
    //
    //        cv.visitVarInsn(ALOAD, 0);
    //        cv.visitFieldInsn(
    //                GETFIELD,
    //                name,
    //                INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
    //                INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE
    //        );
    //
    //        cv.visitVarInsn(ALOAD, 2);//qName
    //        cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, GET_METHOD_NAME, GET_METHOD_SIGNATURE);
    //        cv.visitVarInsn(ASTORE, 4);
    //        cv.visitVarInsn(ALOAD, 4);
    //        Label ifNullNotLabel = new Label();
    //        cv.visitJumpInsn(IFNONNULL, ifNullNotLabel);
    //        cv.visitVarInsn(ALOAD, 2);//qName
    //        cv.visitVarInsn(ALOAD, 3);//containerClassName
    //        cv.visitVarInsn(ALOAD, 0);//this (perInstance)
    //        cv.visitMethodInsn(
    //                INVOKESTATIC,
    //                ASPECTS_CLASS_NAME,
    //                ASPECT_OF_METHOD_NAME,
    //                ASPECT_OF_PER_INSTANCE_METHOD_SIGNATURE
    //        );
    //        cv.visitVarInsn(ASTORE, 4);
    //cv.visitVarInsn(ALOAD, 0);
    //--
    cv.visitVarInsn(ALOAD, 1);
    cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, GET_METHOD_NAME, GET_METHOD_SIGNATURE);
    //--
    //        cv.visitFieldInsn(
    //                GETFIELD,
    //                name,
    //                INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
    //                INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE
    //        );
    cv.visitInsn(ARETURN);
    //        cv.visitVarInsn(ALOAD, 2);
    //        cv.visitVarInsn(ALOAD, 4);
    //        cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, PUT_METHOD_NAME, PUT_METHOD_SIGNATURE);
    //        cv.visitInsn(POP);
    //        cv.visitLabel(ifNullNotLabel);
    //        cv.visitVarInsn(ALOAD, 4);
    //        cv.visitInsn(ARETURN);
    cv.visitMaxs(0, 0);

    m_ctx.markAsAdvised();
    m_isAdvised = true;
}

From source file:org.codehaus.aspectwerkz.transform.inlining.weaver.InstanceLevelAspectVisitor.java

License:Open Source License

private void addHasAspectMethod(String mapFieldName) {
    MethodVisitor cv = super.visitMethod(ACC_PUBLIC + ACC_SYNTHETIC, INSTANCE_LEVEL_HASASPECT_METHOD_NAME,
            INSTANCE_LEVEL_HASASPECT_METHOD_SIGNATURE, null, null);

    cv.visitVarInsn(ALOAD, 0);//  w  w w  . j av  a 2s. c  o m
    cv.visitFieldInsn(GETFIELD, mapFieldName, INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
            INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE);
    cv.visitInsn(DUP);
    Label ifMapNonNull = new Label();
    cv.visitJumpInsn(IFNONNULL, ifMapNonNull);
    cv.visitInsn(ICONST_0);
    cv.visitInsn(IRETURN);
    cv.visitLabel(ifMapNonNull);
    cv.visitVarInsn(ALOAD, 1);
    cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, "containsKey", "(Ljava/lang/Object;)Z");
    //cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, GET_METHOD_NAME, GET_METHOD_SIGNATURE);
    //
    //        Label ifNullLabel = new Label();
    //        cv.visitJumpInsn(IFNULL, ifNullLabel);
    //        cv.visitInsn(ICONST_1);
    //        cv.visitInsn(IRETURN);
    //        cv.visitLabel(ifNullLabel);
    //        cv.visitInsn(ICONST_0);
    cv.visitInsn(IRETURN);
    cv.visitMaxs(0, 0);

    m_ctx.markAsAdvised();
    m_isAdvised = true;
}

From source file:org.codehaus.aspectwerkz.transform.inlining.weaver.InstanceLevelAspectVisitor.java

License:Open Source License

private void addBindAspectMethod(final String name) {
    MethodVisitor cv = super.visitMethod(ACC_PUBLIC + ACC_SYNTHETIC, INSTANCE_LEVEL_BINDASPECT_METHOD_NAME,
            INSTANCE_LEVEL_BINDASPECT_METHOD_SIGNATURE, null, null);

    cv.visitVarInsn(ALOAD, 0);/*w  w w  .j a  v a 2 s  . c o m*/
    cv.visitFieldInsn(GETFIELD, name, INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
            INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE);
    // if == null, field = new HashMap()
    Label ifFieldNullNotLabel = new Label();
    cv.visitJumpInsn(IFNONNULL, ifFieldNullNotLabel);
    cv.visitVarInsn(ALOAD, 0);
    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(PUTFIELD, name, INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
            INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE);
    cv.visitLabel(ifFieldNullNotLabel);

    cv.visitVarInsn(ALOAD, 0);
    cv.visitFieldInsn(GETFIELD, name, INSTANCE_LEVEL_ASPECT_MAP_FIELD_NAME,
            INSTANCE_LEVEL_ASPECT_MAP_FIELD_SIGNATURE);
    cv.visitVarInsn(ALOAD, 1);
    cv.visitVarInsn(ALOAD, 2);
    cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME, PUT_METHOD_NAME, PUT_METHOD_SIGNATURE);
    cv.visitVarInsn(ALOAD, 2);
    cv.visitInsn(ARETURN);
}

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

License:Apache License

public void writeAssertStatement(AssertStatement statement) {
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();

    // don't rewrite assertions with message
    boolean rewriteAssert = isNull(statement.getMessageExpression());
    AssertionTracker oldTracker = assertionTracker;
    Janitor janitor = new Janitor();
    final Label tryStart = new Label();
    if (rewriteAssert) {
        assertionTracker = new AssertionTracker();
        try {/*from w  w w.j a  va  2 s .  c o  m*/
            // because source position seems to be more reliable for statements
            // than for expressions, we get the source text for the whole statement
            assertionTracker.sourceText = new SourceText(statement, controller.getSourceUnit(), janitor);
            mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/powerassert/ValueRecorder");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "<init>",
                    "()V", false);
            //TODO: maybe use more specialized type here
            controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
            assertionTracker.recorderIndex = controller.getCompileStack().defineTemporaryVariable("recorder",
                    true);
            mv.visitLabel(tryStart);
        } catch (SourceTextNotAvailableException e) {
            // set assertionTracker to null to deactivate AssertionWriter#record calls
            assertionTracker = null;
            // don't rewrite assertions w/o source text
            rewriteAssert = false;
        }
    }

    statement.getBooleanExpression().visit(controller.getAcg());

    Label exceptionThrower = operandStack.jump(IFEQ);

    // do nothing, but clear the value recorder
    if (rewriteAssert) {
        //clean up assertion recorder
        mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "clear",
                "()V", false);
    }
    Label afterAssert = new Label();
    mv.visitJumpInsn(GOTO, afterAssert);
    mv.visitLabel(exceptionThrower);

    if (rewriteAssert) {
        mv.visitLdcInsn(assertionTracker.sourceText.getNormalizedText());
        mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
        mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/powerassert/AssertionRenderer", "render",
                "(Ljava/lang/String;Lorg/codehaus/groovy/runtime/powerassert/ValueRecorder;)Ljava/lang/String;",
                false);
    } else {
        writeSourcelessAssertText(statement);
    }
    operandStack.push(ClassHelper.STRING_TYPE);
    AssertionTracker savedTracker = assertionTracker;
    assertionTracker = null;

    // now the optional exception expression
    statement.getMessageExpression().visit(controller.getAcg());
    operandStack.box();
    assertFailedMethod.call(mv);
    operandStack.remove(2); // assertFailed called static with 2 arguments 

    if (rewriteAssert) {
        final Label tryEnd = new Label();
        mv.visitLabel(tryEnd);
        mv.visitJumpInsn(GOTO, afterAssert);
        // finally block to clean assertion recorder
        final Label catchAny = new Label();
        mv.visitLabel(catchAny);
        mv.visitVarInsn(ALOAD, savedTracker.recorderIndex);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "clear",
                "()V", false);
        mv.visitInsn(ATHROW);
        // add catch any block to exception table
        controller.getCompileStack().addExceptionBlock(tryStart, tryEnd, catchAny, null);
    }

    mv.visitLabel(afterAssert);
    if (rewriteAssert) {
        controller.getCompileStack().removeVar(savedTracker.recorderIndex);
    }
    assertionTracker = oldTracker;
    // close possibly open file handles from getting a sample for 
    // power asserts
    janitor.cleanup();
}

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

License:Apache License

private void evaluateLogicalAndExpression(BinaryExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();

    expression.getLeftExpression().visit(acg);
    operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
    Label falseCase = operandStack.jump(IFEQ);

    expression.getRightExpression().visit(acg);
    operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
    operandStack.jump(IFEQ, falseCase);/*ww  w. j  a  v a 2 s.  c  om*/

    ConstantExpression.PRIM_TRUE.visit(acg);
    Label trueCase = new Label();
    mv.visitJumpInsn(GOTO, trueCase);

    mv.visitLabel(falseCase);
    ConstantExpression.PRIM_FALSE.visit(acg);

    mv.visitLabel(trueCase);
    operandStack.remove(1); // have to remove 1 because of the GOTO
}

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

License:Apache License

private void evaluateElvisOperatorExpression(ElvisOperatorExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    TypeChooser typeChooser = controller.getTypeChooser();

    Expression boolPart = expression.getBooleanExpression().getExpression();
    Expression falsePart = expression.getFalseExpression();

    ClassNode truePartType = typeChooser.resolveType(boolPart, controller.getClassNode());
    ClassNode falsePartType = typeChooser.resolveType(falsePart, controller.getClassNode());
    ClassNode common = WideningCategories.lowestUpperBound(truePartType, falsePartType);

    // x?:y is equal to x?x:y, which evals to 
    //      var t=x; boolean(t)?t:y
    // first we load x, dup it, convert the dupped to boolean, then 
    // jump depending on the value. For true we are done, for false we
    // have to load y, thus we first remove x and then load y. 
    // But since x and y may have different stack lengths, this cannot work
    // Thus we have to have to do the following:
    // Be X the type of x, Y the type of y and S the common supertype of 
    // X and Y, then we have to see x?:y as  
    //      var t=x;boolean(t)?S(t):S(y)
    // so we load x, dup it, store the value in a local variable (t), then 
    // do boolean conversion. In the true part load t and cast it to S, 
    // in the false part load y and cast y to S 

    // load x, dup it, store one in $t and cast the remaining one to boolean
    int mark = operandStack.getStackLength();
    boolPart.visit(controller.getAcg());
    operandStack.dup();/*  w ww  . j  a v  a2s.com*/
    if (ClassHelper.isPrimitiveType(truePartType)
            && !ClassHelper.isPrimitiveType(operandStack.getTopOperand())) {
        truePartType = ClassHelper.getWrapper(truePartType);
    }
    int retValueId = compileStack.defineTemporaryVariable("$t", truePartType, true);
    operandStack.castToBool(mark, true);

    Label l0 = operandStack.jump(IFEQ);
    // true part: load $t and cast to S
    operandStack.load(truePartType, retValueId);
    operandStack.doGroovyCast(common);
    Label l1 = new Label();
    mv.visitJumpInsn(GOTO, l1);

    // false part: load false expression and cast to S
    mv.visitLabel(l0);
    falsePart.visit(controller.getAcg());
    operandStack.doGroovyCast(common);

    // finish and cleanup
    mv.visitLabel(l1);
    compileStack.removeVar(retValueId);
    controller.getOperandStack().replace(common, 2);

}

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

License:Apache License

private void evaluateNormalTernary(TernaryExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    TypeChooser typeChooser = controller.getTypeChooser();

    Expression boolPart = expression.getBooleanExpression();
    Expression truePart = expression.getTrueExpression();
    Expression falsePart = expression.getFalseExpression();

    ClassNode truePartType = typeChooser.resolveType(truePart, controller.getClassNode());
    ClassNode falsePartType = typeChooser.resolveType(falsePart, controller.getClassNode());
    ClassNode common = WideningCategories.lowestUpperBound(truePartType, falsePartType);

    // we compile b?x:y as 
    //      boolean(b)?S(x):S(y), S = common super type of x,y
    // so we load b, do boolean conversion. 
    // In the true part load x and cast it to S, 
    // in the false part load y and cast y to S 

    // load b and convert to boolean
    int mark = operandStack.getStackLength();
    boolPart.visit(controller.getAcg());
    operandStack.castToBool(mark, true);

    Label l0 = operandStack.jump(IFEQ);
    // true part: load x and cast to S
    truePart.visit(controller.getAcg());
    operandStack.doGroovyCast(common);/*from w  w  w.ja  va  2s  .  c  o  m*/
    Label l1 = new Label();
    mv.visitJumpInsn(GOTO, l1);

    // false part: load y and cast to S
    mv.visitLabel(l0);
    falsePart.visit(controller.getAcg());
    operandStack.doGroovyCast(common);

    // finish and cleanup
    mv.visitLabel(l1);
    controller.getOperandStack().replace(common, 2);

}

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

License:Apache License

/**
 * writes some int standard operations for compares
 * @param type the token type/* w  w w  . j  a v a 2s  . c o  m*/
 * @return true if a successful std operator write
 */
protected boolean writeStdCompare(int type, boolean simulate) {
    type = type - COMPARE_NOT_EQUAL;
    // look if really compare
    if (type < 0 || type > 7)
        return false;

    if (!simulate) {
        MethodVisitor mv = controller.getMethodVisitor();
        OperandStack operandStack = controller.getOperandStack();
        // operands are on the stack already
        int bytecode = stdCompareCodes[type];
        mv.visitInsn(getCompareCode());
        Label l1 = new Label();
        mv.visitJumpInsn(bytecode, l1);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        operandStack.replace(ClassHelper.boolean_TYPE, 2);
    }
    return true;
}

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

License:Apache License

protected boolean writeSpaceship(int type, boolean simulate) {
    if (type != COMPARE_TO)
        return false;
    /*  /*from w  w w  .j  a v a 2  s. c om*/
       we will actually do
             
      (x < y) ? -1 : ((x == y) ? 0 : 1)
      which is the essence of what the call with Number would do
      this compiles to something along
              
      <x>
      <y>
      LCMP
      IFGE L1
      ICONST_M1
      GOTO L2
      L1
      <x>
      <y>
      LCMP
      IFNE L3
      ICONST_0
      GOTO L2
      L3
      ICONST_1
      L2
              
      since the operators are already on the stack and we don't want
      to load them again, we will instead duplicate them. This will
      require some pop actions in the branches!
              
      DUP4          (operands: L1L2L1L2)
      LCMP          
      IFGE L1       (operands: L1L2)
      ICONST_M1     (operands: L1L2I)
      GOTO L2
      L1
      -----         (operands: L1L2)
      LCMP
      IFNE L3       (operands: -)
      ICONST_0      (operands: I)
      GOTO L2
      L3
      - jump from L1 branch to here (operands: -)
      ICONST_1      (operands: I)
      L2  
      - if jump from GOTO L2 we have LLI, but need only I
      - if from L3 branch we get only I
              
      this means we have to pop of LL before loading -1
              
      since there is no DUP4 we have to do this:
    DUP2_X1
    POP2
    DUP2_X1
    DUP2_X1
    POP2
    DUP2_X1          
    */
    if (!simulate) {
        MethodVisitor mv = controller.getMethodVisitor();
        // duplicate arguments
        doubleTwoOperands(mv);

        Label l1 = new Label();
        mv.visitInsn(getCompareCode());
        mv.visitJumpInsn(IFGE, l1);
        // no jump, so -1, need to pop off surplus LL
        removeTwoOperands(mv);
        mv.visitInsn(ICONST_M1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);

        mv.visitLabel(l1);
        Label l3 = new Label();
        mv.visitInsn(getCompareCode());
        mv.visitJumpInsn(IFNE, l3);
        mv.visitInsn(ICONST_0);
        mv.visitJumpInsn(GOTO, l2);

        mv.visitLabel(l3);
        mv.visitInsn(ICONST_1);

        controller.getOperandStack().replace(ClassHelper.int_TYPE, 2);
    }
    return true;
}