Example usage for org.objectweb.asm MethodVisitor visitLdcInsn

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

Introduction

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

Prototype

public void visitLdcInsn(final Object value) 

Source Link

Document

Visits a LDC instruction.

Usage

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

License:Open Source License

/**
 * Creates the signature for the join point.
 * <p/>//from   w w w. j  a v  a2s  .  c  om
 * FIXME signature field should NOT be of type Signature but of the specific type (update all refs as well)
 *
 * @param cv
 */
protected void createSignature(final MethodVisitor cv) {
    cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, TARGET_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE);
    cv.visitLdcInsn(new Integer(m_joinPointHash));

    cv.visitMethodInsn(INVOKESTATIC, SIGNATURE_FACTORY_CLASS, NEW_METHOD_SIGNATURE_METHOD_NAME,
            NEW_METHOD_SIGNATURE_METHOD_SIGNATURE);
    cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, SIGNATURE_FIELD_NAME,
            METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE);

}

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

License:Open Source License

/**
 * Initializes a static mixin field.//from  w w w. ja  va  2s.  com
 *
 * @param mv
 * @param fieldInfo
 */
private void initializeStaticMixinField(final MethodVisitor mv, final MixinFieldInfo fieldInfo) {
    mv.visitLdcInsn(fieldInfo.mixinClassInfo.getName().replace('/', '.'));
    if (fieldInfo.isPerJVM) {
        mv.visitFieldInsn(GETSTATIC, m_declaringTypeName, TARGET_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
        mv.visitMethodInsn(INVOKEVIRTUAL, CLASS_CLASS, GETCLASSLOADER_METHOD_NAME,
                CLASS_CLASS_GETCLASSLOADER_METHOD_SIGNATURE);
        mv.visitMethodInsn(INVOKESTATIC, MIXINS_CLASS_NAME, MIXIN_OF_METHOD_NAME,
                MIXIN_OF_METHOD_PER_JVM_SIGNATURE);
    } else {
        mv.visitFieldInsn(GETSTATIC, m_declaringTypeName, TARGET_CLASS_FIELD_NAME, CLASS_CLASS_SIGNATURE);
        mv.visitMethodInsn(INVOKESTATIC, MIXINS_CLASS_NAME, MIXIN_OF_METHOD_NAME,
                MIXIN_OF_METHOD_PER_CLASS_SIGNATURE);
    }
    mv.visitTypeInsn(CHECKCAST, fieldInfo.mixinClassInfo.getName().replace('.', '/'));
    mv.visitFieldInsn(PUTSTATIC, m_declaringTypeName, fieldInfo.fieldName,
            fieldInfo.mixinClassInfo.getSignature());
}

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

License:Open Source License

/**
 * Initializes a member mixin field./*from  w w  w  .  j  ava 2  s .  com*/
 *
 * @param mv
 * @param fieldInfo
 */
private void initializeMemberMixinField(final MethodVisitor mv, final MixinFieldInfo fieldInfo) {
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(fieldInfo.mixinClassInfo.getName().replace('/', '.'));
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, MIXINS_CLASS_NAME, MIXIN_OF_METHOD_NAME,
            MIXIN_OF_METHOD_PER_INSTANCE_SIGNATURE);
    mv.visitTypeInsn(CHECKCAST, fieldInfo.mixinClassInfo.getName().replace('.', '/'));
    mv.visitFieldInsn(PUTFIELD, m_declaringTypeName, fieldInfo.fieldName,
            fieldInfo.mixinClassInfo.getSignature());
}

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 {/*ww w.j  a  va2 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.AssertionWriter.java

License:Apache License

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

    BooleanExpression booleanExpression = statement.getBooleanExpression();
    // push expression string onto stack
    String expressionText = booleanExpression.getText();
    List<String> list = new ArrayList<String>();
    addVariableNames(booleanExpression, list);
    if (list.isEmpty()) {
        mv.visitLdcInsn(expressionText);
    } else {//from w  w w  .  ja  v  a2  s.c  o m
        boolean first = true;

        // let's create a new expression
        mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
        mv.visitInsn(DUP);
        mv.visitLdcInsn(expressionText + ". Values: ");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false);
        //TODO: maybe use more special type StringBuffer here
        operandStack.push(ClassHelper.OBJECT_TYPE);
        int tempIndex = controller.getCompileStack().defineTemporaryVariable("assert", true);

        for (String name : list) {
            String text = name + " = ";
            if (first) {
                first = false;
            } else {
                text = ", " + text;
            }

            mv.visitVarInsn(ALOAD, tempIndex);
            mv.visitLdcInsn(text);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                    "(Ljava/lang/Object;)Ljava/lang/StringBuffer;", false);
            mv.visitInsn(POP);

            mv.visitVarInsn(ALOAD, tempIndex);
            new VariableExpression(name).visit(controller.getAcg());
            operandStack.box();
            mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "toString",
                    "(Ljava/lang/Object;)Ljava/lang/String;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false);
            mv.visitInsn(POP);
            operandStack.remove(1);
        }
        mv.visitVarInsn(ALOAD, tempIndex);
        controller.getCompileStack().removeVar(tempIndex);
    }
}

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

License:Apache License

private void record(int normalizedColumn) {
    if (assertionTracker == null)
        return;//  w w  w  . java  2s.com

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

    operandStack.dup();
    operandStack.box();

    mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
    operandStack.push(ClassHelper.OBJECT_TYPE);
    //helper.swapWithObject(ClassHelper.OBJECT_TYPE);
    operandStack.swap();
    mv.visitLdcInsn(normalizedColumn);
    mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "record",
            "(Ljava/lang/Object;I)Ljava/lang/Object;", false);
    mv.visitInsn(POP);
    operandStack.remove(2);
}

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

License:Apache License

private void loadInitValue(ClassNode type) {
    MethodVisitor mv = controller.getMethodVisitor();
    if (ClassHelper.isPrimitiveType(type)) {
        mv.visitLdcInsn(0);
    } else {//from  ww w. j a v  a2  s.  c  o m
        mv.visitInsn(ACONST_NULL);
    }
    controller.getOperandStack().push(type);
}

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

License:Apache License

public static void pushConstant(MethodVisitor mv, int value) {
    switch (value) {
    case 0:// w ww .j  a  va  2 s.  c  o m
        mv.visitInsn(ICONST_0);
        break;
    case 1:
        mv.visitInsn(ICONST_1);
        break;
    case 2:
        mv.visitInsn(ICONST_2);
        break;
    case 3:
        mv.visitInsn(ICONST_3);
        break;
    case 4:
        mv.visitInsn(ICONST_4);
        break;
    case 5:
        mv.visitInsn(ICONST_5);
        break;
    default:
        if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
            mv.visitIntInsn(BIPUSH, value);
        } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
            mv.visitIntInsn(SIPUSH, value);
        } else {
            mv.visitLdcInsn(value);
        }
    }
}

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

License:Apache License

/**
 * Visits a class literal. If the type of the classnode is a primitive type,
 * the generated bytecode will be a GETSTATIC Integer.TYPE.
 * If the classnode is not a primitive type, we will generate a LDC instruction.
 *///w w w . j a v a  2s. c  o m
public static void visitClassLiteral(MethodVisitor mv, ClassNode classNode) {
    if (ClassHelper.isPrimitiveType(classNode)) {
        mv.visitFieldInsn(GETSTATIC, getClassInternalName(ClassHelper.getWrapper(classNode)), "TYPE",
                "Ljava/lang/Class;");
    } else {
        mv.visitLdcInsn(org.objectweb.asm.Type.getType(getTypeDescription(classNode)));
    }
}

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

License:Apache License

private void generateCreateCallSiteArray() {
    List<String> callSiteInitMethods = new LinkedList<String>();
    int index = 0;
    int methodIndex = 0;
    final int size = callSites.size();
    final int maxArrayInit = 5000;
    // create array initialization methods
    while (index < size) {
        methodIndex++;//  w w  w .  j  a v a2s.c om
        String methodName = "$createCallSiteArray_" + methodIndex;
        callSiteInitMethods.add(methodName);
        MethodVisitor mv = controller.getClassVisitor().visitMethod(MOD_PRIVSS, methodName,
                "([Ljava/lang/String;)V", null, null);
        controller.setMethodVisitor(mv);
        mv.visitCode();
        int methodLimit = size;
        // check if the next block is over the max allowed
        if ((methodLimit - index) > maxArrayInit) {
            methodLimit = index + maxArrayInit;
        }
        for (; index < methodLimit; index++) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitLdcInsn(index);
            mv.visitLdcInsn(callSites.get(index));
            mv.visitInsn(AASTORE);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    // create base createCallSiteArray method
    MethodVisitor mv = controller.getClassVisitor().visitMethod(MOD_PRIVSS, CREATE_CSA_METHOD,
            GET_CALLSITEARRAY_DESC, null, null);
    controller.setMethodVisitor(mv);
    mv.visitCode();
    mv.visitLdcInsn(size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
    mv.visitVarInsn(ASTORE, 0);
    for (String methodName : callSiteInitMethods) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, controller.getInternalClassName(), methodName,
                "([Ljava/lang/String;)V", false);
    }

    mv.visitTypeInsn(NEW, CALLSITE_ARRAY_CLASS);
    mv.visitInsn(DUP);
    controller.getAcg().visitClassExpression(new ClassExpression(controller.getClassNode()));

    mv.visitVarInsn(ALOAD, 0);

    mv.visitMethodInsn(INVOKESPECIAL, CALLSITE_ARRAY_CLASS, "<init>", "(Ljava/lang/Class;[Ljava/lang/String;)V",
            false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}